Problem with subprocess.call and cp

T

Torsten Bronger

Hallöchen!

The following code

from subprocess import call
call(['cp', 'subdir/*.jpg', 'othersubdir/'])

yields

cp: call of stat for "subdir/*.jpg" not possible: File or directory not found

(This may not be the real error message since it's back-translated
from German.) I could use shell=True, however, what's going wrong
here?

Tschö,
Torsten.
 
F

Fredrik Lundh

Torsten said:
The following code

from subprocess import call
call(['cp', 'subdir/*.jpg', 'othersubdir/'])

yields

cp: call of stat for "subdir/*.jpg" not possible: File or directory not found

(This may not be the real error message since it's back-translated
from German.) I could use shell=True, however, what's going wrong
here?

under Unix, it's the shell that expands glob patterns. individual commands
usually don't know anything about such patterns.

so if you run the "cp" command directly, it will look for a single file named
"subdir/*.jpg". if you run it via the shell, it will get a list of matching files
from the shell.

here's a corresponding pure-python solution, btw:

import glob, shutil
for file in glob.glob("subdir/*.jpg"):
shutil.copy(file, "othersubdir")

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top