Reading Files

Now its time to start reading text files but first we need to open them. Using built in function  open(‘filename.txt’) we’re able to open files. This function creates a file object, which would be utilized to call other support methods associated with it. Now in previous post we’ve saved some text in .txt format under name P-files.txt if you want you can create your own file and use it in this exercise. Be sure to check the current working directory in python using command os.getcwd() and be sure that the .txt file is in this folder (for more information check out post: “).
>>>filename = open(‘P-files.txt’)
 >>> count = 0
>>> for line in filename:
...     count = count + 1
...
>>> print 'Line Count: ', count
Line Count:  5
The reason that the open function does not read the entire file is that the file might be quite large with many gigabytes of data. The open statement takes the same amount of time regardless of the size of the file. The for loop actually causes the data to be read from the file. When the file is read using a for loop in this manner, Python takes care of splitting the data in the file into separate lines using the newline character. Python reads each line through the newline and includes the newline as the last character in the line variable for each iteration of the for loop.
Because the for loop reads the data one line at a time, it can efficiently read and count the lines in very large files without running out of main memory to store the data. The above program can count the lines in any size file using very little memory since each line is read, counted, and then discarded.

If you know the file is relatively small compared to the size of your main memory, you can read the whole file into one string using the read method on the file handle.

Nema komentara:

Objavi komentar