Mastering File System Navigation with pathlib
The pathlib module in Python provides an object-oriented approach to working with file system paths. It simplifies common file operations like creating, deleting, moving, and querying files and directories, making your code more readable and less prone to errors compared to traditional string-based path manipulation. This challenge will test your ability to leverage pathlib for efficient and robust file system interactions.
Problem Description
Your task is to create a Python script that utilizes the pathlib module to perform a series of file system operations. You will be given a base directory, and you need to create a specific directory structure within it, create some sample files, and then perform various checks and manipulations on these files and directories.
Key Requirements:
- Directory Creation: Create a nested directory structure:
base_dir/project/data/raw. - File Creation: Inside the
rawdirectory, create two empty files:data_01.csvandlog.txt. - Path Existence Check: Verify that all created directories and files exist.
- File Renaming: Rename
data_01.csvtoprocessed_data_01.csv. - File Copying: Copy
log.txtto theprojectdirectory. - Directory Listing: List all files (not directories) within the
project/datadirectory. - File Deletion: Delete the original
log.txtfile. - Cleanup: Ensure all created directories and files are removed after the operations are completed (this is crucial for testing and preventing clutter).
Expected Behavior:
Your script should execute these operations sequentially and report the status of each significant step (e.g., "Directory created successfully," "File not found," "Operation completed"). For the listing of files, it should print the names of the files found.
Edge Cases to Consider:
- What if the
base_diralready exists? Your script should handle this gracefully. - What if some of the files or directories you expect to find or delete don't exist due to a previous error?
Examples
Example 1:
- Scenario: A clean execution starts.
- Input: A
base_dirpath that does not contain the specified structure. - Output: A series of print statements confirming successful creation, renaming, copying, listing, and deletion. The final state of the
base_dirshould be empty. - Explanation: The script successfully creates the nested directories, adds the files, performs the requested manipulations, and finally cleans up all created artifacts.
Example 2:
- Scenario: Attempting to rename a file that doesn't exist.
- Input: Assume the
data_01.csvfile was accidentally deleted before the renaming step. - Output: A print statement indicating that the file to be renamed was not found. Subsequent operations should proceed if possible, or report further issues gracefully.
- Explanation: The script should detect the missing file and report the error without crashing.
Constraints
- The
base_dirwill be a valid, writeable directory path provided as a string. - Your script should not rely on any external libraries beyond Python's standard library, specifically
pathlib. - The script should be efficient and avoid unnecessary I/O operations.
- All created files and directories must be cleaned up at the end of the script's execution.
Notes
- Start by importing the
Pathclass from thepathlibmodule. - Remember that
pathlibobjects have convenient methods for creating directories (mkdir), creating empty files (touch), checking existence (exists), renaming (rename), copying (copy), listing contents (iterdir), and deleting (unlinkfor files,rmdirfor empty directories, orglobfor more complex pattern-based deletion). - For recursive deletion of directories and their contents, consider using
shutil.rmtreeif you're allowed to useshutil, or implement a recursive deletion logic yourself if strictly limited topathlib(though this can be more complex). For this challenge, assumingshutilis acceptable for cleanup is a good starting point ifpathlibalone proves too restrictive for recursive deletion. However, try to accomplish as much as possible withpathlibfirst. - Pay close attention to the return values of
pathlibmethods and how to handle potential exceptions.