Working with Files and Directories
File Management Operations (Copying, Moving, Deleting)
This topic focuses on file management operations in Python, specifically copying, moving, and deleting files. You'll learn how to perform these operations using the built-in functions and modules available in Python. This topic covers the essential techniques and considerations for managing files in your Python programs.
YouTube Video Link: File Management Operations in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.
Examples
Example 1: Copying a File
import shutil
source = "path/to/source/file.txt"
destination = "path/to/destination/file.txt"
shutil.copy(source, destination)Example 2: Moving a File
import shutil
source = "path/to/source/file.txt"
destination = "path/to/destination/file.txt"
shutil.move(source, destination)Example 3: Deleting a File
Example 4: Copying a Directory
Example 5: Moving a Directory
Example 6: Deleting a Directory
Exercises
Exercise 1: Write a program that prompts the user to enter the path of a file and copies it to a specified destination folder.
Exercise 2:
Create a function called move_files that takes a source directory and a destination directory as input. The function should move all files from the source directory to the destination directory.
Exercise 3: Write a program that asks the user to enter the path of a file and deletes it if it exists.
Exercise 4:
Create a function called copy_directory that takes a source directory and a destination directory as input. The function should copy all files and subdirectories from the source directory to the destination directory.
Exercise 5: Write a program that prompts the user to enter the path of a directory and deletes it if it exists, along with all its files and subdirectories.
Navigating Directory Structures
This topic focuses on navigating directory structures in Python. It covers how to work with directories and paths, including listing directory contents, navigating through directories, and performing operations on files within directories. You'll learn how to use the built-in modules and functions in Python to efficiently navigate and manipulate directory structures.
YouTube Video Link: Navigating Directory Structures in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.
Examples
Example 1: Listing Files in a Directory
Example 2: Checking if a Path is a Directory
Example 3: Navigating Through Directories
Example 4: Creating a Directory
Example 5: Removing a Directory
Exercises
Exercise 1: Write a program that prompts the user to enter a directory path and displays all the subdirectories and files within that directory.
Exercise 2:
Create a function called count_files that takes a directory path as input and returns the total number of files in that directory and its subdirectories.
Exercise 3: Write a program that asks the user to enter a directory path and lists all the files (excluding directories) in that directory and its subdirectories.
Exercise 4:
Create a function called find_files that takes a directory path and a file extension as input. The function should return a list of all the files in the directory and its subdirectories that have the specified file extension.
Exercise 5: Write a program that prompts the user to enter a directory path and displays the size (in bytes) of each file in the directory and its subdirectories.
Working with File Metadata (Permissions, Timestamps)
This topic focuses on working with file metadata in Python, specifically permissions and timestamps. You'll learn how to retrieve and modify the permissions of a file, as well as access and manipulate the various timestamps associated with a file, such as the creation time, modification time, and access time. Understanding and working with file metadata is essential for managing and tracking file properties in your Python programs.
YouTube Video Link: Working with File Metadata in Python - Please note that the provided link is a placeholder. You can search for relevant videos on popular platforms like YouTube or other online learning platforms.
Examples
Example 1: Retrieving File Permissions
Example 2: Modifying File Permissions
pythonCopy
Example 3: Retrieving File Timestamps
Example 4: Modifying File Timestamps
Example 5: Checking if a File is Readable/Writable/Executable
pythonCopy
Exercises
Exercise 1: Write a program that prompts the user to enter a file path and displays the permissions of the file in human-readable format (e.g., "readable", "writable", "executable") for the owner, group, and others.
Exercise 2:
Create a function called change_permissions that takes a file path and a permission code as input. The function should modify the permissions of the file based on the provided permission code.
Exercise 3: Write a program that asks the user to enter a file path and displays the creation time, modification time, and access time of the file in a readable format.
Exercise 4:
Create a function called set_timestamp that takes a file path, a timestamp type (e.g., "access", "modification"), and a timestamp value as input. The function should modify the respective timestamp of the file to the provided value.
Exercise 5: Write a program that prompts the user to enter a file path and checks if the file is executable.
Using the os and shutil modules
osModule:The
osmodule provides a way to interact with the operating system in a platform-independent manner.It includes functions for working with files, directories, and other operating system-related tasks.
Some commonly used functions from the
osmodule are:os.getcwd(): Returns the current working directory.os.listdir(path): Returns a list of files and directories in the specified path.os.path.join(path, *paths): Joins one or more path components intelligently.os.path.exists(path): Checks if a path exists.os.mkdir(path): Creates a new directory.os.remove(path): Removes a file.os.rename(src, dst): Renames a file or directory.os.chmod(path, mode): Changes the permissions of a file or directory.os.stat(path): Returns information about a file.
Example:
shutilModule:The
shutilmodule provides high-level file operations and utilities.It simplifies tasks such as copying files and directories, moving files, and removing directories.
Some commonly used functions from the
shutilmodule are:shutil.copy(src, dst): Copies a file from the source path to the destination path.shutil.copy2(src, dst): Copies a file and preserves its metadata (timestamps, permissions) in the destination.shutil.move(src, dst): Moves a file or directory from the source path to the destination path.shutil.rmtree(path): Removes a directory and all its contents.shutil.make_archive(base_name, format, root_dir): Creates an archive file (e.g., zip, tar) from a directory.
Example
Last updated