[Tutor] Executing shell commands

boB Stepp robertvstepp at gmail.com
Sat Feb 15 21:17:53 EST 2020


On Sat, Feb 15, 2020 at 6:42 PM Phil <phillor9 at gmail.com> wrote:

> I thought I might start with something like this and then collect the
> file names in a list and then upload them one by one. However, I'm
> missing a step from the listing of the file names and then collecting
> them. Plus the following fails with a directory does not exist error
> even though executing the same command "ls /home/phil/Downloads/*.gpx"
> from the command line shows that there isn't a problem.
>
> import subprocess
> subprocess.call(["ls", "/home/phil/Downloads/*.gpx"])

If I am interpreting the docs correctly
(https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)
in order to expand filename wildcards you have to add "shell=True":

subprocess.call("ls", "/home/phil/Downloads/*.gpx", shell=True)  # I
don't think you need the square brackets.

Also, if you have Python 3.5 or greater the docs recommend using
subprocess.run().  The link that I provided is using subprocess.run(),
though I think adding "shell=True" will work for subprocess.call() as
well.

HTH!

-- 
boB


More information about the Tutor mailing list