13 BSC CS - Python - Chapter 5
13 BSC CS - Python - Chapter 5
file.close()
Opening a File
In Python, files are opened using the open() Using with Statement for File Management
function, which returns a file object.
Instead of manually closing files, Python
When opening a file, you specify the file path offers the with statement for automatic file
and the mode in which you want to open it handling.
(e.g., read, write, append).
Using with, the file is automatically closed
The most commonly used modes include: once the code block inside with is executed.
E
● 'r': Read (default) – Opens the file for Example Code:
reading; file must exist.
EG
● 'w': Write – Opens the file for writing; with open("example.txt", "r") as
creates a new file if it doesn’t exist or
file:
truncates it if it does.
LL
● 'a': Append – Opens the file for content = file.read() #
appending data; creates a new file if it Perform file operations here
doesn’t exist.
O
# No need to close the file
● 'r+': Read and Write – Opens the file manually; it's done
C
for both reading and writing; file must automatically
exist.
EE
Syntax: Examples of Opening and Closing Files
R
"mode")
file.write("Hello, World!") #
# Opening a file named Writing to the file
.M
Closing a File
IG
Example Code:
# Open a file in read mode
file = open("output.txt", "r")
# Closing the file after opening
content = file.read() #
it
Reading the content
1
print(content) # Reading from a text file
file.close() # with open("example.txt", "r") as
Closing the file file:
print(file.read())
Example 4: Using with to Read a File
Automatically Writing to a Text File:
with open("output.txt", "r") as When writing to a file using 'w' mode, the file
file: is created if it doesn’t exist or truncated if it
does.
for line in file:
print(line.strip()) # Use 'a' mode to append without overwriting.
Printing each line
E
Example Code:
EG
Example 5: Handling File Not Found Error
# Writing to a text file
with open("example.txt", "w") as
LL
try:
with file:
open("non_existent_file.txt", file.write("Hello, Python!")
O
"r") as file:
C
print(file.read()) Appending to a Text File:
except FileNotFoundError:
EE
print("File not found! # Appending data to a text file
Please check the file path.") with open("example.txt", "a") as
R
file:
Working with Text Files file.write("\nAppending a
EG
new line.")
Text files store data in human-readable format
and are typically used for storing plain text.
.D
Content is usually accessed or manipulated as Binary files are accessed using modes such as
strings.
IG
file.
● .readline(): Reads one line at a time.
● .readlines(): Reads all lines and with open("image.png", "rb") as
returns them as a list. file:
binary_data = file.read()
Syntax:
Example Code:
with open("example.txt", "r") as
file:
# Reading binary data
content = file.read() #
with open("binary_file.dat",
Reads all content at once
"rb") as file:
data = file.read()
Example Code:
2
print(data) as file:
binary_content =
Writing to a Binary File: file.read(20) # Read the first
20 bytes
Syntax: print(binary_content)
E
"wb") as file:
# Writing binary data file.write(data)
EG
with open("binary_file.dat",
"wb") as file: Example 5: Copying a Binary File
LL
data = b"This is binary
data" # Copy a binary file
file.write(data)
O
with open("source_image.jpg",
"rb") as src_file:
C
Examples of Working with Text and Binary with open("copy_image.jpg",
Files "wb") as dest_file:
EE
Example 1: Reading and Printing Each Line
in a Text File dest_file.write(src_file.read())
R
EG
print(line.strip()) #
Print each line without extra Serialization (also known as "pickling") is the
.M
3
import pickle This is useful for tracking where the pointer is
in the file and understanding where data is
# Pickling a Python dictionary being read or written.
data = {"name": "Alice", "age":
25, "city": "New York"} Example Code:
with open("data.pkl", "wb") as
file: with open("example.txt", "rb")
pickle.dump(data, file) # as file:
Serializing data file.seek(10) #
# Unpickling the data Move pointer 10 bytes from the
with open("data.pkl", "rb") as start
file: print(file.tell()) #
loaded_data = Output: 10 (current position)
E
pickle.load(file) # data = file.read(5) #
EG
Deserializing data Read the next 5 bytes
print(loaded_data) print(file.tell()) #
LL
Output: 15 (new position after
seek() Method reading)
O
The seek() method allows you to move the file
Examples of Pickle, seek(), and tell()
pointer to a specific position within the file.
C
Example 1: Pickling and Unpickling a List
This is useful when you want to navigate to a
EE
particular part of a file to read or write from
that point onward. import pickle
# List to be pickled
R
# Pickling
offset: Number of bytes to move the pointer. with open("numbers.pkl", "wb")
as file:
.D
4
with open("sample.txt", "r") as Random access allows you to read from or
write to any part of a binary file without
file: having to read it sequentially from the
print("Initial position:", beginning.
file.tell())
file.read(10) This is achieved using the seek() method,
print("Position after which moves the file pointer to the specified
location.
reading 10 characters:",
file.tell()) It is useful when working with large files, as
you can directly access specific data without
Example 4: Reading File in Reverse Order reading the entire file.
Using seek()
Example Code:
E
with open("sample.txt", "rb") as
EG
# Writing to a binary file for
file:
demonstration
file.seek(0, 2) #
with open("example.bin", "wb")
LL
Move to the end of the file
as file:
position = file.tell() #
Get end position
O
file.write(b"1234567890ABCDEF")
while position > 0:
# Random access: Reading
C
position -= 1
specific parts of the file
file.seek(position)
EE
with open("example.bin", "rb")
as file:
print(file.read(1).decode(),
file.seek(4) #
R
end="")
Move to the 5th byte
EG
print(file.read(2)) #
Example 5: Using Pickle to Save and Load a
Output: b'56'
Complex Object
file.seek(-6, 2) #
.D
print(file.read(4)) #
def __init__(self, name,
Output: b'CDEF'
age):
.P
self.name = name
Zipping and Unzipping Files
IG
self.age = age
person = Person("Alice", 30)
Python’s zipfile module allows for
# Saving the person object compressing and decompressing files.
R
E
zipfile.ZipFile("archive.zip", with open("example.bin", "rb+")
EG
"r") as zipf: as file:
file.seek(5) # Move
LL
zipf.extractall("extracted_files to the 6th byte
") # Extract all files into a file.write(b"Python") #
directory Overwrite from the 6th byte
O
file.seek(0)
C
Working with Directories print(file.read()) #
Output: b'HelloPython'
EE
The os module in Python provides methods for
interacting with directories, including creating,
renaming, listing contents, and deleting Example 2: Adding Multiple Files to a Zip
Archive
R
directories.
EG
"w") as zipf:
● os.rename(): Renames a file or
files_to_zip =
directory.
● os.rmdir(): Removes an empty ["example.bin", "sample.txt"]
.M
import os
with zipfile.ZipFile("data.zip",
import shutil
SH
"r") as zipf:
zipf.extract("sample.txt",
# Creating a new directory
"extracted_specific") #
os.mkdir("my_directory")
Extracts only sample.txt
# Listing contents of a
directory
print("Contents of current Example 4: Creating a Nested Directory
Structure
directory:", os.listdir("."))
import os
# Renaming the directory
os.makedirs("parent_dir/child_di
os.rename("my_directory",
r", exist_ok=True)
"renamed_directory")
print("Directories created:",
6
os.listdir("parent_dir")) Example Code:
import re
Example 5: Copying and Removing a
Directory text = "Welcome to Python
programming!"
# Searching for the word
import shutil
"Python"
# Copy directory
match = re.search("Python",
shutil.copytree("parent_dir",
text)
"backup_dir")
if match:
# Remove directory
print("Pattern found:",
shutil.rmtree("parent_dir")
match.group())
E
else:
Introduction to Regular Expressions
EG
print("Pattern not found.")
Regular Expressions (RegEx) are sequences of
characters that form a search pattern.
LL
They are widely used for string matching and Sequence Characters in Regular
manipulation, such as finding, replacing, or
O
Expressions
validating specific patterns within text.
C
Sequence characters (also known as
Python provides the re module to work with metacharacters) allow you to specify patterns
regular expressions.
EE
in regular expressions.
○ re.search(): Searches for a pattern within a meaning and can be used to create complex
string and returns the first match. search patterns.
EG
pattern in a string.
○ re.sub(): Replaces occurrences of a pattern
with a specified string.
.M
7
+ One or more occurrences ab+ matches ab, abb, etc., but not a
{m,n} Matches between m and n repetitions a{2,4} matches aa, aaa, or aaaa
E
Example Code: print("End match:",
EG
match.group() if match else "No
import re match")
text = "Hello, world! Hello
LL
# Finding all occurrences of
again!" 'Hello' in the text
# Matching words that start with matches = re.findall(r"Hello",
O
'H' at the beginning of the text)
C
string print("All matches:", matches)
match = re.search(r"^Hello",
EE
text)
Special Characters in Regular Expressions
print("Start match:",
match.group() if match else "No Special characters, also called metacharacters,
R
pattern matching.
text)
.M
.P
8
\W Matches any non-alphanumeric character \W+ matches "!@#" in "Hello!@#"
\s Matches any whitespace character (space, \s+ matches " " in "Hello world"
tab)
E
[] Matches any one character within the [aeiou] matches vowels in "hello"
brackets
EG
LL
[^ ] Matches any one character not in brackets [^aeiou] matches consonants in "hello"
O
() Groups a pattern together (abc)+ matches "abcabc" in "abcabcxyz"
C
EE
` ` Acts as an OR operator
R
EG
'N']
123, Another number: 456."
Using Regular Expressions on Files
R
E
content = file.read() HTML files often contain structured data,
EG
# Find all email addresses in which you may want to extract, such as text
the file within specific tags (e.g., <title>, <a>, <p>
emails = tags).
LL
re.findall(r"[a-zA-Z0-9._%+-]+@[
While Python offers libraries like
a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", BeautifulSoup for HTML parsing, you can
O
content) also use regular expressions for simpler cases.
print("Emails found:", emails)
C
# Replace phone numbers with a Regular expressions allow you to match
patterns within HTML files, like retrieving
EE
placeholder
content within tags or finding specific
updated_content =
attributes.
re.sub(r"\d{3}-\d{3}-\d{4}",
R
attributes.
Example 1: Extracting Hashtags from a File 3. Extract Information: Use functions
like re.findall() to capture desired data.
.P
with open("social_posts.txt",
import re
"r") as file:
# Sample HTML content for
R
posts = file.read()
demonstration
SH
hashtags = re.findall(r"#\w+",
html_content = """
posts)
<html>
print("Hashtags found:",
<head><title>Sample
hashtags)
Page</title></head>
<body>
Example 2: Replacing Dates in a File <h1>Welcome to My
Website</h1>
# Replace all dates (format: <p>This is a <a
DD/MM/YYYY) with "DATE" href="https://example.com">link<
placeholder /a> to an example site.</p>
with open("events.txt", "r") as <p>Contact us at <a
file: href="mailto:contact@example.com
10
">contact@example.com</a>.</p>
</body>
</html>
"""
# Extract title content
title =
re.findall(r"<title>(.*?)</title
>", html_content)
print("Title:", title[0] if
title else "No title found")
# Extract all hyperlinks
links =
E
re.findall(r'href="(http.*?)"',
EG
html_content)
print("Links:", links)
LL
# Extract all email addresses
emails =
re.findall(r'href="mailto:(.*?)"
O
', html_content)
C
print("Emails:", emails)
EE
Explanation of Key Patterns:
"http".
● href="mailto:(.*?)": Matches email
.M
Examples:
paragraphs =
R
re.findall(r"<p>(.*?)</p>",
SH
html_content)
print("Paragraphs:", paragraphs)
heading =
re.findall(r"<h1>(.*?)</h1>",
html_content)
print("H1 Tag Content:",
heading)
11