0

I'm making a script that wgets some pictures from a website and displays them. Basically it's a smart picture frame program that keeps up with a website that has it's pictures changed daily. I can't get Python's subprocess.run method to run a simple rm *.jpg:

subprocess.run(["rm", "-fv", "*.jpg"])

Alternativly I've tried:

subprocess.run(["rm", "*.jpg"])

I've done some trouble shooting and the top line there is what I've found flawed. The wget runs well and so does the rest of the script. The wget runs right after the rm:

subprocess.run(["wget", "-nd", "-p", "--accept=.jpg", "-e", "robots=off", "--reject=0.jpg", "--reject=8.jpg", "fnitemshop.com"])

I've tried adding the following lines before the rm but I've since commented out as they failed to fix my issue:

#subprocess.run(["chmod", "a+r+w+x", "*.jpg"])
#subprocess.run(["unlink", "*.jpg"])

I've also tried:

    subprocess.run(["rm", "*.jpg"], shell=True)

Needless to say I'm a bit confused.

SOLUTION

Turns out that the issue was using double-quotes, should have done single quotes. Like so:

subprocess.run(['rm *.jpg'], shell=True)

Thanks to Tamaki Sakura for his answer to this question.

Nathan P.
  • 11
  • 2

1 Answers1

1

subprocess isn’t running your command in a shell, so your * isn’t getting expanded. You can add shell=True to make it do that, but there are security implications of using a shell like that.

The documentation has all the information about this optional argument and also the security considerations to read before using a shell.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469