LESSON 01 –
FILE I/O

 

 

LESSON NOTE/GUIDE

LINKS TO INFORMATION ON FILE I/O:

 

            https://www.tutorialspoint.com/python/python_files_io.htm

            https://www.javatpoint.com/python-files-io

 

PROGRAM 1 – READING FROM A FILE

A few setup steps are required:

·       Create a file that will hold some text so that we can read from it.

o   Right click on the project name > New > File

o   Name it data.txt

o   Type in a few sentences (see image below)


·       Create a new Python File and name it Program01.py.

o   Check that this file appears near the data.txt file in your project explorer on the left hand side.

 

THE CODE

 

# Open a file (error if file does not exist) 
my_file = open("data.txt", "r")  #r=read
 
# Read five first characters 
five_characters = my_file.read(5)
print(five_characters)
 
# Read line (reads rest of line) 
line = my_file.readline()
print(line)
 
#Close the file 
my_file.close()

EXPLANATION

 

In the first line of code, we open the file data.txt for reading only.  We will access the file via the my_file object.  Note that if the file you specified doesn’t exist, you will get an error on this line.

 

In the second line of code, we read the first five characters in the file and store that in the string variable five_characters.  On the next line, we simply output that text to screen.

 

In the fourth line of code, we read the remaining characters of the line that the cursor is currently at and store that string in line.  We then output that to the screen.

In the last line of code, we close the file.  It is good practice to do this when we are done using the file.  It also makes sure that everything is properly updated.

 

Note:  Using the read() function with no parameter would lead to reading the entire file into a string.

 

PROGRAM 2 – READING ENTIRE FILE ONE LINE AT A TIME

 

·        This program reads the data of a file one line at a time.

THE CODE

 

my_file = open("data.txt", "r")      #r=read
 
while True:                          #infinite loop
    line = my_file.readline()        #read next line
    if line=="":                     #if no line, break
        break
    print(">", line)
 
my_file.close()

EXPLANATION


The first line opens the file for reading. 

 

The next line creates an infinite while loop.  Inside the loop, the first line of code reads a line from the text file and stores it in the string line. 

 

If that line is the empty string, that means we have reached the end of the file so we break out of the loop.

If we didn’t break out of the loop, then we print the line out to screen.

 

 

PROGRAM 3 – READING ENTIRE FILE ONE CHARACTER AT A TIME

 

  • This program reads a file one character at a time.

 

THE CODE

 

my_file = open("data.txt", "r")
 
while True:
    chr = my_file.read(1)        #read one character
    if chr=="":
        break
    print(">", chr)
 
my_file.close()

EXPLANATION


This code is the same as in the previous program except the line where we read from file.  Using my_file.read(1) reads only a single character from the file.

 

Also, instead of naming the string that holds the data line, we called that same string chr here as it holds a single character.

Note: Notice that this program gives a strange result where the end of the line in the file is located.  That is because there is actually an invisible end-of-line character that is in the text file.  When you display that character, it simply brings the cursor to the next line.