





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






ļ· Binary files
ļ· Text files
File operation and file functions
A file is one, which will enable the user to read, write and store a group of related data, without losing them even if the program is over. To perform these functions there are several basic file operations as,
ļ· Naming a file - It is conventional, and convenient to name files in relation to the data stored.
ļ· Opening a file
ļ· Reading data from the file
ļ· Writing data to the file and
ļ· Closing a file
open()
Before going to do any file operation, the concerned file should be opened.
Syntax:
file_object=open(āfile_nameā,āfile_modeā)
The open() function creates a file object. Finally it should be given, as to what purpose the file is being used. It is also called the file mode.
file_mode Description
R Opens a file for reading purpose and this is the default file opening mode/
W Opens a file for writing purpose
A Opens a file for appending data to it.
r+ Existing file is opened to the beginning for both reading and writing
w+ Opens a file for reading and writing purpose. The file pointer is positioned at the beginning of the file.
a+ Opens a file for reading and writing purpose. The file pointer is positioned at the end of the file
Rb Opens a file for reading in binary format and this is the default file opening mode.
Ab Opens a file for appending data in binary format
Wb Opens a file for writing in binary format
rb+ Opens a file for reading and writing purpose in a binary format and the file pointer is positioned at the beginning of the file
wb+ Opens a file for reading and writing purpose in a binary format. If the file already exists it overwrites the file. If the file does not exist it will create a new one for both reading and writing.
ab+ Opens a file for appending and reading data in binary format. Here the file pointer is positioned at end if the file already exists. It will create a new one if the file does not exists.
For example, a simple open() function call is as follows.
out=open(āabc.datā,āwā)
close()
This function closes a file that was opened by a call open().
The general form of the function call to close() is close(file-object) (e.g.) p1=open(āabc.datā,āwā); p2=open(ādef.datā,ārā); āāā āāā close(p1); close(p2);
These two functions are considered as most basic of all input/output functions in file handling. The write () function writes characters to a disk file that was previously opened for writing,through the use of the function open() .Similarly read () function is used to read characters from a file opened in read mode by open() .The general format for read() method is:
file_object.read(no_of_bytes_to_be_read)
n=int(input(āenter a valueā))
expert:
print(āyou didnāt enter the integer inputā)
else:
print(āvalue entered correctly and storedā)
Python uses C-style string formatting to create new strings. The "%" operator is used
to format a set of variables enclosed in a "tuple" along with a format string. The format string contains text with argument specifier symbols like "%f" and "%d".
The use os module in Python
The tasks performed by this module are
ļ· To find the name of the current working directory ļ· To change the current directory ļ· To create an new directory ļ· To delete a file ļ· To delete a directory ļ· To check the file present in the current directory or not
Modules
In Python module is a file that contains definitions of functions, variables and classes. The module name
is the same as the file name. We have used some scientific functions that present in math module and it
is a built in Python module. The main advantage of using module is it allows us to make our programs
more robust and powerful. We can have our own module and in our example program we created a
module by name āmyfunctions.pyā. The module contains two functions definition namely fact() and
maximum(). The coding is as follows.
def fact(no):
f=
for i in range(1,no+1):
f=f*i
return (f)
def maximum(arr):
max=arr[0]
for i in range(1,len(arr)):
if (max <arr[i]):
max=arr[i]
return max
# Module test example - moduletest.py
import myfunctions
no=int(input("Enter number to find factorial :"))
print ("Factorial of a given number is %d"%myfunctions.fact(5))
a=[8,10,30,15,20]
print("The maximum number is %d"%myfunctions.maximum(a))
Output
Enter number to find factorial :
Factorial of a given number is 120
The maximum number is 30