File Handling
File Handling
Python
File Handling:
• Python provides a robust and flexible set of tools for file handling,
allowing you to read from and write to files easily.
File Handling:
• Python provides a robust and flexible set of tools for file handling,
allowing you to read from and write to files easily.
• Python treats files differently as text or binary and this is important.
File Handling:
• Python provides a robust and flexible set of tools for file handling,
allowing you to read from and write to files easily.
• Python treats files differently as text or binary and this is important.
• Each line of code includes a sequence of characters, and they
form a text file.
File Handling:
• Python provides a robust and flexible set of tools for file handling,
allowing you to read from and write to files easily.
• Python treats files differently as text or binary and this is important.
• Each line of code includes a sequence of characters, and they
form a text file.
• Each line of a file is terminated with a special character, called the EOL
or End of Line characters like comma {,} or newline character.
File Handling:
• Python provides a robust and flexible set of tools for file handling,
allowing you to read from and write to files easily.
• Python treats files differently as text or binary and this is important.
• Each line of code includes a sequence of characters, and they
form a text file.
• Each line of a file is terminated with a special character, called the EOL
or End of Line characters like comma {,} or newline character.
• It ends the current line and tells the interpreter a new one has begun.
Advantages of File Handling:
• Versatility: File handling in Python allows you to perform a wide range
of operations, such as creating, reading, writing, appending, renaming,
and deleting files.
Advantages of File Handling:
• Versatility: File handling in Python allows you to perform a wide range of
operations, such as creating, reading, writing, appending, renaming, and
deleting files.
• Flexibility: File handling in Python is highly flexible, as it allows you to
work with different file types (e.g. text files, binary files, CSV files, etc.),
and to perform different operations on files (e.g. read, write, append,
etc.).
Advantages of File Handling:
• Versatility: File handling in Python allows you to perform a wide range of
operations, such as creating, reading, writing, appending, renaming, and
deleting files.
• Flexibility: File handling in Python is highly flexible, as it allows you to
work with different file types (e.g. text files, binary files, CSV files, etc.),
and to perform different operations on files (e.g. read, write, append,
etc.).
• User–friendly: Python provides a user-friendly interface for file
handling, making it easy to create, read, and manipulate files.
Advantages of File Handling:
• Versatility: File handling in Python allows you to perform a wide range of
operations, such as creating, reading, writing, appending, renaming, and
deleting files.
• Flexibility: File handling in Python is highly flexible, as it allows you to
work with different file types (e.g. text files, binary files, CSV files, etc.),
and to perform different operations on files (e.g. read, write, append,
etc.).
• User–friendly: Python provides a user-friendly interface for file handling,
making it easy to create, read, and manipulate files.
• Cross-platform: Python file-handling functions work across different
platforms (e.g. Windows, Mac, Linux), allowing for seamless integration
and compatibility.
open() Function in Python:
• Before performing any operation on the file like reading or writing,
first, we have to open that file.
open() Function in Python:
• Before performing any operation on the file like reading or writing,
first, we have to open that file.
• we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose
of the opening file.
open() Function in Python:
• Before performing any operation on the file like reading or writing,
first, we have to open that file.
• we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose
of the opening file.
f = open(filename, mode)
open() Function in Python:
• Before performing any operation on the file like reading or writing,
first, we have to open that file.
• we should use Python’s inbuilt function open() but at the time of
opening, we have to specify the mode, which represents the purpose
of the opening file.
f = open(filename, mode)
following mode is supported: r r+
w w+
a a+
f = open(filename, mode)
• r: open an existing file for a read operation.
f = open(filename, mode)
• r: open an existing file for a read operation.
• w: open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
f = open(filename, mode)
• r: open an existing file for a read operation.
• w: open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
• a: open an existing file for append operation. It won’t override existing
data.
f = open(filename, mode)
• r: open an existing file for a read operation.
• w: open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
• a: open an existing file for append operation. It won’t override existing
data.
• r+: To read and write data into the file. The previous data in the file will
be overridden.
f = open(filename, mode)
• r: open an existing file for a read operation.
• w: open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
• a: open an existing file for append operation. It won’t override existing
data.
• r+: To read and write data into the file. The previous data in the file will
be overridden.
• w+: To write and read data. It will override existing data.
f = open(filename, mode)
• r: open an existing file for a read operation.
• w: open an existing file for a write operation. If the file already contains
some data, then it will be overridden but if the file is not present then it
creates the file as well.
• a: open an existing file for append operation. It won’t override existing
data.
• r+: To read and write data into the file. The previous data in the file will
be overridden.
• w+: To write and read data. It will override existing data.
• a+: To append and read data from the file. It won’t override existing
data.
The open command will open the file in the
read mode and the for loop will print each
line present in the file.
file = open(‘file_1.txt', 'r’)
for each in file:
print (each)
we will extract a string that contains all
characters in the file then we can use
file.read().
file = open(‘file_1.txt', 'r’)
print (file.read())
we will see how we can read a file using the
with statement.
create_file(filename)
read_file(filename)
append_file(filename, "This is some additional text.\n")
read_file(filename)
rename_file(filename, new_filename)
read_file(new_filename)
delete_file(new_filename)
Appending Text to a File:
# Open the file in append mode
with open('example.txt', 'a') as file:
# Append text to the file
file.write('This is a new line.\n’)
file.write('Another line added.\n')
Writing Binary Files Using Pickle:
• The pickle module in Python can be used to serialize and deserialize
objects. Here's an example of writing binary files using pickle:
• In the above example, the wb mode is used to open the file in binary
write mode.
• The pickle.dump() function is then used to write the Python object
(data in this case) to the binary file.
Reading Binary Files Using Pickle:
File handling
• File handling in Python involves various operations, including
opening, reading, writing, and closing files. Python provides several
built-in functions and methods for file handling.
Opening a File:
▪ You can open a file using the open() function.
▪ filename: This is the name of the file or the path to the file.
▪ mode: This specifies the mode in which the file is opened.
Common modes include:
▪ 'r': Read (default mode).
▪ 'w': Write (creates a new file or overwrites an existing file).
▪ 'a': Append (opens the file for writing but appends to the end of the
file).
▪ 'b': Binary mode.
▪ 'x': Exclusive creation (fails if the file already exists).
Closing a File:
▪ It's essential to close a file after performing operations on it using the
close() method:
▪ readlines(): Reads all lines from the file and returns a list.
Writing to a File:
▪ To write to a file, use the write() method:
▪ You can also write multiple lines by using newline characters (\n) or
by calling write() multiple times.
Appending to a File:
• To append data to an existing file, open it in append mode ('a') and
use the write() method:
Binary File Handling:
• For binary file handling, use the 'b' mode in combination with the
above methods. For example:
Exception Handling:
• File operations can raise exceptions.
• It's good practice to use try and except blocks to handle these
exceptions, ensuring that the file is properly closed:
Using with Statement:
• The with statement is used with file handling to ensure that the file is
properly closed, even if an exception occurs: