[Tutor] Executing a .sh script in a for loop

Alan Gauld alan.gauld at yahoo.co.uk
Wed Aug 19 05:00:37 EDT 2020


On 19/08/2020 06:54, D.P.Noone at lumc.nl wrote:

> for root, dirs, files in os.walk("."):
>   for filename in files:
>     if filename.endswith("fileending.txt"):
>       subprocess.Popen('./script.sh',)
>       print("Done")

> This is the same as if I just type script.sh in the command line in linux, 
> but do not target it to any file specifically. i.e.

Correct. You have just answered your own question.
How do you suppose script.sh is supposed to know what your
python code is doing? There is no magic communication channel.
You need to tell it the file name when you call it just as
you do at the command line.

Popen() can take arguments as well as the script name.

sub.Popen( ['./script.sh', filename] )

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list