Subprocess Cp Returns Error - Bufsize Must Be Integer [Duplicate]

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/", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/", line 659, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

What am I doing wrong?

0

You've got two problems:

1 - subprocess.call() expects a list of arguments, not multiple arguments:

#wrong
subprocess.call('cp', in, out)
#right
subprocess.call(['cp', in, out])

2 - in is a python reserved keyword, change it to something like inp:

#wrong
subprocess.call(['cp', in, out])
#right
subprocess.call(['cp', inp, out])
    
2
Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest