Python File Management

Introduction to Python Programming File Management

This article describes following important Python File Management concepts.

  • How to Read data from a text file?
  • How to Write data to a text file?
  • How to Append data to a text file?

Introduction to File Management

Real life software projects maintains large amount of data. For Examples: Records related to Students, Exams, Employees, Products, Places, Temperature, etc. We can manage data using file management concepts.

  • File – is a location on disc where data is stored.

Python programming provides many functions to manage such data files.


File Management Operations

Most commonly used file management operations under Python projects are as under:

  • Read
  • Write
  • Append


Modes of File Management

Depending on our requirements, we use various file management modes:

  • r - opens a file for reading
  • w - opens or create a text file for writing
  • a - opens a text file for appending content
  • x - creates the specified file, returns an error if the file exists

In addition you can specify binary or text mode of the file depending on your requirements:

  • t - Text mode (default value)
  • b - Binary mode (for images)


Python File Management – Basic Syntax

To open a student.txt file for reading purpose, use following statement:

f = open("student.txt")

Above code is the same as:

f = open("student.txt", "rt")

Because "r" for read, and "t" for text are the default values.


Useful Python File Management Functions

Some of the very useful Python File Management related functions are given below:

To open the file, use the built-in open() function.

The open() function returns a file object, which has a read() method for reading the content of the given file:

Example

f = open("student.txt", "r")

print(f.read())

If the student.txt file is located in a different location (other than your source file location), you will have to specify the file path as shown below:

Example: Open a data file from a different location:

f = open("D:\\myfolder\student.txt", "r")

print(f.read())


Read Specified Characters from a File

By default the read() method returns the whole text, but you can also specify how many characters you want to read:

Example: To read 10 first characters of the file use following code:

f = open("student.txt", "r")

print(f.read(10))

 

Read Specified No. of Lines

You can read one line from a data file using the readline() method:

Example: To read one line from a file use following code:

f = open("student.txt", "r")

print(f.readline())


Example: To read three lines of data, use readline() three times as shown below: 

f = open("student.txt", "r")

print(f.readline())

print(f.readline())

print(f.readline())

 

Likewise, you can use a looping concept to read number of lines as per your program requirements. You can read the whole file, line by line:

Example: 

f = open("student.txt", "r")

for line in f:

  print(line)

 

Close Files

It is a good practice to always close the file when you are done with it. Close the file when you are finish with your file processing using following code:

Example:

f = open("student.txt", "r")

print(f.readline())

f.close()


Other Important Python File Management Programs 


< Home >

No comments:

Post a Comment