简体   繁体   中英

Python 3.3 readlines truncating text file

I am working with Python 3.3 using PyDev for Eclipse, Alright, so this is my code:

countdata = open(countfilename, 'r')
countlist = countdata.readlines()
print(len(countlist))
genecountline = wordlist(countlist[-1])
print(genecountline)

countfilename refers to a rather lengthy text file of 7847 lines that is generated from a text file using a script given to me by the instructor in my machine learning class (I did have to convert said script to Python 3 using 2to3).

wordlist is a simple function I built that takes a line of text and returns the words in it as a list.

I pull the whole file into a list of lines so that I an refer to specific lines at will for my calculation. Whether I read them in all at once with readlines or iterate over the file and add the lines to the list one by one like this:

countdata = open(countfilename, 'r')
countlist = []
for line in countdata:
    countlist.append(line)

doesn't matter. Either way I do it, print(len(countlist)) gives me approximately 7630 , I say approximately because sometimes it is as low as 7628 or as high as 7633 . The specific line returned by countlist[-1] is always different (the file is built using a generator object, as mentioned my instructor built that script and I am not entirely sure how exactly it works).

genecountline = wordlist(countlist[-1])
print(genecountline)

I put in just to see what python thinks the last line of the file is. And when I open the file in textpad, the line it returns is in fact the line number returned by len(countlist) . In other words it appears to be ignoring the last approx. 210 lines of my file. So my question is how do I fix this, and how do I prevent it from doing this again?

If you're not reading from a static text file but from the one that generates each time you run your program, it could be that you don't close that file (in which case everything might not have been written to it). If you don't want to close it, you could flush it (.flush() method).

You should post the code that generates the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM