1

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

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
madman
  • 153
  • 1
  • 4
  • 16

5 Answers5

1
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()               
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
0

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'
The6thSense
  • 8,103
  • 8
  • 31
  • 65
  • 1
    This would print the entire file, that's not what the question specified. It should only print the rest of the file. – skyking Jul 24 '15 at 05:44
  • This didn't seem to work - wouldn't this also only work if you knew that the word Final was in the first file? imainge we had x files and wanted the computer to identify and output correct. Also, can you explain that last line in your code? never used a join and want to make sure I see what you are trying to do there. thanks. – madman Jul 25 '15 at 19:45
  • @madman this should work on all other files as well I am just checking if the first line contains the word final .Initially what I am doing is converting the file into list object .That is each element in the list is a line of the file.Hence to print it into a string format I have used `"".join()` method look at this SO [question](http://stackoverflow.com/questions/1876191/explain-python-join) on "".join(list) – The6thSense Jul 27 '15 at 05:57
0

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.

skyking
  • 13,817
  • 1
  • 35
  • 57
0

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.

  • This is a nice way to do it. When I look at this code, I would assume that the word Final would be printed as well (though it isn't and I don't want it to be) can you walk me through why only the additional lines in f[0] are printed? thanks – madman Jul 25 '15 at 19:58
  • @madman The "continue" statement after the do_print, stop the execution of the current iteration and goes to the for loop to execute the next iteration (if there's any) https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops – Galia Ladiray Weiss Jul 27 '15 at 05:13
0

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()
aswins
  • 21
  • 3