Use the __methods__ technique that we
saw earlier to see the methods of a file object.
>>> myOutputFile = open('peak_deflection.txt','w')
>>> myOutputFile.__methods__
['close', 'fileno', 'flush', 'isatty', 'read',
'readinto', 'readline', 'readlines', 'seek', 'tell',
'truncate', 'write', 'writelines']
The readline()
method reads a single line
from a file into a string, including the new line character that terminates the
string. The readlines()
method reads all the
lines in a file into a list. The write()
function writes a string to a file. Look at the standard Python documentation
on the official Python website (http://www.python.org) for a description
of functions that operate on files. File objects are described in the
Built-in Types section of the Python Library
Reference.
The following example reads each line of a text file and changes the line to
uppercase characters:
# Read-only is the default access mode
>>> inputFile = open('foam.txt')
# You must declare write access
>>> outputFile = open('upper.txt','w')
>>> lines = inputFile.readlines()
>>> for line in lines:
... newLine = line.upper()
... outputFile.write(newLine)
...
>>> inputFile.close()
>>> outputFile.close()
The first line opens the input file; you do not need the 'r'
because read-only is the default access mode. The next line opens a new file to which you will
write. You read the lines in the input file into a list. Finally, you enter a loop that
converts each line to uppercase characters and writes the result to the output file. The final
two lines close the files.