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

Flynn, Stephen (Life & Pensions) Steve.Flynn at capita.com
Wed Aug 19 04:47:07 EDT 2020


At no point do you ever actually try to run the script.sh code with a filename as its first argument. You effectively call "script.sh" however many times you find a file which matches your filename requirements but never tell it which filename to operate on.

Call "script.sh" and also supply the filename variable as it's first argument.

S. 

-----Original Message-----
From: Tutor <tutor-bounces+steve.flynn=capita.co.uk at python.org> On Behalf Of D.P.Noone at lumc.nl
Sent: 19 August 2020 06:55
To: tutor at python.org
Subject: [Tutor] Executing a .sh script in a for loop

**EXTERNAL**

Hey all,

I am trying to use a for loop to execute a .sh script on every file in a directory that ends with a particular ending and extension.

My loop successfully loops over the files with the correct endings, opens the script but does not execute it correctly on the file.

Here is the for loop:

import os
import sys
import subprocess

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

The result is I get this repeated for every file I want in the directory:

Usage: script.sh output_from_fileending.txt 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.

> script.sh

Usage: script.sh output_from_fileending.txt

The script usually works if you do:

>script.sh targetfile_with_fileending.txt

Generated plots of 1D profiles and fits:
targetfile_plot.pdf

Any ideas?

Thanks in advance,
Dylan



_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


This message has been scanned by Capita systems, but if you believe it to be spam, please send it to spam at forcepoint.com.

Messages sent to spam at forcepoint.com are queued for email analysis by Forcepoint Threat Lab.
This email originates from outside of Capita.
Keep this in mind before responding, opening attachments or clicking any links. Unless you recognise the sender and know the content is safe.
If in any doubt, the grammar and spelling are poor, or the name doesn't match the email address then please contact the sender via an alternate known method.



This email is security checked and subject to the disclaimer on web-page: https://www.capita.com/email-disclaimer.aspx


More information about the Tutor mailing list