简体   繁体   中英

Run a python script recursively indicating input/output paths/names

I have directory containing multiple subdirectories, all of which contain a file named sample.fas . Here, I want to run a python script ( script.py ) in each file sample.fas of the subdirectories, an export the output(s) with the name of each of their subdirectories.

However, the script needs the user to indicate the path/name of the input, and not create automatically the outputs (it's necessary to specify the path/name). Like this:

script.py sample_1.fas output_1a.nex output_1b.fas

I try using this lines, without success:

while find . -name '*.fas'; # find the *fas files
do python script.py $*.fas > /path/output_1a output_1b; # run the script and export the two outputs
done

So, I want to create a bash that read each sample.fas from all subdirectories (run the script recursively), and export the outputs with the names of their subdirectories.

I would appreciate any help.

One quick way of doing this would be something like:

for x in $(find . -type f -name *.fas); do 
    /usr/bin/python /my/full/path/to/script.py ${x} > /my/path/$(basename $(dirname ${x}))
done

This is running the script against all .fas files identified in the current directory (subdirectories included) and then redirects whatever the python script is outputting to a file named like the directory in which the currently processed .fas file was located. This file is created in /my/path/ .

There is an assumption here (well, a few), and that is that all the directories which contain .fas files have unique names. Also, the paths are supposed not to have any spaces in them, this can be fixed with proper quoting. Another assumption is that the script is always outputting valid data (this just redirects all output from the script to that file). However, this should hopefully get you going in the right direction.

But I get the feeling that I didn't properly understand your question. If this is the case, could you rephrase and maybe provide a tree showing how the directories and sub-directories are structured like?

Also, if my answer is helping you, I would appreciate it if you could mark it as the accepted answer by clicking the check mark on the left.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM