views:

354

answers:

2

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?

+1  A: 

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.

unbeknown
Interesting... But it doesn't work!
Right! That was Popen(). Sorry.
unbeknown
Don't you think I could simply use:os.system('cp -r ROOT/* /dst')This seems to work...
Maybe you can use the glob module to build the list of files you want to copy and pass the filenames to subprocess.call().
unbeknown
os.system() actually executes the command through a subshell. The filename expansion is then done by the shell.
unbeknown
+3  A: 

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

chrispy
Yes, it works... And I think it's exactly the same as os.system... Is it right?