Introduction to Files and
Types of Files
In programming, handling files is a crucial aspect of data
manipulation and storage. Files can be categorized into various
types based on their content and structure. In this tutorial, we'll
explore different types of files such as text files, binary files, and
CSV files, along with understanding relative and absolute paths.
Types of Files
1. Text File
A text file contains human-readable characters encoded in a
specific format like ASCII or UTF-8. Text files are commonly
used for storing textual data, such as code, documents, or
configuration files.
2. Binary File
Binary files contain data encoded in binary format, which can
include images, videos, executables, etc. These files are not
human-readable and require specific programs to interpret their
contents correctly.
3. CSV File
CSV (Comma-Separated Values) files are a type of text file used
for tabular data storage. Each line in a CSV file represents a row,
and columns are separated by commas or other delimiters.
Paths
Relative Path
A relative path specifies the location of a file or directory
relative to the current working directory. It does not start from
the root directory.
Absolute Path
An absolute path specifies the complete location of a file or
directory starting from the root directory.
Working with Text Files in
Opening a Text File
To open a text file in , you can use the open() function. Specify
the file path and the mode in which you want to open the file.
file = open('example.txt', 'r')
Text File Open Modes
r: Read mode (default). Opens a file for reading.
r+: Read/write mode. Opens a file for reading and writing.
w: Write mode. Opens a file for writing. Creates a new file
if it does not exist or truncates the file if it exists.
w+: Read/write mode. Opens a file for reading and writing.
Creates a new file if it does not exist or truncates the file if
it exists.
a: Append mode. Opens a file for appending. Creates a new
file if it does not exist.
a+: Append/read mode. Opens a file for reading and
appending. Creates a new file if it does not exist.
Closing a Text File
It's important to close a file after performing operations on it to
release system resources.
file.close()
Opening a File Using with Clause
The with statement automatically closes the file when the block
of code is exited.
with open('example.txt', 'r') as file:
# File operations
Writing/Appending Data to a Text File
You can write data to a text file using the write() and
writelines() methods.
with open('example.txt', 'w') as file:
file.write("Hello, World!\n")
file.writelines(["Line 2\n", "Line 3\n"])
Reading from a Text File
You can read data from a text file using the read(), readline(),
and readlines() methods.
with open('example.txt', 'r') as file:
content = file.read() # Reads entire file
line = file.readline() # Reads a single line
lines = file.readlines() # Reads all lines into a
list
Seek and Tell Methods
The seek() method moves the file pointer to a specified
position, and the tell() method returns the current position of
the file pointer.
with open('example.txt', 'r') as file:
file.seek(0) # Move pointer to the beginning
print(file.tell()) # Prints the current position
Manipulation of Data in a Text File
You can manipulate data in a text file by reading its content,
modifying it, and then writing it back.
with open('example.txt', 'r+') as file:
content = file.read()
modified_content = content.replace('old', 'new')
file.seek(0)
file.write(modified_content)
file.truncate() # Truncate to new size if content
is shorter
Conclusion
Understanding file handling in is essential for various
applications involving data input/output operations. Text files,
binary files, and CSV files serve different purposes and require
distinct handling methods. Mastering file operations enables
efficient data manipulation and storage in programs.
In file handling with , the seek() and tell() methods are used
to manipulate and determine the current position of the file
pointer within a file.
seek() Method
The seek() method is used to move the file pointer to a
specified position within the file. It allows you to navigate to
different locations within the file for reading or writing
operations. The syntax for seek() method is:
file.seek(offset, whence)
offset: It specifies the number of bytes to move the file
pointer. A positive offset moves the pointer forward, while
a negative offset moves it backward.
whence (optional): It specifies the reference position for the
offset. There are three possible values:
o 0 (default): Start of the file.
o 1: Current position of the file pointer.
o 2: End of the file.
Example:
with open('example.txt', 'r') as file:
file.seek(5) # Move pointer to the 6th byte from
the beginning of the file
data = file.read() # Reads from the current
position to the end of the file
print(data)
tell() Method
The tell() method returns the current position of the file
pointer within the file. It provides the byte offset from the
beginning of the file to the current position. The syntax for
tell() method is:
file.tell()
Example:
with open('example.txt', 'r') as file:
print(file.tell()) # Prints the current position
of the file pointer
file.seek(10) # Move pointer to the 11th byte from
the beginning of the file
print(file.tell()) # Prints the updated position
of the file pointer
Usage Scenario:
Let's consider a scenario where you have a large text file and
you want to read a specific portion of it. You can use seek() to
navigate to the desired position within the file efficiently
without reading unnecessary data from the beginning. Similarly,
tell() can be used to keep track of the current position of the
file pointer for further operations.
with open('large_file.txt', 'r') as file:
file.seek(1000) # Move to the 1001st byte from the
beginning
data = file.read(100) # Read the next 100 bytes
from the current position
print(data)
print(file.tell()) # Print the current position
Understanding and utilizing seek() and tell() methods
effectively can optimize file handling operations, especially
when dealing with large files or performing complex data
manipulations.
Here are some programs that demonstrate the usage of file
handling operations, including seek() and tell() methods:
Program 1: Reading a Specific Portion of a Text File
def read_specific_portion(file_name, start, end):
with open(file_name, 'r') as file:
file.seek(start)
data = file.read(end - start)
return data
file_name = 'example.txt'
start_position = 10
end_position = 50
print("Content from position", start_position, "to",
end_position, ":")
print(read_specific_portion(file_name, start_position,
end_position))
Program 2: Truncating a File at a Specified Position
def truncate_file_at_position(file_name, position):
with open(file_name, 'r+') as file:
file.seek(position)
file.truncate()
file_name = 'example.txt'
truncate_position = 20
truncate_file_at_position(file_name, truncate_position)
print("File truncated at position", truncate_position)
Program 3: Reading and Writing Data to a File at a Specific
Position
def read_write_at_position(file_name, position,
data_to_write=None, size_to_read=10):
with open(file_name, 'r+') as file:
file.seek(position)
if data_to_write:
file.write(data_to_write)
print("Data written at position", position)
else:
file.seek(position)
data = file.read(size_to_read)
print("Data read from position", position,
":", data)
file_name = 'example.txt'
write_position = 30
write_data = "New content"
read_position = 15
read_write_at_position(file_name, write_position,
write_data)
read_write_at_position(file_name, read_position)
These programs demonstrate practical applications of file
handling operations in , including reading specific portions of a
file, truncating a file at a certain position, and reading/writing
data at specific positions within a file using seek() and tell()
methods.
Below is a program that reads a text file and displays all the
four-letter words present in the file. Each step is explained with
comments for better understanding:
def display_four_letter_words(file_name):
# Open the text file in read mode
with open(file_name, 'r') as file:
# Read the content of the file
content = file.read()
# Split the content into words based on
whitespace
words = content.split()
print("Four-letter words in the file:")
# Iterate through each word
for word in words:
# Check if the length of the word is 4
if len(word) == 4:
# Print the four-letter word
print(word)
# File name of the text file
file_name = 'example.txt'
# Call the function to display four-letter words
display_four_letter_words(file_name)
Explanation of the program:
1. The function display_four_letter_words() takes the file
name as input and reads the content of the file.
2. It splits the content into individual words based on
whitespace using the split() method.
3. It then iterates through each word and checks if the length
of the word is equal to 4 using the len() function.
4. If the length is 4, it prints the word.
5. Finally, it calls the function with the file name as an
argument to display all the four-letter words in the text file.
Make sure to replace 'example.txt' with the name of your text
file. This program will read the contents of the file, identify all
the four-letter words, and print them out.
Binary File Operations in
Binary files contain data in a format that is not human-readable,
and they require specific methods to read from and write to
them. In this tutorial, we will cover basic operations on binary
files in , including opening files using different modes, closing
files, using the pickle module for serialization, and performing
various read, write, search, append, and update operations.
Basic Operations on a Binary File
Opening a Binary File
You can open a binary file in using the open() function with
appropriate file open modes. Common modes for binary files
include:
rb: Read mode. Opens a file for reading in binary format.
rb+: Read/write mode. Opens a file for reading and writing
in binary format.
wb: Write mode. Opens a file for writing in binary format.
Creates a new file if it does not exist, and truncates the file
if it exists.
wb+: Read/write mode. Opens a file for reading and writing
in binary format. Creates a new file if it does not exist, and
truncates the file if it exists.
ab: Append mode. Opens a file for appending in binary
format. Creates a new file if it does not exist.
ab+: Append/read mode. Opens a file for reading and
appending in binary format. Creates a new file if it does not
exist.
Closing a Binary File
It's essential to close a binary file after performing operations on
it to release system resources.
file.close()
Importing the pickle Module
The pickle module in is used for serializing and deserializing
objects. It allows you to convert objects into a byte stream,
which can then be written to a binary file or transmitted over a
network.
import pickle
dump() and load() Methods
The dump() method is used to serialize a object and write it to a
binary file, while the load() method is used to deserialize data
from a binary file and reconstruct the original object.
# Serialize and write object to binary file
pickle.dump(obj, file)
# Deserialize data from binary file
obj = pickle.load(file)
Operations on a Binary File
Read Operation
To read data from a binary file, you can use methods like
read(), readline(), or readlines().
data = file.read() # Read entire file
line = file.readline() # Read a single line
lines = file.readlines() # Read all lines into a list
Write/Create Operation
To write data to a binary file, you can use the write() method.
file.write(data)
Search Operation
To search for specific data within a binary file, you need to read
the file and then search for the desired data.
# Read the file
file.seek(0)
content = file.read()
# Search for data
if b'search_data' in content:
print("Data found")
else:
print("Data not found")
Append Operation
To append data to a binary file, you can use the seek() method
to move the file pointer to the end of the file and then use the
write() method to add data.
file.seek(0, 2) # Move to the end of the file
file.write(data_to_append)
Update Operation
To update data in a binary file, you need to read the file, modify
the data, and then write the modified data back to the file.
# Read the file
file.seek(0)
content = file.read()
# Modify the data
modified_content = modify_data(content)
# Write the modified data back to the file
file.seek(0)
file.write(modified_content)
This covers the basic operations on binary files in , including
opening files using different modes, closing files, using the
pickle module for serialization, and performing various read,
write, search, append, and update operations.
CSV File Operations in
CSV (Comma-Separated Values) files are commonly used for
storing tabular data. provides the csv module to facilitate
reading from and writing to CSV files. In this tutorial, we'll
explore how to import the csv module, open and close CSV
files, write data to a CSV file, and read data from a CSV file.
Importing the csv Module
The csv module in provides classes and methods for working
with CSV files.
import csv
Opening and Closing a CSV File
You can use the open() function to open a CSV file in the
desired mode, and the file object can then be used with the
csv.reader() or csv.writer() functions.
# Open a CSV file for reading
with open('file.csv', 'r') as file:
reader = csv.reader(file)
# Read data from the file
# Open a CSV file for writing
with open('file.csv', 'w') as file:
writer = csv.writer(file)
# Write data to the file
Writing Data to a CSV File
You can write data to a CSV file using the csv.writer()
function along with methods like writerow() and writerows().
with open('file.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'Country']) #
Write a single row
writer.writerows(rows) # Write multiple rows
Reading Data from a CSV File
You can read data from a CSV file using the csv.reader()
function, which returns an iterator over the lines in the file.
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row) # Process each row
This covers the basic operations on CSV files in , including
importing the csv module, opening and closing CSV files,
writing data to a CSV file using writer() along with
writerow() and writerows(), and reading data from a CSV file
using reader().
Here are some programs with explanatory comments to
elaborate on binary file reading, writing, and the usage of the
pickle module's dump() and load() functions:
Program 1: Writing Data to a Binary File
import pickle
# Data to be written to the binary file
data = {'name': 'John', 'age': 30, 'city': 'New York'}
# Open the binary file in write mode
with open('data.bin', 'wb') as file:
# Serialize and write the data to the binary file
pickle.dump(data, file)
print("Data written to the binary file successfully.")
Program 2: Reading Data from a Binary File
import pickle
# Open the binary file in read mode
with open('data.bin', 'rb') as file:
# Deserialize data from the binary file
data = pickle.load(file)
# Display the data
print("Data read from the binary file:")
print(data)
Program 3: Appending Data to a Binary File
import pickle
# Additional data to be appended to the binary file
additional_data = {'name': 'Alice', 'age': 25, 'city':
'Los Angeles'}
# Open the binary file in append mode
with open('data.bin', 'ab') as file:
# Serialize and append the additional data to the
binary file
pickle.dump(additional_data, file)
print("Additional data appended to the binary file
successfully.")
Program 4: Updating Data in a Binary File
import pickle
# Define a function to update data in the binary file
def update_data(file_name, key, new_value):
# Open the binary file in read mode
with open(file_name, 'rb') as file:
# Deserialize data from the binary file
data = pickle.load(file)
# Update the data
data[key] = new_value
# Open the binary file in write mode
with open(file_name, 'wb') as file:
# Serialize and write the updated data to the
binary file
pickle.dump(data, file)
print("Data updated in the binary file
successfully.")
# Update the age in the binary file
update_data('data.bin', 'age', 35)
Program 5: Searching for Data in a Binary File
import pickle
# Define a function to search for data in the binary
file
def search_data(file_name, key, value):
# Open the binary file in read mode
with open(file_name, 'rb') as file:
# Deserialize data from the binary file
data = pickle.load(file)
# Check if the key-value pair exists in the data
if key in data and data[key] == value:
print("Data found in the binary file.")
else:
print("Data not found in the binary file.")
# Search for the name 'John' in the binary file
search_data('data.bin', 'name', 'John')
These programs demonstrate various operations on binary files
in , including writing, reading, appending, updating, and
searching for data. The usage of the pickle module's dump() and
load() functions enables serialization and deserialization of
objects, allowing seamless storage and retrieval of complex data
structures in binary files.
Here are some additional programs demonstrating the use of
pickle's dump() and load() functions for binary file operations:
Program 1: Storing and Retrieving a List of Dictionaries
import pickle
# Data to be stored in the binary file
data = [{'name': 'John', 'age': 30},
{'name': 'Alice', 'age': 25},
{'name': 'Bob', 'age': 35}]
# Writing data to the binary file
with open('data.bin', 'wb') as file:
pickle.dump(data, file)
print("Data written to the binary file successfully.")
# Reading data from the binary file
with open('data.bin', 'rb') as file:
loaded_data = pickle.load(file)
print("Data read from the binary file:")
print(loaded_data)
Program 2: Storing and Retrieving a Dictionary of Lists
import pickle
# Data to be stored in the binary file
data = {'names': ['John', 'Alice', 'Bob'],
'ages': [30, 25, 35]}
# Writing data to the binary file
with open('data.bin', 'wb') as file:
pickle.dump(data, file)
print("Data written to the binary file successfully.")
# Reading data from the binary file
with open('data.bin', 'rb') as file:
loaded_data = pickle.load(file)
print("Data read from the binary file:")
print(loaded_data)
Program 3: Storing and Retrieving Custom Objects
import pickle
# Define a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
# Create objects of the class
person1 = Person('John', 30)
person2 = Person('Alice', 25)
person3 = Person('Bob', 35)
# Data to be stored in the binary file
data = [person1, person2, person3]
# Writing data to the binary file
with open('data.bin', 'wb') as file:
pickle.dump(data, file)
print("Data written to the binary file successfully.")
# Reading data from the binary file
with open('data.bin', 'rb') as file:
loaded_data = pickle.load(file)
print("Data read from the binary file:")
for person in loaded_data:
print(person.name, person.age)
Program 4: Appending Data to an Existing Binary File
import pickle
# Define a function to append data to the binary file
def append_data(file_name, new_data):
# Reading existing data from the binary file
with open(file_name, 'rb') as file:
existing_data = pickle.load(file)
# Appending new data
existing_data.append(new_data)
# Writing updated data back to the binary file
with open(file_name, 'wb') as file:
pickle.dump(existing_data, file)
print("Data appended to the binary file
successfully.")
# Data to be appended
new_person = Person('Charlie', 40)
# Append data to the binary file
append_data('data.bin', new_person)
These programs showcase the versatility of pickle in storing
and retrieving various types of data structures, including lists,
dictionaries, and custom objects, in binary files. The dump()
function is used to serialize and write data to the file, while the
load() function is used to deserialize and read data from the
file.
Here are a few more programs demonstrating different scenarios
and operations with binary files and pickle:
Program 5: Updating Data in a List of Dictionaries Stored in
a Binary File
import pickle
# Function to update a person's age in the list of
dictionaries
def update_age(file_name, name, new_age):
# Reading data from the binary file
with open(file_name, 'rb') as file:
data = pickle.load(file)
# Updating the age if the person exists
for person in data:
if person['name'] == name:
person['age'] = new_age
# Writing the updated data back to the binary file
with open(file_name, 'wb') as file:
pickle.dump(data, file)
print("Data updated successfully.")
# Update Alice's age to 26
update_age('data.bin', 'Alice', 26)
Program 6: Storing and Retrieving a Dictionary of Sets
import pickle
# Data to be stored in the binary file
data = {'set1': {1, 2, 3},
'set2': {4, 5, 6},
'set3': {7, 8, 9}}
# Writing data to the binary file
with open('data.bin', 'wb') as file:
pickle.dump(data, file)
print("Data written to the binary file successfully.")
# Reading data from the binary file
with open('data.bin', 'rb') as file:
loaded_data = pickle.load(file)
print("Data read from the binary file:")
print(loaded_data)
Program 7: Handling Errors while Loading Data from a
Binary File
import pickle
# Try to read data from a non-existing binary file
try:
with open('non_existing_file.bin', 'rb') as file:
data = pickle.load(file)
print("Data read successfully.")
except FileNotFoundError:
print("File not found.")
except pickle.UnpicklingError:
print("Error occurred while unpickling the data.")
Program 8: Deleting Data from a List of Custom Objects in
a Binary File
import pickle
# Define a function to delete a person from the list
def delete_person(file_name, name):
# Reading data from the binary file
with open(file_name, 'rb') as file:
data = pickle.load(file)
# Removing the person if found
data = [person for person in data if person.name !=
name]
# Writing the updated data back to the binary file
with open(file_name, 'wb') as file:
pickle.dump(data, file)
print("Data deleted successfully.")
# Delete Bob from the list
delete_person('data.bin', 'Bob')
These programs showcase various scenarios and operations you
can perform with binary files and pickle, such as updating data
in a list of dictionaries, storing and retrieving complex data
structures like dictionaries of sets, handling errors while loading
data, and deleting data from a list of custom objects.
Below is a program to delete a specific data item from a binary
file where the binary file stores the data of a list:
import pickle
# Function to delete a specific data item from the
binary file
def delete_data(file_name, item_to_delete):
try:
# Open the binary file in read mode
with open(file_name, 'rb') as file:
# Deserialize data from the binary file
data = pickle.load(file)
except FileNotFoundError:
print("File not found.")
return
except EOFError:
print("File is empty.")
return
# Check if the item exists in the list
if item_to_delete in data:
# Remove the item from the list
data.remove(item_to_delete)
# Open the binary file in write mode
with open(file_name, 'wb') as file:
# Serialize and write the updated data back
to the binary file
pickle.dump(data, file)
print(f"{item_to_delete} deleted successfully
from the binary file.")
else:
print(f"{item_to_delete} not found in the
binary file.")
# File name of the binary file
file_name = 'data.bin'
# Item to delete from the binary file
item_to_delete = 25 # Example: Deleting the item with
value 25 from the list
# Call the function to delete the specific data item
delete_data(file_name, item_to_delete)
This program reads the data from the binary file into a list,
deletes the specified data item from the list if it exists, and then
writes the updated list back to the binary file. If the specified
data item is not found in the list, it prints a message indicating
that the item was not found.
Below are some programs with explanatory comments to
elaborate on the csv module, csv.reader(), csv.writer(),
writerow(), and writerows():
Program 1: Writing Data to a CSV File
import csv
# Data to be written to the CSV file
data = [
['Name', 'Age', 'City'],
['John', 30, 'New York'],
['Alice', 25, 'Los Angeles'],
['Bob', 35, 'Chicago']
]
# Open the CSV file in write mode
with open('data.csv', 'w', newline='') as file:
# Create a CSV writer object
writer = csv.writer(file)
# Write data to the CSV file using writerows()
writer.writerows(data)
print("Data written to the CSV file successfully.")
Program 2: Reading Data from a CSV File
import csv
# Open the CSV file in read mode
with open('data.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Iterate over each row in the CSV file
for row in reader:
print(row)
Program 3: Writing Data to a CSV File Row by Row
import csv
# Data to be written to the CSV file
data = [
['Name', 'Age', 'City'],
['John', 30, 'New York'],
['Alice', 25, 'Los Angeles'],
['Bob', 35, 'Chicago']
]
# Open the CSV file in write mode
with open('data.csv', 'w', newline='') as file:
# Create a CSV writer object
writer = csv.writer(file)
# Write each row of data to the CSV file using
writerow()
for row in data:
writer.writerow(row)
print("Data written to the CSV file row by row
successfully.")
Program 4: Reading Data from a CSV File Row by Row
import csv
# Open the CSV file in read mode
with open('data.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Read each row of data from the CSV file
for row in reader:
print(row)
Program 5: Appending Data to an Existing CSV File
import csv
# Data to be appended to the CSV file
new_data = [
['Eva', 28, 'Houston'],
['Michael', 40, 'San Francisco']
]
# Open the CSV file in append mode
with open('data.csv', 'a', newline='') as file:
# Create a CSV writer object
writer = csv.writer(file)
# Append new data to the CSV file using writerows()
writer.writerows(new_data)
print("Data appended to the CSV file successfully.")