python窗口解释for循环到cmd命令的值

2024-10-02 00:21:30 发布

您现在位置:Python中文网/ 问答频道 /正文

这是密码

    from subprocess import Popen, PIPE

saveerr = sys.stderr 
fsock = open('error.log', 'w') 
sys.stderr = sys.stdout = fsock

D = {}

D['\\\\aucb-net-01\\d$'] = '\\\\nasaudc01\\remote_site_sync\\aucb-net-01'
D['\\\\aupw-file-01\\e$'] = '\\\\nasaudc01\\remote_site_sync\\aupw-file-01'

for k,v in sorted(D.items()):
    print (k,":",v) 
    cmd = 'robocopy {} {} /E /MIR /W:2 /R:1'.format(k,v)
    p = Popen(cmd, stdout=PIPE, bufsize=1, universal_newlines=True)
    for line in p.stdout:
        print(line)

我想在robocopy命令后面的cmd中插入“K”和“v”的值,这样在for循环中,它将对dictionary D={}中提到的所有源和目标执行robocopy

我还希望脚本检查robocopy输出日志中的失败错误.log文件

         Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :      2575         0      2575         0         0         0
Files :      6039         0      6039         0         2         0
Bytes :   1.547 g         0   1.547 g         0         0         0
Times :   0:00:53   0:00:00                       0:00:00   0:00:53

Ended : Tue Aug 30 04:32:48 2016

如果两个文件失败,那么脚本应该向某个电子邮件地址发送邮件。你知道吗


Tags: cmdlogfornetremotestderrstdoutsys
1条回答
网友
1楼 · 发布于 2024-10-02 00:21:30

Python的方法是:

cmd = 'robocopy {} {} /E /MIR /W:2 /R:1'.format(k, v)

或者,在Python 3.6中:

cmd = f'robocopy {k} {v} /E /MIR /W:2 /R:1'

然而,这是错误的!如果kv有空格,它将失败,它可以是security hazard(想象一下如果k = '; rm -rf /;)。生成子流程的正确方法是:

cmd = ['robocopy', k, v, '/E', '/MIR', '/W:2', '/R:1']

相关问题 更多 >

    热门问题