Python Diaries ….. Day 11
File Handling 📑 📑 📑
Welcome to Day 11 of Python Diaries! Today, we’re delving into the concept of file handling. File handling in Python is a fundamental aspect of working with data on a computer. It allows you to interact with files stored on a storage medium, such as a hard drive. This interaction can involve tasks like reading from files to retrieve data, writing to files to store information, and modifying existing files.
Python provides a versatile set of tools and functions for file handling, making it relatively straightforward to perform various operations on files. These operations include opening files, reading their contents, writing new data, appending to existing files, and more.
Files can contain a wide range of information, from plain text documents to images, databases, spreadsheets, and more. Python’s file handling capabilities are designed to accommodate these various types of data, making it a powerful tool for tasks like data analysis, data manipulation, and automation.
Remember that it’s important to handle files responsibly. Always ensure that you close files after you’re done with them to prevent any potential data loss or corruption. Additionally, be mindful of file permissions and handle exceptions appropriately to account for potential errors that may occur during file operations.
Opening and Closing Files
You can open a file using the open()
function. It takes two arguments: the file path and the mode . Once you're done with a file, it's important to close it using close()
.
Modes:
'r'
: Read mode. Allows you to read the contents of a file.'w'
: Write mode. Allows you to write data to a file. It will overwrite the existing content.'a'
: Append mode. Allows you to add data to the end of an existing file.'b'
: Binary mode. Used for working with non-text files like images or executables.'x'
: Exclusive creation. Creates a new file but raises an error if the file already exists.
# Opening a file for reading
file = open('example.txt', 'r')
# Opening a file for writing (creates a new file if it doesn't exist, and truncates if it does)
file = open('example.txt', 'w')
# Opening a file for appending (appends to the end of an existing file)
file = open('example.txt', 'a')
# Opening a file in binary mode
file = open('example.txt', 'rb')
# Closing a file
file.close()
Reading from Files
You can read from a file using the read()
method. It reads the entire contents of the file, or you can specify a number of bytes to read.
file = open('example.txt', 'r')
content = file.read() # Reads the entire file
line = file.readline() # Reads a single line
lines = file.readlines() # Reads all lines and returns a list
print(content)
file.close()
Writing to Files
You can write to a file using the write()
method. This will replace the existing content with the new content.
file = open('example.txt', 'w')
file.write('Hello, World!')
file.close()
Appending to Files
If you want to add content to an existing file without overwriting it, you can open it in append mode and use the write()
method.
file = open('example.txt', 'a')
file.write('\nThis is an appended line.')
file.close()
Working with Binary Files
Binary files handle non-text files like images, audio, etc. Use the ‘rb’ mode for reading and ‘wb’ mode for writing.
# Reading a binary file
with open('binary_file.jpg', 'rb') as file:
content = file.read()
# Writing to a binary file
with open('new_binary_file.jpg', 'wb') as file:
file.write(content)
Using with
Statement
The with
statement automatically takes care of closing the file after you're done with it.
with open('example.txt', 'r') as file:
content = file.read()
print(content)
# File is automatically closed here
File Positions
Every time you read from or write to a file, the “cursor” advances. You can change the position using the seek()
method.
with open('example.txt', 'r') as file:
content = file.read(10) # Reads the first 10 characters
file.seek(0) # Move cursor back to the beginning
content = file.read(10) # Reads the next 10 characters
Working with Directories
Python’s os
module provides functions for interacting with the file system.
import os
# Create a directory
os.mkdir('my_directory')
# List files in a directory
files = os.listdir('my_directory')
print(files)
# Remove a file
os.remove('my_file.txt')
# Remove an empty directory
os.rmdir('my_directory')
Using the os
Module for File Operations
The os
module provides various file operations.
import os
# Rename a file
os.rename('old_name.txt', 'new_name.txt')
# Get the current working directory
cwd = os.getcwd()
# Change the working directory
os.chdir('/path/to/directory')
# Check if a file exists
os.path.exists('file.txt')
File path
A file path is a string of characters that specifies the location of a file or directory on a computer’s file system. It provides a unique address that allows the operating system to locate and access the file or directory. There are two main types of file paths:
Absolute Paths:
An absolute path provides the complete address or location of a file or directory starting from the root directory. It includes all the parent directories leading up to the file or directory.
- Example (Windows):
C:\Users\John\Documents\file.txt
- Example (Unix/Linux):
/home/John/Documents/file.txt
Relative Path:
A relative path specifies the location of a file or directory relative to the current working directory. It does not include the entire path from the root directory.
- Example: If the current working directory is
/home/John/Documents
, thenfile.txt
would be a relative path.
There are some other types of file paths.
UNC Path (Windows):
UNC (Universal Naming Convention) paths are used in Windows networks to identify shared resources. They start with \\
followed by the name of the computer or server and then the path to the shared folder.
- Example:
\\ServerName\SharedFolder\file.txt
URL Path:
A URL (Uniform Resource Locator) is a web address that specifies the location of a resource on the internet. It includes the protocol (like http
or https
), domain name, and the path to the resource.
Environment Variable Path:
Some systems use environment variables to represent file paths, allowing for easier configuration and portability.
- For example, in Unix/Linux,
$HOME
represents the home directory of the current user.
Special Paths:
Some systems have special paths that represent common directories.
- Example:
.
refers to the current directory...
refers to the parent directory.~
(tilde) represents the home directory of the current user in Unix-like systems.
Network Path:
In some operating systems, networked drives or shares can be accessed using a network path.
- Example (Windows):
\\ServerName\SharedFolder\file.txt
Device Path:
In Unix-like systems, device files are used to access hardware devices. The path to a device file starts with /dev/
.
- Example:
/dev/sda
refers to the first hard drive.
Raw Path:
In some contexts, you might encounter raw file paths that are not interpreted by the system. These paths are provided as they are.
- Example:
\\?\C:\Windows\System32
Path with Spaces:
Some paths may contain spaces. To properly handle them, you might need to enclose the path in quotes.
- Example:
"C:\Program Files\Application\file.txt"
Path separators
Forward Slash ( / ):
This is used as the path separator in Unix-like operating systems, including Linux and macOS.
- Example:
/home/user/documents/file.txt
Backslash ( \ ):
This is used as the path separator in Windows operating systems.
- Example:
C:\Users\User\Documents\file.txt
The choice of path separator is a convention that has developed in each operating system:
- In Unix-like systems, the forward slash (/) is used because it doesn’t conflict with other characters and is compatible with the convention used in URLs and many programming languages (including Python).
- In Windows, the backslash () is used partly due to historical reasons and to differentiate it from the forward slash, which is commonly used in command-line options.
It’s worth noting that in Python, you can often use either forward slashes or double backslashes in file paths on Windows.
However, for better portability and to avoid potential issues, it’s recommended to use os.path.join()
or pathlib.Path
to construct file paths. These functions will automatically use the appropriate path separator based on the operating system, making your code more platform-independent.
This tutorial should give you a solid foundation in Python file handling. Remember to always close your files after you’re done with them and handle errors appropriately. Happy coding!
You can access the other topics in this tutorial series right here:
- Day 1: Data types, Comment, Type casting and User input.
- Day 2: Numbers, Strings, and Booleans.
- Day 3: Operators
- Day 4: Lists
- Day 5: Tuples and Sets
- Day 6: Dictionaries
- Day 7: If , If Else, If elif else Condition statements
- Day 8: While loops
- Day 9: For loops
- Day 10: Functions
- Day 11: File handling
- Day 12: Error Handling
- Day 13: Regular expressions
- Day 14: OOPS in python (class)
- Day 15: Python GUI libraries
🥲In the next session we will discuss about Error handling in python.