Nearby lessons
76 of 108Python - File Handling
Introduction to File Handling
As part of programming requirements, we have to store our data permanently for future use.
For this requirement, we should use files.
Files are very common permanent storage areas used to store data.
Why Do We Need File Handling?
Normally, the data stored in variables exists only while the program is running.
When the program terminates, that data is lost.
To store data permanently so that it can be used again in the future, Python provides File Handling.
Using files, we can save information permanently and access it whenever required.
Types of Files
There are two types of files.
- Text Files
- Binary Files
1. Text Files
Usually, we use text files to store character data.
Example
main.py
No output captured.
Explanation
A text file stores data in the form of characters.
Examples include:
- Notes
- Logs
- Source code
- Configuration files
2. Binary Files
Usually, we use binary files to store binary data such as:
- Images
- Video files
- Audio files
Explanation
Binary files store data in binary format instead of plain characters.
These files are commonly used for multimedia and other non-text data.
Comparison Between Text Files and Binary Files
| Text Files | Binary Files |
|---|---|
| Stores character data | Stores binary data |
| Human readable | Not directly human readable |
| Example: abc.txt | Examples: Images, Videos, Audio files |
Opening a File
Before performing any operation such as:
- Reading
- Writing
we must first open the file.
For this purpose, Python provides the built-in open() function.
At the time of opening the file, we must specify the mode, which represents the purpose of opening the file.
Syntax of open()
main.py
No output captured.
Parameters
- filename → Name of the file.
- mode → Specifies how the file should be opened.
File Modes in Python
Python supports the following file modes.
1. Read Mode (r)
Opens an existing file for reading.
Features
- File pointer is placed at the beginning of the file.
- If the file does not exist, Python raises:
Exception
main.py
No output captured.
Important Point
- This is the default mode.
2. Write Mode (w)
Opens a file for writing.
Features
- If the file already contains data, the existing data is overwritten.
- If the file does not exist, a new file is created.
3. Append Mode (a)
Opens an existing file for appending data.
Features
- Existing data is not overwritten.
- New data is added at the end of the file.
- If the file does not exist, Python creates a new file.
4. Read and Write Mode (r+)
Used to both read and write data.
Features
- Existing data is not deleted.
- File pointer is positioned at the beginning of the file.
5. Write and Read Mode (w+)
Used for writing as well as reading.
Features
- Existing data is overwritten.
6. Append and Read Mode (a+)
Used to append and read data.
Features
- Existing data is not overwritten.
- New data is appended to the file.
7. Exclusive Creation Mode (x)
Used to create a new file for writing.
Features
- If the file already exists, Python raises:
Exception
main.py
No output captured.
Binary File Modes
All the above modes are applicable to text files.
If we suffix the mode with b, then it represents a binary file.
Binary Modes
main.py
No output captured.
Example
main.py
No output captured.
Explanation
The above statement opens the file abc.txt in write mode.
File Mode Summary
| Mode | Description |
|---|---|
| r | Opens an existing file for reading. |
| w | Opens a file for writing and overwrites existing data. |
| a | Opens a file for appending data. |
| r+ | Reads and writes without deleting existing data. |
| w+ | Writes and reads; existing data is overwritten. |
| a+ | Appends and reads without overwriting existing data. |
| x | Creates a new file exclusively for writing. |
Binary File Mode Summary
| Mode | Description |
|---|---|
| rb | Read binary file. |
| wb | Write binary file. |
| ab | Append binary file. |
| r+b | Read and write binary file. |
| w+b | Write and read binary file. |
| a+b | Append and read binary file. |
| xb | Create a new binary file. |
Chapter Summary
| Topic | Description |
|---|---|
| File Handling | Used to store data permanently. |
| Text File | Stores character data. |
| Binary File | Stores images, videos, audio, etc. |
| open() | Opens a file. |
| File Mode | Specifies how the file is opened. |
| Binary Mode | Text mode with b suffix. |
Important Notes
- Files are used to store data permanently.
- There are two types of files: Text Files and Binary Files.
- Before reading or writing, a file must be opened.
- The
open()function is used to open a file. - The file mode specifies the purpose of opening the file.
- The default mode is
r. - The
wmode overwrites existing data. - The
amode appends new data without deleting existing data. - The
xmode creates a new file and raisesFileExistsErrorif the file already exists. - Adding
bto a file mode makes it suitable for binary files.
Closing a File
After completing all operations on a file, it is highly recommended to close the file.
For this purpose, we use the close() function.
Syntax
main.py
No output captured.
Explanation
The close() function closes the opened file after all file operations are completed.
Closing a file releases the resources associated with that file.
Various Properties of a File Object
Once a file is opened and we get a file object, we can access several properties related to that file.
The following properties are available:
| Property / Method | Description |
|---|---|
name |
Returns the name of the opened file. |
mode |
Returns the mode in which the file is opened. |
closed |
Returns True if the file is closed; otherwise False. |
readable() |
Returns True if the file is readable. |
writable() |
Returns True if the file is writable. |
Program: Display File Properties
The following program demonstrates how to access different properties of a file object.
Example
main.py
No output captured.
Output
Understanding the Program
Let's understand each statement used in the program.
Opening the File
main.py
No output captured.
Explanation
Opens the file abc.txt in write mode.
The returned file object is stored in the variable f.
File Name
main.py
No output captured.
Explanation
Returns the name of the opened file.
Output
File Mode
main.py
No output captured.
Explanation
Returns the mode in which the file is opened.
Output
Check Whether the File is Readable
main.py
No output captured.
Explanation
Since the file is opened in write mode, it is not readable.
Output
Check Whether the File is Writable
main.py
No output captured.
Explanation
Since the file is opened in write mode, it is writable.
Output
Check Whether the File is Closed
main.py
No output captured.
Explanation
Before calling close(), the file is still open.
Output
Closing the File
main.py
No output captured.
Explanation
Closes the file after completing all file operations.
Check Again
main.py
No output captured.
Explanation
After calling close(), the file is closed.
Output
Execution Flow
main.py
No output captured.
Summary of File Object Properties
| Property | Returns |
|---|---|
f.name |
Name of the file. |
f.mode |
File opening mode. |
f.closed |
True if closed; otherwise False. |
f.readable() |
Whether the file can be read. |
f.writable() |
Whether the file can be written. |
Chapter Summary
| Topic | Description |
|---|---|
close() |
Closes the opened file. |
name |
Returns the file name. |
mode |
Returns the file mode. |
closed |
Indicates whether the file is closed. |
readable() |
Checks if the file is readable. |
writable() |
Checks if the file is writable. |
Important Notes
- After completing file operations, it is highly recommended to close the file.
- The
close()function is used to close an opened file. namereturns the name of the opened file.modereturns the mode in which the file is opened.closedreturnsFalsewhile the file is open andTrueafter it is closed.readable()returns whether the file supports reading.writable()returns whether the file supports writing.
Quick Recap
- Always close a file after completing all file operations.
- The
close()function releases the file resources. - A file object provides useful properties such as
name,mode,closed,readable(), andwritable(). - These properties help us understand the current status of the opened file.
Important Interview Questions
- Why should we close a file after completing file operations?
- What is the purpose of the
close()function? - What does the
nameproperty return? - What does the
modeproperty return? - What is the difference between
readable()andwritable()? - When does the
closedproperty returnTrue? - Can we check whether a file is readable before performing read operations?
- Which property tells us whether a file has already been closed?
Reading Character Data from Text Files
We can read character data from a text file by using the following methods.
read()→ To read total data from the file.read(n)→ To read n characters from the file.readline()→ To read only one line.readlines()→ To read all lines into a list.
Overview of Reading Methods
| Method | Purpose |
|---|---|
read() |
Reads the complete file. |
read(n) |
Reads a specified number of characters. |
readline() |
Reads one line. |
readlines() |
Reads all lines into a list. |
read() Method
The read() method is used to read the entire content of a text file.
Syntax
main.py
No output captured.
Explanation
The read() method returns all the data available in the file.
After reading the complete file, the file pointer moves to the end of the file.
Example 1: Read Complete Data from a File
main.py
No output captured.
Output
Program Explanation
Let's understand each statement in the program.
Open the File
main.py
No output captured.
Explanation
Opens the file abc.txt in read mode.
The file object is stored in the variable f.
Read Complete Data
main.py
No output captured.
Explanation
The read() method reads the entire file content and stores it in the variable data.
Display the Data
main.py
No output captured.
Explanation
The statement displays all the contents of the file.
Close the File
main.py
No output captured.
Explanation
Closes the file after completing the read operation.
read(n) Method
The read(n) method is used to read the first n characters from the file.
Syntax
main.py
No output captured.
Parameter
- n represents the number of characters to be read.
Example 2: Read the First 10 Characters
main.py
No output captured.
Output
Program Explanation
Let's understand how the program works.
Open the File
main.py
No output captured.
Explanation
Opens the file in read mode.
Read 10 Characters
main.py
No output captured.
Explanation
The read(10) method reads only the first 10 characters from the file.
After reading those characters, the file pointer automatically moves to the next unread position.
Display the Data
main.py
No output captured.
Explanation
Displays the first ten characters that were read from the file.
Close the File
main.py
No output captured.
Explanation
Closes the file after completing the operation.
Difference Between read() and read(n)
| Method | Description |
|---|---|
read() |
Reads the entire file. |
read(n) |
Reads only the specified number of characters. |
Execution Flow
main.py
No output captured.
Summary
| Method | Purpose |
|---|---|
read() |
Reads the complete file content. |
read(n) |
Reads only the specified number of characters. |
Important Notes
read()reads the entire content of the file.read(n)reads only the specified number of characters.- The value of
nrepresents the number of characters to read. - After reading data, the file pointer automatically moves forward.
- Always close the file after completing file operations by using the
close()function.
Quick Recap
- Use
read()when you want the complete file content. - Use
read(n)when you need only a specific number of characters. - Both methods start reading from the current file pointer position.
- The file pointer automatically advances after every read operation.
Important Interview Questions
- What is the purpose of the
read()method? - What is the difference between
read()andread(n)? - What does the parameter
nrepresent inread(n)? - What happens to the file pointer after calling
read()? - Can
read(n)read the complete file? - Why should the file be closed after reading?
- Which mode should be used while reading a text file?
- What is stored in the variable returned by
read()?
Reading Lines from Text Files
In addition to reading the complete file or a specific number of characters, Python also provides methods to read data line by line.
The following methods are used for reading lines from a text file:
readline()→ Reads one line at a time.readlines()→ Reads all lines and returns them as a list.
readline() Method
The readline() method is used to read only one line from a text file.
Syntax
main.py
No output captured.
Explanation
Each call to readline() reads the next line from the file.
After reading a line, the file pointer automatically moves to the beginning of the next line.
Example 3: Read Data Line by Line
main.py
No output captured.
Output
Program Explanation
Let's understand how the program reads data line by line.
Read First Line
main.py
No output captured.
Explanation
Reads the first line from the file.
Read Second Line
main.py
No output captured.
Explanation
Reads the second line from the file.
Read Third Line
main.py
No output captured.
Explanation
Reads the third line from the file.
Each call moves the file pointer to the beginning of the next line.
Close the File
main.py
No output captured.
Explanation
Closes the file after completing all read operations.
readlines() Method
The readlines() method reads all lines from the file and returns them as a list.
Syntax
main.py
No output captured.
Example 4: Read All Lines into a List
main.py
No output captured.
Output
Program Explanation
Let's understand each statement used in the program.
Read All Lines
main.py
No output captured.
Explanation
Reads all lines from the file and stores them in the variable lines as a list.
Print Each Line
main.py
No output captured.
Explanation
The for loop prints each line one by one.
The end='' argument prevents printing an extra blank line.
Close the File
main.py
No output captured.
Explanation
Closes the file after reading all lines.
Example 5
main.py
No output captured.
Output
Understanding the Program
This example demonstrates how different reading methods work together while sharing the same file pointer.
Step 1
main.py
No output captured.
Explanation
Reads the first 3 characters from the file.
Output
Step 2
main.py
No output captured.
Explanation
Reads the remaining characters of the current line.
Output
Step 3
main.py
No output captured.
Explanation
Reads the next 4 characters from the current file pointer position.
Output
Step 4
main.py
No output captured.
Explanation
Displays the message:
Output
Step 5
main.py
No output captured.
Explanation
Reads all remaining data from the current file pointer position.
Output
Execution Flow of Example 5
main.py
No output captured.
Difference Between Reading Methods
| Method | Description |
|---|---|
read() |
Reads the complete file. |
read(n) |
Reads the specified number of characters. |
readline() |
Reads one line at a time. |
readlines() |
Reads all lines and returns a list. |
Summary
| Method | Purpose |
|---|---|
read() |
Reads complete file data. |
read(n) |
Reads specified number of characters. |
readline() |
Reads one line. |
readlines() |
Reads all lines into a list. |
Important Notes
readline()reads only one line from the file at a time.- Each call to
readline()moves the file pointer to the next line. readlines()reads all lines from the file and returns them as a list.read(),read(n),readline(), andreadlines()start reading from the current file pointer position.- After each read operation, the file pointer automatically moves forward.
- Always close the file after reading data.
Quick Recap
- Use
readline()when reading one line at a time. - Use
readlines()when all lines are required as a list. - All reading methods use the same file pointer.
- Mixing different reading methods affects subsequent reads because the file pointer keeps moving forward.
Important Interview Questions
- What is the purpose of the
readline()method? - What does the
readlines()method return? - What is the difference between
readline()andreadlines()? - How does the file pointer behave after calling
readline()? - Can
read(),read(n), andreadline()be used together? - Why is
end=''used while printing lines? - Which method returns a list of lines?
- Why should a file be closed after reading?
Writing Data to Text Files
We can write character data to text files by using the following two methods:
write(str)writelines(list of lines)
Overview of Writing Methods
| Method | Purpose |
|---|---|
write(str) |
Writes a single string to a text file. |
writelines(list) |
Writes multiple strings from a list into a text file. |
write() Method
The write() method is used to write character data to a text file.
Syntax
main.py
No output captured.
Parameter
- str represents the string to be written into the file.
Example 1: Writing Data by Using write()
main.py
No output captured.
Output
Contents of abcd.txt
Understanding the Program
Let's understand how the above program writes data into the file.
Step 1: Open the File
main.py
No output captured.
Explanation
Opens the file abcd.txt in write mode.
If the file already exists, its previous contents are overwritten.
If the file does not exist, Python automatically creates a new file.
Step 2: Write the First Line
main.py
No output captured.
Explanation
Writes the first line into the file.
The \n moves the cursor to the next line.
Step 3: Write the Second Line
main.py
No output captured.
Explanation
Writes the second line into the file.
Step 4: Write the Third Line
main.py
No output captured.
Explanation
Writes the third line into the file.
Step 5: Display Success Message
main.py
No output captured.
Explanation
Displays a confirmation message after successfully writing data to the file.
Step 6: Close the File
main.py
No output captured.
Explanation
Closes the file after completing the write operation.
Important Note
In the above program, if we execute the program again, the existing data in the file will be overwritten every time.
Instead of overwriting, if we want to append data, then we should open the file as follows:
Append Mode
main.py
No output captured.
Explanation
Opening a file in a (append) mode preserves the existing contents and adds the new data at the end of the file.
writelines() Method
The writelines() method is used to write multiple lines from a list into a text file.
Syntax
main.py
No output captured.
Example 2: Writing Multiple Lines
main.py
No output captured.
Output
Contents of abcd.txt
Understanding writelines()
Let's understand how the writelines() method works.
Create a List
main.py
No output captured.
Explanation
Creates a list containing four strings.
Notice that the first three strings contain the newline character (\n), while the last string does not.
Write All Lines
main.py
No output captured.
Explanation
Writes all strings from the list into the file.
Each string is written in the same order as it appears in the list.
Difference Between write() and writelines()
| Method | Description |
|---|---|
write(str) |
Writes a single string to the file. |
writelines(list) |
Writes multiple strings from a list. |
Execution Flow
main.py
No output captured.
Summary
| Method | Purpose |
|---|---|
write() |
Writes a single string to the file. |
writelines() |
Writes multiple strings from a list. |
Important Notes
- We can write character data to text files by using
write()andwritelines(). write()writes a single string to the file.writelines()writes multiple strings from a list.- Opening a file in
wmode overwrites the existing data. - To append data instead of overwriting, open the file in
amode. - The
writelines()method writes all strings from the given list into the file in sequence.
Quick Recap
- Use
write()to write a single string. - Use
writelines()to write multiple strings from a list. wmode replaces existing file contents.amode preserves old data and appends new data.- Always close the file after writing.
Important Interview Questions
- What is the purpose of the
write()method? - What is the purpose of the
writelines()method? - What is the difference between
write()andwritelines()? - What happens if a file is opened in
wmode? - Which mode should be used to append data without deleting existing data?
- Can
writelines()write multiple strings at once? - Why is
close()called after writing to a file? - What happens if the file specified in write mode does not already exist?
Importance of the Newline Character (\n)
When writing data by using the write() method, we must provide the line separator \n.
Otherwise, all the data will be written into a single line.
Why is the Newline Character Required?
The newline character (\n) tells Python to move the cursor to the next line after writing the current string.
If the newline character is omitted, the next string is written immediately after the previous string without any line break.
Example: Writing Without Using \n
The following program writes data without using the newline character.
Program
main.py
No output captured.
Contents of abcd.txt
Program Explanation
Let's understand why all the text appears on a single line.
Step 1: Open the File
main.py
No output captured.
Explanation
Opens the file abcd.txt in write mode.
Step 2: Write the First String
main.py
No output captured.
Explanation
Writes the string Durga into the file.
Since there is no \n, the cursor remains on the same line.
Step 3: Write the Second String
main.py
No output captured.
Explanation
The string Software is written immediately after Durga.
No space or new line is added automatically.
Step 4: Write the Third String
main.py
No output captured.
Explanation
The string Solutions is written immediately after Software.
As a result, all three strings appear continuously in a single line.
Step 5: Close the File
main.py
No output captured.
Explanation
Closes the file after completing the write operation.
Observation
Since the newline character (\n) is not used, all the strings are written continuously in a single line.
Correct Way: Using the Newline Character
The following program uses the newline character while writing each string.
Program
main.py
No output captured.
Contents of abcd.txt
Program Explanation
Let's understand why each string appears on a separate line.
Write the First Line
main.py
No output captured.
Explanation
Writes Durga and then moves the cursor to the next line.
Write the Second Line
main.py
No output captured.
Explanation
Writes Software on the next line and again moves the cursor to the following line.
Write the Third Line
main.py
No output captured.
Explanation
Writes Solutions on the next available line.
Result
Each call to write() includes the newline character (\n), so every string is written on a separate line.
How the Newline Character Works
main.py
DurgaSoftwareSolutions
With \n
write("Durga\n")
write("Software\n")
write("Solutions\n")
Durga
Software
SolutionsExecution Flow
main.py
No output captured.
Summary
| Method / Symbol | Purpose |
|---|---|
write() |
Writes a string to a file. |
\n |
Moves the cursor to the next line. |
writelines() |
Writes multiple strings from a list. |
Important Notes
- While using the
write()method, we must provide the newline character (\n) if each string should appear on a separate line. - If
\nis not used, all strings are written in a single line. - The
writelines()method writes the strings exactly as they are provided in the list. - If newline characters are required while using
writelines(), they must be included in each string of the list.
Quick Recap
write()does not automatically insert a new line.- The newline character (
\n) is responsible for moving the cursor to the next line. - Without
\n, all strings are written continuously. - When using
writelines(), newline characters must be included manually if line breaks are required.
Important Interview Questions
- Why is the newline character (
\n) required while usingwrite()? - What happens if
\nis not used? - Does the
write()method automatically move to the next line? - How does
writelines()handle newline characters? - What is the difference between writing with and without
\n? - Can multiple strings be written on separate lines without using
\n? - Why do all strings appear on a single line in some programs?
- Which symbol is used to move the cursor to the next line in Python?
Section Completion
This completes the Writing Data to Text Files section of the File Handling chapter.
The following topics have been covered:
- ✅
write()method - ✅
writelines()method - ✅ Writing a single string
- ✅ Writing multiple lines
- ✅ Append mode (
a) - ✅ Importance of the newline character (
\n)
Checking Whether a File Exists
Before performing operations on a file, it is a good programming practice to check whether the file exists.
If we try to open a file that does not exist in read (r) mode, Python raises a FileNotFoundError.
To avoid this error, Python provides the os.path.isfile() function.
os.path.isfile() Function
The os.path.isfile() function checks whether the specified file exists.
Syntax
main.py
No output captured.
Return Value
- Returns
Trueif the specified file exists. - Returns
Falseif the file does not exist.
Example: Checking File Existence
main.py
No output captured.
Sample Output (File Exists)
Sample Output (File Does Not Exist)
Program Explanation
Let's understand each statement used in the program.
Import the os Module
main.py
No output captured.
Explanation
The os module provides various functions for interacting with the operating system.
The path.isfile() function belongs to this module.
Read File Name from the User
main.py
No output captured.
Explanation
Accepts the file name from the user at runtime.
Check File Existence
main.py
No output captured.
Explanation
If the specified file exists, the condition becomes True.
Otherwise, it becomes False.
Display the Result
main.py
No output captured.
Explanation
Displays the appropriate message based on whether the file exists.
Why Check File Existence?
Checking whether a file exists helps prevent runtime errors.
It also allows the program to handle missing files gracefully instead of terminating unexpectedly.
Program: Count Lines, Words and Characters
The following program reads a text file and counts:
- Total number of lines
- Total number of words
- Total number of characters
Example
main.py
No output captured.
Sample Output
Understanding the Program
Let's understand how the program calculates the number of lines, words, and characters.
Initialize the Counters
main.py
No output captured.
Explanation
Three variables are initialized to zero.
- lcount → Counts the number of lines.
- wcount → Counts the number of words.
- ccount → Counts the number of characters.
Read the File Line by Line
main.py
No output captured.
Explanation
The for loop reads one line at a time from the file.
Count the Lines
main.py
No output captured.
Explanation
Each iteration represents one line, so the line counter is incremented by one.
Split the Line into Words
main.py
No output captured.
Explanation
The split() method separates the line into individual words.
It returns a list containing all the words in that line.
Count the Words
main.py
No output captured.
Explanation
The number of words in the current line is added to the total word count.
Count the Characters
main.py
No output captured.
Explanation
The len(line) function returns the number of characters in the current line.
The result is added to the total character count.
Execution Flow
main.py
No output captured.
Summary
| Function / Method | Purpose |
|---|---|
os.path.isfile() |
Checks whether a file exists. |
split() |
Splits a line into words. |
len() |
Returns the number of characters or list elements. |
for line in file |
Reads one line at a time. |
Important Notes
- Always check whether a file exists before opening it in read mode.
os.path.isfile()returnsTrueonly if the specified file exists.- The
split()method separates a line into individual words. len()is used to count words and characters.- Reading a file line by line is memory efficient for large files.
- Always close the file after completing all file operations.
Quick Recap
- Use
os.path.isfile()to verify file existence. - Checking file existence prevents
FileNotFoundError. - Use
split()to separate words in a line. - Use
len()to count lines, words, and characters. - Process files line by line using a
forloop.
Important Interview Questions
- Why should we check whether a file exists before opening it?
- What is the purpose of
os.path.isfile()? - What values can
os.path.isfile()return? - How can you count the number of lines in a text file?
- How can you count the number of words in a file?
- How can you count the number of characters in a file?
- What is the purpose of the
split()method? - Why is the
len()function used in this program?
Working with Binary Files
Until now, we have worked with text files. Python also supports working with binary files.
Binary files are used to store non-text data such as:
- Images
- Audio files
- Video files
- PDF documents
- Executable files
While working with binary files, the file should be opened in binary mode by using rb, wb, ab, etc.
Common Binary File Modes
| Mode | Description |
|---|---|
rb |
Read binary file. |
wb |
Write binary file. |
ab |
Append binary data. |
rb+ |
Read and write binary file. |
wb+ |
Write and read binary file. |
ab+ |
Append and read binary file. |
Example: Copy an Image File
The following program copies the contents of one image file into another image file.
Program
main.py
No output captured.
Output
Program Explanation
Let's understand each statement in the program.
Step 1: Open the Source Image
main.py
No output captured.
Explanation
Opens the source image in read binary mode.
The image data can now be read as binary bytes.
Step 2: Read Binary Data
main.py
No output captured.
Explanation
Reads the complete binary contents of the image.
The binary data is stored in the variable data.
Step 3: Open the Destination File
main.py
No output captured.
Explanation
Creates a new binary file in write mode.
If the file already exists, its contents will be overwritten.
Step 4: Write Binary Data
main.py
No output captured.
Explanation
Writes all binary data into the new image file.
The copied image will be exactly the same as the original image.
Step 5: Close Both Files
main.py
No output captured.
Explanation
Both source and destination files are closed after the copy operation is completed.
Execution Flow
main.py
No output captured.
Introduction to CSV Files
CSV stands for Comma Separated Values.
A CSV file is used to store tabular data such as:
- Student records
- Employee records
- Sales reports
- Product information
Each row in a CSV file represents one record, and each value is separated by a comma.
CSV Module
Python provides the built-in csv module for working with CSV files.
Import Statement
main.py
No output captured.
Writing Data into a CSV File
To write data into a CSV file, we use the csv.writer() function.
Syntax
main.py
No output captured.
writerow() Method
The writerow() method writes a single record into the CSV file.
Syntax
main.py
No output captured.
Example: Write Employee Records
main.py
No output captured.
Sample Output
Generated emp.csv File
Understanding the Program
Let's understand how the CSV writing program works.
Open the CSV File
main.py
No output captured.
Explanation
Creates the CSV file in write mode.
The parameter newline='' prevents blank lines from appearing between records.
Create CSV Writer
main.py
No output captured.
Explanation
Creates a CSV writer object that is responsible for writing records into the CSV file.
Write Header Row
main.py
No output captured.
Explanation
Writes the column headings into the CSV file.
Write Employee Records
main.py
No output captured.
Explanation
Each call to writerow() writes one employee record into the CSV file.
Summary
| Function / Method | Purpose |
|---|---|
rb |
Read binary file. |
wb |
Write binary file. |
csv.writer() |
Creates a CSV writer object. |
writerow() |
Writes one record into the CSV file. |
newline='' |
Prevents blank lines in CSV files. |
Important Notes
- Binary files store non-text data such as images, videos, and audio files.
- Always use binary modes (
rb,wb, etc.) while working with binary files. - The
csvmodule is used to work with CSV files. csv.writer()creates a writer object.writerow()writes one record at a time.- Always use
newline=''while opening a CSV file for writing. - Always close the file after completing file operations.
Quick Recap
- Binary files are used for storing images, videos, audio, and other binary data.
- Use
rbto read andwbto write binary files. - CSV stands for Comma Separated Values.
- Use the
csvmodule to create and manage CSV files. - Use
writerow()to insert one record into a CSV file.
Important Interview Questions
- What is a binary file?
- Which file modes are used for binary files?
- How can you copy one image file to another in Python?
- What does CSV stand for?
- What is the purpose of the
csvmodule? - What does
csv.writer()return? - What is the purpose of the
writerow()method? - Why is
newline=''used while creating CSV files?
Reading Data from a CSV File
Python provides the csv.reader() function to read data from a CSV file.
The reader() function reads one record at a time and returns each record as a list.
CSV Reader
To read records from a CSV file, we first create a reader object by using the csv.reader() function.
Syntax
main.py
No output captured.
Example: Reading Employee Records
main.py
No output captured.
Sample Output
Program Explanation
Let's understand how the program reads records from a CSV file.
Step 1: Import the CSV Module
main.py
No output captured.
Explanation
The csv module provides functions for reading and writing CSV files.
Step 2: Open the CSV File
main.py
No output captured.
Explanation
Opens the CSV file in read mode.
Step 3: Create the Reader Object
main.py
No output captured.
Explanation
Creates a CSV reader object.
The reader object reads one record at a time from the CSV file.
Step 4: Read Each Record
main.py
No output captured.
Explanation
The for loop reads every record in the file.
Each record is returned as a list of column values.
Step 5: Close the File
main.py
No output captured.
Explanation
Closes the CSV file after completing the read operation.
Execution Flow
main.py
No output captured.
Introduction to ZIP Files
A ZIP file is a compressed file that can store one or more files and folders.
Compression reduces the file size, making it easier to store and transfer files.
Python provides the built-in zipfile module for creating, reading, and extracting ZIP files.
Advantages of ZIP Files
- Reduces file size.
- Saves storage space.
- Makes file transfer faster.
- Allows multiple files to be stored in a single archive.
- Easy to share files over the Internet.
zipfile Module
Python provides the zipfile module to perform ZIP file operations.
Import Statement
main.py
No output captured.
ZipFile Class
The ZipFile class is used to create, read, and extract ZIP files.
Syntax
main.py
No output captured.
Parameters
| Parameter | Description |
|---|---|
file_name |
Name of the ZIP file. |
mode |
Specifies how the ZIP file should be opened. |
compression |
Specifies the compression method. |
Compression Types
The most commonly used compression constants are:
| Constant | Description |
|---|---|
ZIP_STORED |
No compression is applied. |
ZIP_DEFLATED |
Compresses files to reduce their size. |
ZIP_DEFLATED
ZIP_DEFLATED is the most commonly used compression method.
It compresses the file before storing it inside the ZIP archive, thereby reducing the file size.
When to Use ZIP Files
- Backing up files.
- Sending multiple files through email.
- Reducing storage space.
- Distributing software packages.
- Archiving project folders.
Summary
| Function / Class | Purpose |
|---|---|
csv.reader() |
Reads records from a CSV file. |
ZipFile |
Creates, reads, and extracts ZIP files. |
ZIP_STORED |
Stores files without compression. |
ZIP_DEFLATED |
Compresses files before storing them. |
Important Notes
csv.reader()is used to read records from a CSV file.- Each record is returned as a list.
- The
zipfilemodule provides support for ZIP file operations. - The
ZipFileclass is the main class used for ZIP file handling. ZIP_DEFLATEDperforms file compression.ZIP_STOREDstores files without compression.- ZIP files reduce storage space and simplify file sharing.
Quick Recap
- Use
csv.reader()to read CSV files. - Each CSV row is returned as a list.
- Use the
zipfilemodule for ZIP file operations. - The
ZipFileclass handles ZIP archives. ZIP_DEFLATEDcompresses files, whileZIP_STOREDdoes not.
Important Interview Questions
- What is the purpose of the
csv.reader()function? - What does each record returned by
csv.reader()contain? - What is a ZIP file?
- Which Python module is used for ZIP file handling?
- What is the purpose of the
ZipFileclass? - What is the difference between
ZIP_STOREDandZIP_DEFLATED? - Why are ZIP files commonly used?
- What are the advantages of compressing files?
Creating a ZIP File
Python provides the built-in zipfile module to create ZIP files.
To create a ZIP file, we create a ZipFile object in write mode by using the ZIP_DEFLATED compression constant.
Program: Creating a ZIP File
main.py
No output captured.
Output
Program Explanation
Let's understand each statement in the program.
Import the Module
main.py
No output captured.
Explanation
Imports all classes, constants, and functions from the zipfile module.
Create a ZIP File
main.py
No output captured.
Explanation
Creates a ZIP file named files.zip.
"w"opens the ZIP file in write mode.ZIP_DEFLATEDindicates that the files should be compressed before being stored.
Add Files to the ZIP Archive
main.py
No output captured.
Explanation
The write() method adds each specified file into the ZIP archive.
Each call inserts one file into the ZIP file.
Close the ZIP File
main.py
No output captured.
Explanation
Closes the ZIP file after adding all files.
Performing Unzip Operation
To read or extract information from an existing ZIP file, create a ZipFile object in read mode.
Syntax
main.py
No output captured.
Explanation
ZIP_STORED represents the unzip operation.
It is the default value, so specifying it is optional.
namelist() Method
After opening the ZIP file, we can obtain the names of all files stored inside it by using the namelist() method.
Syntax
main.py
No output captured.
Program: Display All Files from a ZIP Archive
main.py
No output captured.
Program Explanation
The following steps explain how the ZIP archive is processed.
Open the ZIP File
main.py
No output captured.
Explanation
Opens the ZIP file in read mode.
ZIP_STORED is the default compression constant used while reading ZIP files.
Get All File Names
main.py
No output captured.
Explanation
Returns a list containing the names of all files present in the ZIP archive.
Process Each File
main.py
No output captured.
Explanation
The for loop processes every file present inside the ZIP archive one by one.
Open Each File
main.py
No output captured.
Explanation
Opens the current extracted file in read mode.
Display File Contents
main.py
No output captured.
Explanation
Reads and displays the complete contents of the current file.
Execution Flow
main.py
No output captured.
Summary
| Function / Method | Purpose |
|---|---|
ZipFile() |
Creates or opens a ZIP file. |
ZIP_DEFLATED |
Used while creating a ZIP file. |
ZIP_STORED |
Used while reading a ZIP file (default value). |
write() |
Adds files to the ZIP archive. |
namelist() |
Returns the names of all files present in the ZIP archive. |
Important Notes
- The
ZipFileclass is available in Python's built-inzipfilemodule. - Use
"w"mode withZIP_DEFLATEDto create a ZIP file. - The
write()method adds files to the ZIP archive. - Use
"r"mode to open an existing ZIP file. ZIP_STOREDis the default value for reading ZIP files.- The
namelist()method returns the names of all files stored inside the ZIP archive. - After obtaining the file names, each file can be opened and its contents can be displayed.
Quick Recap
- Create ZIP files using
ZipFile()withZIP_DEFLATED. - Add files to the archive using the
write()method. - Open an existing ZIP file in read mode.
- Use
namelist()to retrieve all file names stored in the archive. - Read each extracted file individually to display its contents.
Important Interview Questions
- Which module is used to perform ZIP operations in Python?
- What is the purpose of the
ZipFileclass? - Why is
ZIP_DEFLATEDused while creating a ZIP file? - What does the
write()method do? - Which mode is used to read an existing ZIP file?
- What is the purpose of the
namelist()method? - What is the difference between
ZIP_DEFLATEDandZIP_STORED? - How can you display the contents of every file present inside a ZIP archive?
Working with Directories
It is very common to perform operations on directories while working with files.
Python provides the built-in os module to perform directory-related operations.
Some common directory operations are:
- Know the current working directory.
- Create a new directory.
- Create multiple directories.
- Remove an existing directory.
- Remove multiple directories.
Import the os Module
main.py
No output captured.
Explanation
The os module provides various functions to interact with the operating system.
It allows us to create, remove, rename, and manage directories.
Q1. Know the Current Working Directory
The current working directory (CWD) is the directory from which the Python program is currently running.
To get the current working directory, use the os.getcwd() function.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Returns the path of the current working directory.
Q2. Create a Subdirectory in the Current Working Directory
We can create a new directory inside the current working directory by using the os.mkdir() function.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Creates a new directory named mysub inside the current working directory.
Directory Structure
main.py
No output captured.
Q3. Create a Subdirectory Inside Another Directory
Suppose the following directory structure already exists.
Before
main.py
No output captured.
Requirement
Create another directory named mysub2 inside mysub.
Program
main.py
No output captured.
Important Note
Assume that mysub is already present in the current working directory.
Explanation
main.py
No output captured.
Explanation
Creates the directory mysub2 inside the existing directory mysub.
Directory Structure
main.py
No output captured.
Q4. Create Multiple Directories
If we want to create multiple directories in a single statement, Python provides the os.makedirs() function.
Required Directory Structure
main.py
No output captured.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Creates all directories specified in the given path in a single statement.
If the parent directories do not exist, Python automatically creates them.
Directory Structure
main.py
No output captured.
Q5. Remove a Directory
To remove an existing empty directory, Python provides the os.rmdir() function.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Removes the specified directory.
The directory must be empty before it can be removed.
Q6. Remove Multiple Directories
To remove multiple empty directories in a specified path, Python provides the os.removedirs() function.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Removes all directories present in the specified path.
The directories must be empty before they can be removed.
Execution Flow
main.py
No output captured.
Summary
| Function | Purpose |
|---|---|
os.getcwd() |
Returns the current working directory. |
os.mkdir() |
Creates a single directory. |
os.makedirs() |
Creates multiple directories. |
os.rmdir() |
Removes a single directory. |
os.removedirs() |
Removes multiple directories. |
Important Notes
- Python provides the built-in
osmodule for directory-related operations. os.getcwd()returns the current working directory path.os.mkdir()creates a single directory.os.makedirs()creates multiple directories in one call.os.rmdir()removes one empty directory.os.removedirs()removes multiple empty directories from the specified path.
Quick Recap
- Use
os.getcwd()to know the current working directory. - Use
os.mkdir()to create a single directory. - Use
os.makedirs()to create nested directories. - Use
os.rmdir()to remove one empty directory. - Use
os.removedirs()to remove multiple empty directories.
Important Interview Questions
- What is the purpose of the
osmodule? - What does
os.getcwd()return? - What is the difference between
os.mkdir()andos.makedirs()? - What is the difference between
os.rmdir()andos.removedirs()? - Can
os.mkdir()create multiple directories in one statement? - When should
os.makedirs()be used? - Can
os.rmdir()remove a non-empty directory? - Why is the current working directory important in file handling?
Directory Operations (Part 2)
In the previous section, we learned how to create and remove directories.
In this section, we will learn how to:
- Rename a directory.
- Display the contents of a directory.
- Display the contents of a directory including all subdirectories.
Q7. Rename a Directory
We can rename an existing directory by using the rename() function of the os module.
Program
main.py
No output captured.
Explanation
main.py
No output captured.
Explanation
Renames the existing directory mysub to newdir.
Directory Structure
main.py
No output captured.
Q8. Display the Contents of a Directory
The os module provides the listdir() function to display the contents of a specified directory.
Note: It displays only the contents of the specified directory. It does not display the contents of its subdirectories.
Program
main.py
No output captured.
Sample Output
Explanation
main.py
No output captured.
Explanation
"."represents the current working directory.- The function returns a list containing all files and directories present in that directory.
- It does not display the contents of subdirectories.
Q9. Display the Contents of a Directory Including Subdirectories
To display the contents of a directory along with all its subdirectories, Python provides the os.walk() function.
Syntax
main.py
No output captured.
Explanation
The os.walk() function returns an iterator object whose contents can be displayed using a for loop.
Where:
- path → Directory path.
.represents the current working directory.
Program
main.py
No output captured.
Sample Output
Program Explanation
Let's understand how the program traverses the directory tree.
Traverse the Directory Tree
main.py
No output captured.
Explanation
Returns an iterator that traverses the complete directory tree.
The for Loop
main.py
No output captured.
Explanation
For every directory visited, the loop receives:
- dirpath → Current directory path.
- dirnames → List of all subdirectories inside the current directory.
- filenames → List of all files inside the current directory.
Display Directory Information
main.py
No output captured.
Explanation
Displays the directory path, subdirectories, and files for every directory visited by os.walk().
Important Note
To display the contents of a particular directory, provide its name as the argument to walk().
Example
main.py
No output captured.
Difference Between listdir() and walk()
| listdir() | walk() |
|---|---|
| Displays the contents of the specified directory only. | Displays the contents of the specified directory and all its subdirectories. |
| Returns a list. | Returns an iterator. |
| Does not traverse the directory tree. | Traverses the complete directory tree. |
Execution Flow
main.py
No output captured.
Summary
| Function | Purpose |
|---|---|
os.rename() |
Renames a directory. |
os.listdir() |
Displays the contents of a directory. |
os.walk() |
Traverses a directory and all its subdirectories. |
Important Notes
- Use
os.rename()to rename an existing directory. os.listdir()displays only the contents of the specified directory.os.listdir()does not display the contents of subdirectories.os.walk()traverses the complete directory tree.os.walk()returns an iterator object.- The iterator provides
dirpath,dirnames, andfilenamesfor each directory. - To traverse another directory, pass its path as the argument to
os.walk().
Quick Recap
- Rename directories using
os.rename(). - Display only the current directory using
os.listdir(). - Display all directories recursively using
os.walk(). listdir()returns a list, whereaswalk()returns an iterator.
Important Interview Questions
- How can you rename a directory in Python?
- What is the purpose of the
os.listdir()function? - Does
os.listdir()display subdirectory contents? - What is the purpose of the
os.walk()function? - What values are returned by
os.walk()? - What is the difference between
os.listdir()andos.walk()? - How can you traverse all files in a directory tree?
- Which function returns an iterator object?
Executing Operating System Commands
Sometimes, from a Python program, we need to execute operating system (DOS) commands.
Python provides the os.system() function for this purpose.
Syntax
main.py
No output captured.
Example
main.py
No output captured.
Explanation
The os.system() function executes the specified operating system command.
In Windows, the command dir displays the files and directories in the current working directory.
Common Uses of os.system()
- Display directory contents.
- Create or delete files using OS commands.
- Execute external programs.
- Run batch or shell commands.
Getting File Statistics
Python provides the os.stat() function to obtain complete information about a file.
Syntax
main.py
No output captured.
Important File Properties
| Property | Description |
|---|---|
st_mode |
Protection bits of the file. |
st_ino |
Inode number. |
st_dev |
Device identifier. |
st_nlink |
Number of hard links. |
st_uid |
User ID of the owner. |
st_gid |
Group ID of the owner. |
st_size |
File size in bytes. |
st_atime |
Time of the most recent access. |
st_mtime |
Time of the most recent modification. |
st_ctime |
Time of the most recent metadata change. |
Important Note
According to the PDF:
st_atimest_mtimest_ctime
return the time as the number of milliseconds since January 1, 1970, 12:00 AM.
By using the datetime module and its fromtimestamp() function, we can obtain the exact date and time.
Program: Display All File Statistics
main.py
No output captured.
Sample Output
Program: Display Specific File Properties
main.py
No output captured.
Sample Output
Program Explanation
Let's understand how the program retrieves file information.
Get File Statistics
main.py
No output captured.
Explanation
Returns a file statistics object containing detailed information about the file.
Display File Size
main.py
No output captured.
Explanation
Returns the size of the file in bytes.
Display Last Access Time
main.py
No output captured.
Explanation
Converts the last accessed timestamp into a readable date and time.
Display Last Modified Time
main.py
No output captured.
Explanation
Converts the last modified timestamp into a readable date and time.
Execution Flow
main.py
No output captured.
Complete Chapter Summary
| Topic | Description |
|---|---|
| File Handling | Store data permanently in files. |
| Text File | Stores character data. |
| Binary File | Stores images, videos, audio files. |
open() | Opens a file. |
close() | Closes a file. |
read() | Reads complete file data. |
read(n) | Reads the specified number of characters. |
readline() | Reads one line. |
readlines() | Reads all lines into a list. |
write() | Writes a string to a file. |
writelines() | Writes multiple strings. |
os.path.isfile() | Checks whether a file exists. |
csv.writer() | Writes CSV data. |
csv.reader() | Reads CSV data. |
ZipFile | Creates and manages ZIP files. |
os.getcwd() | Returns the current working directory. |
os.mkdir() | Creates a directory. |
os.makedirs() | Creates multiple directories. |
os.rmdir() | Removes a directory. |
os.removedirs() | Removes multiple directories. |
os.rename() | Renames a directory. |
os.listdir() | Lists directory contents. |
os.walk() | Traverses directories and subdirectories. |
os.system() | Executes operating system commands. |
os.stat() | Returns complete file statistics. |
Important Notes
- The
os.system()function executes operating system commands from a Python program. - The
os.stat()function returns complete information about a file. st_sizereturns the file size in bytes.st_atimereturns the last accessed time.st_mtimereturns the last modified time.st_ctimereturns the last metadata change time.datetime.fromtimestamp()converts timestamp values into a readable date and time.
Quick Recap
- Use
os.system()to execute operating system commands. - Use
os.stat()to obtain detailed file information. - Use
st_sizeto determine the file size. - Use
st_atimeandst_mtimeto obtain file timestamps. - Convert timestamps into readable format by using
datetime.fromtimestamp().
Important Interview Questions
- What is the purpose of the
os.system()function? - What is the purpose of the
os.stat()function? - What information does
st_sizeprovide? - What is the difference between
st_atimeandst_mtime? - Why is
datetime.fromtimestamp()used? - Which module provides the
os.stat()function? - How can you execute an operating system command from Python?
- How can you determine the size of a file in Python?