Subprocess cp returns error - bufsize must be integer [duplicate]

删除回忆录丶 提交于 2020-01-14 07:35:07

问题


I'm trying to copy from one directory to another, and rename them at the same time by calling 'cp' like so:

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)

But it returns:

 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

What am I doing wrong?


回答1:


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

in is a python keyword. better use inp as variable name. Also, subprocess.call() expects a list of arguments, not multiple arguments:

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


来源:https://stackoverflow.com/questions/34156193/subprocess-cp-returns-error-bufsize-must-be-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!