Trying to loop through folders and execute an existing python script.

I am trying to loop through lots and lots of folders and use the names of the folders to run a Python script which has parameters.
E.g.
-- setup_refs -n John -f England/London/Hackney/John -c con/con.cnf

Normally to run `setup_refs` once from command line it's: `python setup_refs.py -n John -f England/London/Hackney/ref -c con/con.cnf`

The program folder (/home/program/) contains:

  • setup.refs.py (script)
  • ref (folder)
  • con (folder) - con.cnf (file)

cd /home/program/ ;

for fldr in /home/program/ref/* ; do

    echo "Setting up ---> $fldr" ;

    basenm=${fldr##*/} ; new_fldr=${basenm%%_*} ; 

    python /home/program/setup_refs.py -f "$new_$fldr" -n /home/program/ref/"$new_$fldr" -c /home/program/config/config.cnf ; 

    echo "$fldr setup complete" ;

done

echo "Automated setup complete." ;


Can this be done?

Observations:
You use the -n and -f switches inconsistently in the code compared to your example.

In your code you use /home/program/config/config.cnf rather than your required con/con.cnf

You don't need the trailing semicolons at the end of each line in bash or other bourne-derived shellscripts.

If you use:

cd /home/program/ref
for fldr in *

you don't need the extra step of removing the directory part of the filename. You will, however be in a different directory than the one you start out in (this may be a problem, but you are using full pathnames anyway).

Stupid question: In the above code if there is a folder (file?) named john_smith your code will set the $new_fldr variable to john. What happens to a folder named john?

Another stupid question: Have you tried running that code?

Andrew

  1. I change directory into cd /home/p995824/programs/kmerid-master/ because the setup.py script is in there where I run it.
  2. Good point, I only need new_fldr=${fldr##*/} because the folder names are one word.
  3. When I run the script it returns:
 file "/home/programs/setup_refs.py", line 7, in <module>
    import ConfigParser
ModuleNotFoundError: No module named 'ConfigParser'

Please use code tags, not icode tags.

ConfigParser.py is not in your python path. Is it installed? If not, install it. Is it perhaps in a python virtualenv that you forgot to "enable"?

Andrew