Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
in is a reserved word, you can't use it in a variable name. –  Colonel Thirty Two Nov 20 '14 at 21:35
    
See this answer stackoverflow.com/a/1009879/2676531 and its first comment. –  Celeo Nov 20 '14 at 21:37
    
sys.argv[0] is not the first argument rather the name of the script you are running. –  igon Nov 20 '14 at 21:39
    
why not have a try? sys.argv[1] is the name of the file, not 0. –  HuStmpHrrr Nov 20 '14 at 21:42
    
Did you even test this? Like, printing suspicious variables? –  ivan_pozdeev Nov 20 '14 at 21:42

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Many thanks Igon! Your codes were very useful. –  user27976 Nov 21 '14 at 13:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.