0

I have two folders one called text_file and binary_file insides of those folders are multiple files numbered 1,2,3.. I have python program that I wrote and I want to use shell script which runs the python script with numbered files once at a time

Here is an example,

 python pythonic_script 1_txt_file.txt 1_bin_file.bi
 python pythonic_script 2_txt_file.txt 2_bin_file.bi

Instead of doing this, I want to write a shell script which runs the python program with all numbered files separately.

Best

1
  • sort the two directories and zip Commented Jul 2, 2015 at 20:45

1 Answer 1

1

I hope your two directories contain symmetrically equal number of the files and their naming is strictly followed according to your description.

Consider going over following steps

Figure out the number of files in one of the directory

#!/bin/sh

n=$(find PATH_TO_YOUR_DIRECOTRY/text_file/ -maxdepth 1 -type f|wc -l)

Organize a for loop and execute your script

for ((i=1;i<n;i++))
do
   python PATH_TO_YOUR_SCRIPT/pythonic_script PATH_TO_TEXT_DIR/$i_txt_file.txt PATH_TO_BIN_DIR/$i_bin_file.bi
done

P.S. Here is a very good book on shell scripting

Sign up to request clarification or add additional context in comments.

2 Comments

I have the feeling that - if the directory contains both txt and bi files - n will be doubled. Even more if other files are listed. I would suggest to put -name '*.txt' as find command line parameter
OP states that he has two folders, I suppose that he has text and binary files separated. Just in case they are not, the provided answer should be sufficient to adapt it to his own needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.