Python : Programs that write to files and read from files

Examples:

data files : data_file_1.txt,        data_file_2.txt,        data_file_3.txt

Open and read a file: file_input.py

Create a new file and write data to the file: create_new_output_file.py

Open an existing file and write data to the bottom of the file : output_to_existing_file.py

Enter data on a keyboard and add it to an existing file : add_user_input_to_file.py

Use a loop to display file contents and add user input data : loop_user_input_to_file.py

File input/output Lesson

Up until now we have worked with programs that use data that was either part of the program or entered on the keyboard:

Example of data that is part of the program:

myInteger = 22;
count = 1;
count = count + 1;

Example of getting data entered on the keyboard:

print ("enter a number between 3 and 10 : ", end= "");
numStr = sys.stdin.readline();
num = numStr.rstrip();
num2 = num * 5;
print("The number you entered is ", num," and the result of multiplying that number by 5 is ",num2);

We have used the "print" statement to display data on the screen.

Data input & output:

Getting data into a program is called "data input". Getting data out of a program is called "data ouput".    

So far the only "data input" we have used is keyboard input. The only "data output" we have used is output to the screen.

In this lesson you can learn how to use files for data input and output. Our programs will write data to a file and read data from a file

File input and output:

In learning how to code for file output and input you will learn how to:

Create a file that can be read by a program (use a text editor such as Notepad or Notepad++ )

Create programs to :

open a file, read data from it, and then close the file.

open a file, write data at the bottom of the file, and then close the file

create a new file, write data into it and then close the file.

get user input from a keyboard and add it to the end of a file.

use a loop to :

  • read the contents of the file and display it on the screen.
  • have the user enter new data and then write that data on the end of the file
  • give the use a way of ending the loop and exiting the program

 

 

Opening Files - Different "modes"

A typical statement to open a file looks like this:

data_file = open("data_file_1.txt", "r+");

Notice the "r+" part of the statement. This defines the "mode" in which the file is opened. The mode determines the following thing about how the Python program works with this file:

What to do if the file does not exist:

  • create a new one (delete the current contents, if any),
  • return an error

Whether the program can read from the file, write to the file, or both

If the program is allowed to write to the file, will it write to the first line in the file or append new lines to the bottom of the file

The choices are:

"r" Open text file for reading, starting with the first line in the file

"r+" Open for reading and writing. Both reading and writing start with the
first line in the file

"w" Truncate file to zero length or create text file for writing.

"w+" Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. Both reading and writing start
with the first line in the file

"a" Open for writing. The file is created if it does not exist. Writing
will start at the end of the file. "a" stands for "append"

There are ways to modify some modes. See the "fseek()" function to change which line
the next read or write statement will impact

Program often close a file and re-open it using a different mode.