06.0 Python‎ > ‎Python Notes‎ > ‎

File Processing

File Types: two major types of files that are stored on a computer
open: prepare a file for processing
close: close a file after processing
with: a code block to help with processing a file

File Types

Text File

  • Contains lines of characters. 
  • Each line ends with of EOL (End Of Line) character. 
  • In Python, a common EOL character is \n.
  • A text file can be read by a program like notepad.

Binary File

  • All files that are not text files are binary files.
  • To process a binary file, the file format must be known by the application.
  • Examples of binary files include: pdf files, png files, doc files, exe file


NOTE:  Unless otherwise directed, the file location is in the same directory as the program being executed.

To process a file (read or write to a file), the file must first be opened. When opening a file a minimum of one parameter must be supplied. The name of the file, if  the mode parameter is not supplied, the file will be opened in read only mode.

file name:  this is the actual name of the file on the disk, including its file extension. 
Example:  assignmment.txt

mode:  the mode is how you are going to process the file. 
Here are 5 modes:
'r' : to read a file (the default mode)
'w' : to write to a file, the contents of the file are deleted before writing to the file
'x' :  open for exclusive writing. If the file already exists, the open() statement will fail.
'a' : to append to a file, append will add data to the end of a file, keeping all of the original data
'+' : read and write the the same file 
'r+' : same as '+'

CAUTION: binary files can be corrupted in a Windows environment, if the file is not opened as a binary file
For binary files:
'b' : used to append binary files (needed in a Windows environment)
'rb', wb', 'r+b' are also used with binary files in a Window environment.

For more detail, see python.org documentation file.

open() Syntax

file_object = open(filename, mode)

file_object: the name that will be used in the program to process the file.
filename: a string that contains the name of the actual file on the disk
mode: a string that will be a 'r', 'w', 'a', 'b' or 'x'

open() Example

This will open the myData.txt file as f_input for reading:

    f_input = open('myData.txt', 'r')

This will open the newData.txt file as f_output for writing:
    f_output = open('newData.txt','w')

This will open the moreData.txt file as f_append for reading:
    f_append = open('moreData.txt', 'a')


When you open a file, it must also be closed. Closing a file after you are finished with it is the same for reading, writing or appending.

close() Syntax


file_object: the name that will be used in the program to process the file.

close() Example

This will close the f_data file object


with is a multi-line statement. All statements that are indented after the with statement are part of the with statement. When opening a file using a with statement, the file is automatically closed when the block of code is complete. Therefore, there is no need to close the file after processing is complete. An error is not issued if the file is closed.

with : Example

This will open the myData.txt file as f_in for reading:
    with open('myData.txt', 'r') as f_in:

This will open the newData.txt file as f_out for writing:     with open('newData.txt','w')as f_out:

This will open the myData.txt file as f_in for reading and  the newData.txt file as f_out for writing all within the same block of code:
    with open('myData.txt', 'r') as f_in, open('newData.txt','w')as f_out: