Reputation:
In my current working directory I have the dir ROOT/ with some files inside.
I know I can exec cp -r ROOT/* /dst
and I have no problems.
But if I open my Python console and I write this:
import subprocess
subprocess.call(['cp', '-r', 'ROOT/*', '/dst'])
It doesn't work!
I have this error: cp: cannot stat ROOT/*: No such file or directory
Can you help me?
Upvotes: 11
Views: 11694
Reputation: 2635
Provide the command as list instead of the string + list.
The following two commands are same:-
First Command:-
test=subprocess.Popen(['rm','aa','bb'])
Second command:-
list1=['rm','aa','bb']
test=subprocess.Popen(list1)
So to copy multiple files, one need to get the list of files using blob and then add 'cp' to the front of list and destination to the end of list and provide the list to subprocess.Popen().
Like:-
list1=blob.blob("*.py")
list1=['cp']+list1+['/home/rahul']
xx=subprocess.Popen(list1)
It will do the work.
Upvotes: 0
Reputation: 1810
Just came across this while trying to do something similar.
The * will not be expanded to filenames
Exactly. If you look at the man page of cp
you can call it with any number of source arguments and you can easily change the order of the arguments with the -t
switch.
import glob
import subprocess
subprocess.call(['cp', '-rt', '/dst'] + glob.glob('ROOT/*'))
Upvotes: 10
Reputation: 13257
Try
subprocess.call('cp -r ROOT/* /dst', shell=True)
Note the use of a single string rather than an array here.
Or build up your own implementation with listdir and copy
Upvotes: 7
Reputation:
The *
will not be expanded to filenames. This is a function of the shell. Here you actually want to copy a file named *. Use subprocess.call()
with the parameter shell=True
.
Upvotes: 4