





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Study Notes on Files Modules and Packages
Typology: Study notes
1 / 9
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