Files

Data is saved in a file, which is usually stored on a computer’s disk. Once the data is saved in a file, it will remain there after the program stops running. Data that is stored in a file can be retrieved and used at a later time. Programs that are used in daily business operations rely extensively on files.

Programmers usually refer to the process of saving data in a file as “writing data to” the file. The term output file is used to describe a file that data is written to. The process of retrieving data from a file is known as “reading data from” the file. The term input file is used to describe

a file that data is read from.

There are always three steps that must be taken when a file is used by a program.

1. Open the file: Opening a file creates a connection between the file and the program. Opening an output file usually creates the file on the disk and allows the program to write data to it. Opening an input file allows the program to read data from the file.

2. Process the file: in this step data is either written to the file (if it is an output file) or read from the file (if it is an input file).

3. Close the file: when the program is finished using the file, the file must be closed. Closing a file disconnects the file from the program.

In general, there are two types of files: text and binary.

A text file contains data that has been encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of characters. The file may be opened and viewed in a text editor such as Notepad.

A binary file contains data that has not been converted to text. The data that is stored in a binary file is intended only for a program to read.

Most programming languages provide two different ways to access data stored in a file:

sequential access and direct access.

In order for a program to work with a file on the computer’s disk, the program must create a file object in memory. A file object is an object that is associated with a specific file and provides a way for the program to work with that file. In the program, a variable references the file object. This variable is used to carry out any operations that are performed on the file.

Opening a File

The open function creates a file object and associates it with a file on the disk. Here is the general format of how the open function is used:

file_variable = open(filename, mode)

In the general format:

• file_variable is the name of the variable that will reference the file object.

• filename is a string specifying the name of the file.

• mode is a string specifying the mode (reading, writing, etc.) in which the file will be

opened.

File modes used are:

When you pass a file name that does not contain a path as an argument to the open function, the Python interpreter assumes that the file’s location is the same as that of the program.

If to open a file in a different location, specify a path as well as a filename in the argument that is passed to the open function. If you specify a path in a string literal (particularly on a Windows computer), be sure to prefix the string with the letter r.

Here is an example:

test_file = open(r 'C:\Users\Ashraf\temp\test.txt', 'w')

The r prefix specifies that the string is a raw string. This causes the Python interpreter to read the backslash characters as literal backslashes. Without the r prefix, the interpreter would assume that the backslash characters were part of escape sequences, and an error would occur.

Writing Data to a File

Once you have opened a file, you use the file object’s methods to perform operations on the file. File objects have a method named write that can be used to write data to a file. Here is the general format of how you call the write method:

file_variable.write(string)

In the format, file_variable is a variable that references a file object, and string is a string that will be written to the file. The file must be opened for writing (using the 'w' or 'a' mode) or an error will occur.

Once a program is finished working with a file, it should close the file. Closing a file disconnects the program from the file. The process of closing an output file forces any unsaved data that remains in the buffer to be written to the file.

In Python you use the file object’s close method to close a file. For example, the following statement closes the file that is associated with customer_file:

customer_file.close()