子进程cp返回错误 - bufsize必须是整数

时间:2015-12-08 12:42:08

标签: python python-2.7 copy subprocess cp

我正在尝试从一个目录复制到另一个目录,并通过调用'cp'同时重命名它们:

directories = ['/Users/Me/Folder1/File1.txt', '/Users/Me/Folder/File2.txt']
output = ['/Users/Me/Folder2/Hello.txt', 'Users/Me/Folder2/World.txt'] 
for in, out, in zip(directories, output):
    subprocess.call('cp', in, out)

但它返回:

 File "./test.py", line 33, in <module>
    subprocess.call('cp', in, out)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 659, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

我做错了什么?

1 个答案:

答案 0 :(得分:10)

subprocess.call('cp', in, out)

in是一个python关键字。最好使用inp作为变量名。此外,subprocess.call()期望list个参数,而不是多个参数:

for inp, out, in zip(directories, output):
    subprocess.call(['cp', inp, out])