Text files Flashcards
reading text files:
read( )
Reading from a file moves the file pointer to right after the part of the file that was read. This means that if you use the read() method without arguments, the file pointer is moved to the end of the file. This entails that if you would try to read() from it a second time, nothing would be read, as there is nothing to be read after the spot where the file pointer is.
opening a file:
open( filename )
The second argument is the “mode”. The mode indicates how you want to treat the file. The default mode (which is picked when you do not supply the second argument) is opening the file as a text file for reading only.
closing a file:
close( )
or
with open( filename ) as handle: statements
the second way:
This syntactic construction has the advantage that the file will be closed automatically after the block ends, so you do not need to include an explicit close() call. This construction is typically Python; you do not see it in many other programming languages.
display the contents of a file:
readline( )
or
readlines( )
A collary to the readline() method is the readlines() method. readlines() reads all the lines in the file, and returns them as a list of strings. The strings include the newline characters.
Both the read() and readlines() method read a whole file at once.
For long files you might not have enough memory to store the file contents efficiently. In such circumstances (or when you do not know the file size), you should read a file line by line with the readline() method.
be careful the orginal text can be covered!!
writing text files:
fp = open( filename, “w”)
fp.write( text )
or
writelines( )
fp.write( )
To write something to a text file, you use the write() method with as argument a string that you want to write to the file. The example code below writes ten lines (line1 to line10) to a file. It then opens the file, reads the contents, and displays them.
all the text is in the file, but it all is on one line. There are no newlines in between.
fp.writelines( )
You can write a list of strings at once, by using the writelines() method that gets the list as argument. Each of the strings in the list must end in a newline character if you want those newline characters in the output file. writelines() is the opposite of readlines(); if you use the list that readlines() returns as argument for writelines(), the contents of the output file will be exactly the same as the contents of the input file.
appending to text files:
fp = open(filename, “a”)
fp.write( text )
“Appending” refers to writing at the end of an existing file. When you open a file for appending, the contents are not erased, but the file pointer is placed at the end of the file, where you can then write new data. You open a file in “append” mode by using “a” as the mode argument when opening the file.
os.path module methods :
import os.path
exists( filename) isfile( filename) isdir( filename) join( ) basename( ) dirname( ) getsize( )
exists( filename)
The function exists() gets a path as argument, and returns True if that path exists, and False if it does not.
isfile()
tests if the path that is supplied as argument is a file. If it is, it returns True. If it is not, it returns False. If the path does not exist, the function also returns False.
isdir()
tests if the path that is supplied as argument is a directory. If it is, it returns True. If it is not, it returns False. If the path does not exist, the function also returns False.
join()
takes one or more parts of a path as argument, and concatenates them reasonably intelligently to a legal name for a path. This means that it will add and remove slashes as needed. join() is particularly handy in combination with listdir().
basename()
extracts the filename from a path, and returns it.
dirname()
extracts the directory name from a path, and returns it.
getsize()
gets the size of the file that is supplied as argument, and returns it as an integer. The file must exist, otherwise you get a runtime error.
file encoding
Text files use an “encoding”, i.e., a system that prescribes how characters in the files are supposed to be interpreted. This encoding may differ between operating systems. You can see the preferred encoding that your system uses with a call to sys.getfilesystemencoding().
from sys import getfilesystemencoding
print( getfilesystemencoding() )