Python File Handling

Current page Page 1

Page 1    Page 2    Page 3

What is a file?

A file is a document or the data stored on a permanent storage device which can be read, written or rewritten according to requirement.

In other words, data is packed up on the storage device as data structures called files. All the files are assigned a name that is used for identification purpose by the operating system and the user.

File input output (I/O) means transfer of data from secondary memory (hard disk) to main memory or vice versa. When we are working on a computer system, the file or the document stored on the hard disk is brought into RAM (Random access memory) for execution and vice versa.

Or we can say:

File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk).

Since, random access memory (RAM) is volatile which loses its data when computer is turned off, we use files for future use of the data.

When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed, so that resources that are tied with the file are freed.

Data File Operations

Before we start working with a file, first we need to open it. After performing the desired operations, it needs to be closed so that the resources that are tied with the files are freed or released.

Hence, in Python, a file operation takes place in the following order.

  1. Open a file
  2. Performing operations/ processing data (Read or write etc. )
  3. Close the file

File types:
Before we discuss the file operations , we should be aware of the file types. Python allows us to create and manage two types of files.
• Text files
• Binary Files

 

Text files


A text file consists of a sequence of lines. A line is a sequence of characters (ASCII or UNICODE), stored on permanent storage media. Although the default character coding in python is ASCII. Using the constant “u” with string, it supports Unicode as well.
In text file, each line of text is terminated with a special character known as EOL (end of line) character. In text files some internal translation take place when this EOL character is read or written. By default, this EOL character is the newline character(‘\n’). So at the lowest level, text file will be a collection of bytes. Text files are stored in human readable form and can be created using any text editor.
We use text files to store character data. For example test.txt, abc.txt etc.

Binary File


A binary file is just a file that contains information in the same format in which the information is held in the memory. In binary file, there is no delimiter for a line. Also no translation occurs in binary file. As a result the binary files are faster and easier for a program to read and write than are the text files. As long as the files doesn’t need to be read by people or need to be ported to a different type system, binary files are the best way to store the program.

Operations on file

Opening a file

Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly.
Syntax:
File_variable/file_handle=open(“filename”,”mode”)

Here:
File_variable/file_handle:
If the opening of file is successful, the operating system returns us a file handle. Handle is not the actual data stored in the file , instead it is a handle that we can use to read data from the file.
While opening a file a file_handle is associated with the file and then reading or writing operation on the file is carried out using this file_handle. (file name is not used)

Filename:

name of the file storing the data

Mode:

mode helps to specify the purpose for which the file is opened. Mode can be :
Read(r): to read the file
Write(w): to write contents in the file
Append(a): to add data into an existing file after the existing contents.

example:

f = open(“test.txt”)    # open file in current directory

f = open(“C:/Python33/abc.txt”)  # specifying full path

We can specify the mode while opening a file. In mode, we specify whether we want to read ‘r’, write ‘w’ or append ‘a’ to the file. We also specify if we want to open the file in text mode or binary mode.

The default is reading in text mode. In this mode, we get strings when reading from the file.

On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.

Current page Page 1

Page 1    Page 2    Page 3