madman
madman

Reputation: 153

Python importing files - for loops

I have two simple files that I want to open in python and based on a keyword print information in the file

file a.txt contains:

'Final

This is ready'

file b.txt contains:

'Draft

This is not ready'

I want to read these two files in and if the file reads 'Final' anywhere in the txt file to print out the rest of the text (excluding the word 'Final'). My for loop is not outputting correctly:

fileList = ['a.txt','b.txt']
firstLineCheck = 'Final\n'

for filepath in fileList: 
f = open(filepath, 'r') #openfiles
for line in f:
    if line == firstLineCheck:
       print line
    else:
         break

I feel like this is something simple - appreciate the help

Upvotes: 1

Views: 149

Answers (5)

The6thSense
The6thSense

Reputation: 8335

Since you wanted to check if Final was present in the first line you could read the file as a list and see if first element contains final if so prints the entire file except first line

fileList = ['a.txt','b.txt']
firstLineCheck = 'Final'

for filepath in fileList: 
    f = open(filepath, 'r').readlines() #openfiles
    if firstLineCheck in f[0]:
        print "".join(f[1:])

output:

This is ready'

Upvotes: 0

aswins
aswins

Reputation: 21

Assuming your keyword is the first line of the file, you can do this. This makes more sense as you could have the word "Final" somewhere in the content of "draft".

fileList = ['a.txt','b.txt']
firstLineCheck = 'Final\n'

for filepath in fileList: 
   with open(filepath, 'r') as f:
       first_line = f.readline()            # read the first line
       if first_line == firstLineCheck:
           print f.read()

Upvotes: 0

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52093

fileList = ['a.txt', 'b.txt']
firstLineCheck = 'Final\n'

for filepath in fileList: 
   with open(filepath, 'r') as f:
       line = f.readline() 
       while line:
           if line == firstLineCheck:
               print f.read()
           line = f.readline()               

Upvotes: 1

Galia Ladiray Weiss
Galia Ladiray Weiss

Reputation: 452

Assuming you want to print all lines starting a line that has only your firstLineCheck in it, and using your code ....

fileList = ['a.txt','b.txt']
firstLineCheck = 'Final\n'

for filepath in fileList: 
    f = open(filepath, 'r') #openfiles
    do_print = False
    for line in f:
        if line == firstLineCheck:
            do_print = True
            continue
        if do_print:
            print line

Note that break takes you out of the loop, and continue will move to the next iteration.

Upvotes: 0

skyking
skyking

Reputation: 14400

There are three faults in your code. First you will only print lines that match and second is that you trigger only on lines that contains only "Final", third it does not exclude the line containing "Final" as specified. The fix would be to use a flag to see if you found the "Final":

fileList = ['a.txt','b.txt']
firstLineCheck = 'Final'
firstLineFound = False


for filepath in fileList: 
    f = open(filepath, 'r') #openfiles
    for line in f:
        if firstLineFound:
            print line
        elif firstLineCheck in line:
            # print line # uncomment if you want to include the final-line
            firstLineFound = True
        else:
            break

if you wanted to trigger only on lines containing only "Final" then you should instead use firstLineCheck = "Final\n" and elif line==firstLineCheck.

Upvotes: 0

Related Questions