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

D.P.Noone at lumc.nl D.P.Noone at lumc.nl
Wed Aug 19 01:54:38 EDT 2020


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





More information about the Tutor mailing list