BioInformatician
BioInformatician

Reputation: 97

Shell Script to Run a python code with two different folders of The Same Numbered Files

I have two folders and each contains files that has same numbering systems which is part of the file name. For example,

Folder 1 has:

  001file_read.txt, 002file_read.txt, until 650file_read.txt

Folder 2 has:

  001filtr.tsv, 002filtr.tsv, until 650filtr.tsv

I want to run a python code using shell script which basically runs like the following:

 python my_python_prog.py 001file_read.txt  001filtr.tsv

The problem is because I have a huge number of files of both folders. I want to find a way to run the command only once where it will automatically run file001 from folder1 to file001 to folder2 and file002 from folder1 to file002 from folder2 and so on until file650 of both folders. How can I do that using shell script?

Thanks a lot for your help in advance,

Upvotes: 0

Views: 79

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74705

If you have bash, you could use a script like this:

#!/bin/bash 

for i in {001..650}; do
    file_read=dir1/${i}file_read.txt
    file_filtr=dir2/${i}filtr.tsv
    if [[ -f $file_read ]] && [[ -f $file_filtr ]]; then
        python my_python_prog.py "$file_read" "$file_filtr"
    fi
done

This loops through all numbers from 000 to 650, checks that both of the two files exists and executes the python command if they do.

If you don't have bash, you can achieve the same thing by making a few changes:

#!/bin/sh

while (( ++i <= 650 )); do
    n=$(printf '%03d' "$i")
    file_read=dir1/${n}file_read.txt
    file_filtr=dir2/${n}filtr.tsv
    if [ -f "$file_read" ] && [ -f "$file_filtr" ]; then
        python my_python_prog.py "$file_read" "$file_filtr"
    fi
done

Alternatively, you could modify your python script, perform the loop within it, and use os.path.isfile() to verify that both files exist. The advantage of this approach would be that you don't call your script 650 times.

Upvotes: 2

Related Questions