我正在尝试从一个目录复制到另一个目录,并通过调用'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, insubprocess.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
我究竟做错了什么?
subprocess.call('cp', in, out)
in
是一个python关键字.更好地inp
用作变量名.还subprocess.call()
需要一个list
参数,而不是多个参数:
for inp, out, in zip(directories, output): subprocess.call(['cp', inp, out])