File Systems and File Handling in Python

File Systems and File Handling in Python

There are two types of files

i) Text Files
ii) Binary Files

Text Files

A text file is a structure containing sequence of lines and a line containing sequence of characters.A line is terminated by EOL (End OF Line) character.This type of file can hold alphabets, digits and symbols.
Commonly used extensions : database(csv,..),source files(c,py,cpp,..), etc

Binary files

Binary files can be any sequence of bytes and must be opened in an appropriate program that knows the specific file format.
Commonly used extensions : images(jpg,gif,..), database(mdb,sqlite,..), executable(exe,dll,..) etc

Opening and Closing Files

For opening and closing of file open() and close() are used respectively.

The open function

Before reading in or writing a file it must be opened and to open a file in python it uses this built-in function.
This function is used to create a file object .
This object can be used further to call other methods associated with files.
Syntax:

file_object=open(file_name,access_mode,buffering)

Here :-
file_name: This argument contains a string value, that will be the name of file which is to be accessed.
access_mode: This argument determines the mode in which file needs to be opened ie. read,write,append etc.By default file access mode is read.
buffering: If the buffering value is set to 0,no buffering
will take place.If the buffering value is 1,line buffering will take place.If the buffering value is set any integer greater than 1,then buffering will take place with given buffer size.If the buffering value is negative,the buffer size will be systems default behavior.
fileobject: This will have any variable name that can be used to access other methods of file.
open: This is built_in method to open file.

Example:

"""
Author : ITVoyagers (https://itvoyagers.in/)


Date : 24th November 2018

Description : Program to open a text file in write mode
"""

f1=open("itvoyagers.txt","w")

Where f1 is file object, open() is built-in python method to open a file,”itvoagers.txt” is name of text file,”w” indicated write access mode.

Note: file_object_name,file_name can be any variable name ,where as open is compulsory method and access mode can be given only from list of access modes.

Other way to open a file:
Syntax:

with open("file_name","accessmode") as fileobject:

Here:
with: This is built-in keyword provided by python.
open: This is built_in method to open file.
file_name: This argument contains a string value, that will be the name of file which is to be accessed.
access_mode: This argument determines the mode in which file needs to be opened ie. read,write,append etc.By default file access mode is read.
as: This is built-in keyword provided by python to create object name.
fileobject: This will have any variable name that can be used to access other methods of file.

Example:

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date : 24th November 2018

Description : Program to open a text file in append mode using with.....as
"""

with open("itvoyagers.txt","a") as f1

Where f1 is file object, open() is built-in python method to open a file,”itvoagers.txt” is name of text file,”a” indicated append access mode.
Note: file_object_name,file_name can be any variable name ,where as open is compulsory method and access mode can be given only from list of access modes.

The close function

This function is used to close the created file object and to clear out any information.The file is closed automatically in python without using close() but it is suggested to use close() to close a file as a good programming practice.
Once file is closed no further writing or appending can be done
Syntax:

fileobject.close()

Here:
fileobject: This can be name of object created.
close(): This is buily-in method to flush out unwritten data and close file object.

Example:

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date : 24th November 2018

Description : Program to close a text file (w.r.t above example)


f1.close()

Where f1 is name of file object created it can be any name,close() built-in method of python.

You can also check other file related operations and modes

Leave a Comment