1

My python code is called within a bash script, and also the python script requires an input argument from bash loop. But I wasn't sure the best way to do this. Here's my attempt:

bash code:

cat filelList.txt | while read i;#file1List.txt stores file names
do
python script.py $i > $i.txt
done

python bit that i'm not sure of:

file = sys.argv[0] # not sure about this
in = open(file)
for line in iter(in): # not sure about this
    do something.......

Does anyone know the best way to write these 3 python lines? Thank you

5
  • 2
    in is a reserved word, you can't use it in a variable name. Commented Nov 20, 2014 at 21:35
  • See this answer stackoverflow.com/a/1009879/2676531 and its first comment. Commented Nov 20, 2014 at 21:37
  • sys.argv[0] is not the first argument rather the name of the script you are running. Commented Nov 20, 2014 at 21:39
  • why not have a try? sys.argv[1] is the name of the file, not 0. Commented Nov 20, 2014 at 21:42
  • Did you even test this? Like, printing suspicious variables? Commented Nov 20, 2014 at 21:42

1 Answer 1

1

Why not do everything in python? Assuming you have a folder where you have to process each file:

import sys
import os

assert os.path.isdir(sys.argv[1])
listing = os.listdir(sys.argv[1])

for filename in listing:
    with open(sys.argv[1]+"/"+filename,'r') as f:

        for line in f:
            #do something                                                                                                                                                                                                                    
            pass

Alternatively if you have a file containing a list of other files to process:

import sys
import os

assert os.path.isfile(sys.argv[1])

with open(sys.argv[1]) as filelist:
    filenames = [line.strip() for line in filelist]
    for filename in filenames:
        if not os.path.isfile(filename):
            print >> sys.stderr , "{0} is not a valid file".format(filename)
            break
        with open(filename,'r') as f:
            for line in f:
                #do something                                                                                                                                                                                                                
                pass
Sign up to request clarification or add additional context in comments.

Comments

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.