Lissane Eddine Ibn Al-Khatib High School
CPGE Center - LAAYOUNE -
File Management
in PYTHON
Professor: ARROU ABDESSELAM
Data files
Concept of file
So far all the programs we have written
were working with data that was entered from the keyboard
and stored in main memory (RAM).
However, one may wish to retain data for longer.
late. This notion of data retention is realized under
the file format.
The data files
Definition of a file:
A file is a set of information recorded on a medium.
hardware (floppy disk, hard drive, etc.).
We distinguish two types of files:
.Text files:
The information is in a text format that is readable by anyone
which text editor.
.Binary files:
The information is only readable by the program that designed it.
Text file
Text file
File operations:
Operations to be applied on a file:
. Creation
. Consultation (Lecture only)
. Modification (Lecture/Writing)
. Closure
Text file
Opening a file
Access to files is ensured through an object.
file that is created using the open() function.
This function is defined as follows by:
file1=open('file_name','open_mode')
filename: represents the name of the file to be opened.
#mode_ouverture:représente le type d'ouverure(r,w,a)
Text file
File opening mode:
Mode Effect
'r' Opening a text file in read-only mode
Opening a text file for writing. If the file
w already exists, it will be overwritten. otherwise it will be created
a Append data, that is, open for writing at the end of
file. the file is created if it does not exist
Text file
The closure of a file
When the operations on a file are completed, it must be
close it by calling the method close().
the opening of the file named file1
file1=open('file_name1','w')
the closing of the file named file1
[Link]()
Text file
Writing to a file
To write to a file, we use the write() method.
Example:
the opening of the file with the object file1
file1=open('filename1','w')
Add the string 'Python language' in the file
[Link]('Python Language')
the closing of the file named file1
[Link]()
Text file
Writing to a file:
Note:
.L'argument de la méthodewrite()doit être une chaîne de
characters.
.To write numbers with the method write() don't forget
so no need for type casting if necessary with the function
str(type).Example:[Link](str(12.5))
Text file
Writing to a file
The writelines(List) method allows you to write the contents of a list.
in a file.
Example:
the opening of the file with the object file1
file1=open('filename1','w')
Add the list=['language', 'Python'] to the file
[Link](['Language','Python'])
the closing of the file with object file1
[Link]()
Text file
Writing to the end of a file
To write at the end of a file, we use the functionopen()in mode
append.
Example:
the opening of the file with the object file1
file1=open('file_name1','a')
Add "New line added with append"
[Link]('\nnew line added with append')
the closing of the file with the object file1
[Link]()
Text file
Sequential reading in a file:
There are different ways to read the stored data in
a file
. Read the entire file
. Read the file line by line
. Read the file character by character
Text file
Sequential reading in a file:
Read the entire file:
The method read(): Allows reading the entire contents of a file and
return a string.
Example:
Text file
Sequential reading in a file:
Read the file line by line:
The method readline(): Allows reading one line at a time and
return a string from the position
currents, if the end is reached it returns an empty string.
Text file
Sequential reading in a file:
Read the file line by line:
Example:
Text file
Sequential reading in a file:
Read the file line by line:
The method readline() transfers all the lines from the file into a
list of string type.
Example:
Text file
Sequential reading in a file:
Read the file character by character:
The method read(n): Allows reading characters and returns
an empty string "" if the end of the file is reached.
Example:
Text file
Sequential reading in a file:
Read the file character by character:
Example:
Text file
Sequential read loop in a file:
File objects are iterable objects, that is to say
that we can extract the elements one by one using the loop
for the while loop.
Exemple1:
Text file
Sequential reading loop in a file:
Example 2 with the readline() method:
Text file
Sequential reading loop in a file:
Example 3 with the readlines() method:
Exercise
•Exercise 01:
1-Write a python program that allows reading the last name, the first name, the
class and the DS score of five students and save them in a text file
named [Link] in the current directory.
2-Write a Python program that allows you to display the content of a file
[Link] according to the form above.
Binary file
Binary file
Definition:
We call 'binary' any file that is not interpretable.
in the form of text: an image, a sound, or even a file
compressed,...
Binary file
Data serialization with the pickle module:
The Pickle module is extremely useful for saving
in a file of data structures like lists (type
list) or dictionaries (typedict).
Binary file
Data Serialization with the pickle module:
Example:
Directed Work
Practical Work