0

I'm running Raspbian on a Raspberry Pi 2 and I wrote a simple Python script to copy all .png files from my home directory (~/) to a predetermined USB drive.

The command I run in terminal works:

cp -r *.png /media/KINGSTON/

In python I have the following:

from subprocess import call

# Some code

call(['cp', '-r', '*.png', '/media/KINGSTON/'])

But when I run the script it says

cp: Cannot stat `*.png' : No such file or directory

I'm definitely in the right directory when I try to copy it. pwd gives the correct results and ls shows all the correct .png files.

user3745189
  • 521
  • 1
  • 7
  • 17
  • 3
    It's because there's no shell being specified. If you add `shell=True` to your arguments it should work. See [here](https://docs.python.org/3/library/subprocess.html) for the docs, and do note that you should not accept user input to anything that uses `shell=True`. – bbayles Jun 04 '15 at 14:48
  • 1
    Refer: http://stackoverflow.com/questions/9997048/python-subprocess-wildcard-usage – Hitesh Dharamdasani Jun 04 '15 at 14:49
  • If it really says "Cannot stat '.png*'", then you have some funky non-printing characters in your string that are messing things up. It *should* say "Cannot stat '*.png'" - that is to be expected, as @Bo102010 points out. If that's just a typo in your question, then never mind... – twalberg Jun 04 '15 at 17:10
  • @twalberg it was a typo. – user3745189 Jun 04 '15 at 18:45
  • @Bo102010 This is the solution I was looking for. – user3745189 Jun 04 '15 at 18:46
  • @HiteshDharamdasani This is an example of of the solution I was looking for. – user3745189 Jun 04 '15 at 18:46
  • @Bo102010: don't use `shell=True`, and you don't even need a subprocess here: use `glob` and `shutil` Python modules instead: `for path in glob('*.png'): copyfile(path, '/media/KINGSTON/')`. If you want recursive behavior then you could use `recursive` parameter and `**`in Python 3.5+ (or `os.walk` + `fnmatch.filter` or `pathlib.Path.rglob`). – jfs Jun 04 '15 at 20:27

0 Answers0