REVISED: Sunday, March 3, 2013
In this tutorial, you will learn how to save information to a file and read information from a file.
I. USING THE PYTHON INTERPRETER SHELL
A. PYTHON FILE OUTPUT OBJECTS
"Double left mouse click" the Python icon on your desktop to start the Python interpreter shell. A Python interpreter shell window similar to the following will open:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>>
After the Python interpreter shell >>> prompt the following syntax is used to create an instance of a class, called a "file object", which we will name outFile:
File object name = open('file name.file type', 'modes')
For example:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> outFile = open('output.txt', 'wt')
>>>
The "file object name" is outFile; which is assigned the "file name" by the method open( ).
The "file type" is txt; and there has to be a period between the "file name" and the "file type."
The "modes" are wt; the w is write mode, and the t is writing in text mode.
If the file output.txt already exists in the default search directory it is opened; and if output.txt does not exist it is created in your Python default hard drive folder.
B. WRITING TO A FILE
Continuing with our original example, the write( ) method is used to write to the file output.txt:
Type "help", "copyright", "credits" or "license" for more information.
>>> outFile = open('output.txt', 'wt')
>>> outFile.write("Hello, World!\nPython is fun!")
28
>>> outFile.close( )
>>>
The \n escape sequence is used as a carriage return to go to a new line.
The number 28 is the number of characters used in the outFile.write( ) method, including the \n escape sequence. These numbers only appear when using the Python interpreter shell. They do not appear when using a Python program. They are used by Python to keep track of where the Python character pointer is in the file. The Python character pointer is like an invisible cursor which moves along through a file as it is processed by Python.
outFile.close( ) method is used to close the output.txt file when we have finished writing to the output.txt file.
C. PYTHON FILE INPUT OBJECTS
After the Python interpreter shell >>> prompt the following syntax is used to create an instance of a class, called a "file object", which we will name inFile:
Continuing with our original example:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> outFile = open('output.txt', 'wt')
>>> outFile.write("Hello, World!\nPython is fun!")
28
>>> outFile.close( )
>>> inFile = open('output.txt', 'rt')
>>>
The "file object name" is inFile; which is assigned the "file name" by the method open( ).
The "file type" is txt; and there has to be a period between the "file name" and the "file type."
The "modes" are rt; the r is read mode, and the t is reading in text mode.
If the file output.txt already exists it is opened; and if output.txt does not exist Python generates an error message.
D. READING FROM A FILE
Continuing with our original example:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> outFile = open('output.txt', 'wt')
>>> outFile.write("Hello, World!\nPython is fun!")
28
>>> outFile.close( )
>>> inFile = open('output.txt', 'rt')
>>> inFile.read( )
'Hello, World!\nPython is fun!'
>>>
As shown above the method inFile.read( ) is used to read the file output.txt. However, it is not printed in a user friendly format.
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> outFile = open('output.txt', 'wt')
>>> outFile.write("Hello, World!\nPython is fun!")
28
>>> outFile.close( )
>>> inFile = open('output.txt', 'rt')
>>> inFile.read( )
'Hello, World!\nPython is fun!'
>>> inFile.close( )
>>> inFile = open('output.txt', 'rt')
>>> text = inFile.read( )
>>> print(text)
Hello, World!
Python is fun!
>>> inFile.close( )
>>> quit( )
As shown above, the file object inFile was closed and reopened, this was done to move the Python invisible cursor back to the beginning of the file. This had to be done because inFile.read( ) method had moved the cursor to the end of the file. Next, text was assigned inFile.read( ) and the method print(text) was used to print the file in a more user friendly fashion. The inFile.close( ) method was used to close the output.txt file. And, last of all, the quit( ) method was "called" to exit from the Python interpreter shell.
II. WRITING YOUR FIRST PYTHON PROGRAM
A Python program is just a text file that you edit directly. You can use any text editor with an understanding of code and endentation; e.g., Notepad++, to create a Python program, also called a module or script. For example, open your favorite text editor and then copy the following example and paste it into your text editor:
# My first Python program!
outFile = open('output.txt', 'wt')
outFile.write("Hello, World!\nPython is fun!")
outFile.close( )
inFile = open('output.txt', 'rt')
text = inFile.read( )
print(text)
inFile.close( )
From your text editor, do a "File Save As" and save the above Python program example, using the file name fun1.py to the same path which your computer used to download Python. For example, using my text editor I saved my fun1.py file to my Python32 folder using the following path:
C:\Python32\fun1.py
Congratulations, you have just written your first Python program!
III. READING YOUR FIRST PYTHON PROGRAM
A Python program is a Python file containing Python definitions and statements. The Python program we created above contains the Python method print( ) which will cause Python to display printed output onto your computer screen. The "file name" is the Python program name with the suffix .py appended; for example:
fun1.py
From your desktop "double left mouse click" the Python icon and the Python interpreter shell window will open. From the Python interpreter shell window >>> Python prompt, type import, followed by the file name fun1, and then press Enter and the fun1 module will be executed during import, as shown below:
>>> import fun1
Hello, World!Python is fun!
>>> quit( )
Comments are like notes you embed in your program to help you remember why you coded the program the way you did. As shown above, Python does not print your comments. A comment in Python starts with a pound sign, #, also called a hash character; and the comment extends to the end of the line.
To exit the Python shell, call the quit( ) method by typing:
quit( )
and then pressing Enter.
quit( )
and then pressing Enter.
In this tutorial, you have learned how to save information to a file and read information from a file.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"