Chapter 9: Working with Files and Directories
9.1 Introduction to
File and Directory Operations
9.1.1 Understanding
file paths and directory structures
Explanation: File paths
specify the location of a file or directory in the file system. Understanding
file paths and directory structures is crucial for working with files and
directories in Python.
Example:
python
///Example
file_path =
"path/to/file.txt"
directory_path =
"path/to/directory/"
9.1.2 Navigating and
manipulating file systems
Explanation: Python
provides various functions and methods to navigate and manipulate the file
system, such as creating, moving, renaming, and deleting files and directories.
Example:
python
///Example
import os
# Creating a directory
os.mkdir("new_directory")
# Renaming a file
os.rename("old_file.txt",
"new_file.txt")
# Deleting a directory
os.rmdir("old_directory")
9.1.3 Working with file
and directory permissions
Explanation: File and
directory permissions control who can read, write, or execute files and
directories. Python provides functions to set and retrieve file permissions.
Example:
python
///Example
import stat
# Setting file
permissions
os.chmod("file.txt",
stat.S_IRUSR | stat.S_IWUSR) # Read and
write permissions for the owner
# Retrieving file
permissions
permissions =
stat.S_IMODE(os.lstat("file.txt").st_mode)
print(oct(permissions)) # Display permissions in octal format
9.2 File Operations
9.2.1 Opening and
closing files
Explanation: Files are
opened using the open() function, which returns a file object. Files should be
closed using the close() method to free system resources.
Example:
python
///Example
file =
open("file.txt", "r")
# Open file in read mode
content = file.read()
print(content)
file.close() # Close the file
9.2.2 Reading file
content
Explanation: File
content can be read using methods such as read(), readline(), or readlines().
These methods provide different ways to read the content of a file.
Example:
python
///Example
file =
open("file.txt", "r")
content =
file.read() # Read entire content as a
string
print(content)
file.seek(0) # Move the file pointer to the beginning
line =
file.readline() # Read a single line
print(line)
file.seek(0)
lines =
file.readlines() # Read all lines and
store them in a list
print(lines)
file.close()
9.2.3 Writing to files
Explanation: Files can
be opened in write mode to write content to them. The write() method is used to
write data to the file.
Example:
python
///Example
file =
open("file.txt", "w")
# Open file in write mode
file.write("Hello,
World!") # Write content to the
file
file.close()
9.3 Directory
Operations
9.3.1 Creating
directories
Explanation:
Directories can be created using the mkdir() function or the os.makedirs()
function. They provide a way to organize files into structured hierarchies.
Example:
python
///Example
import os
os.mkdir("new_directory") # Create a new directory
os.makedirs("new_directory/sub_directory") # Create nested directories
9.3.2 Listing directory
contents
Explanation: Python
provides functions to list the contents of a directory, including files and
subdirectories. These functions help in exploring and working with directory
structures.
Example:
python
///Example
import os
files =
os.listdir("directory") # Get
a list of files and directories in the specified directory
for file in files:
print(file)
9.3.3 Removing
directories
Explanation:
Directories can be removed using the rmdir() function or the os.removedirs()
function. Removing a directory deletes it along with its contents.
Example:
python
///Example
import os
os.rmdir("directory") # Remove an empty directory
os.removedirs("directory/sub_directory") # Remove nested directories
This chapter covers
file and directory operations in Python. Understanding how to navigate,
manipulate, and interact with files and directories is essential for many
programming tasks. The examples provided demonstrate the usage and syntax of
each topic, helping readers understand how to work with files and directories
in their own programs.
0 comments:
Post a Comment