Hone logo
Hone
Problems

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:

  1. Directory Creation: Create a nested directory structure: base_dir/project/data/raw.
  2. File Creation: Inside the raw directory, create two empty files: data_01.csv and log.txt.
  3. Path Existence Check: Verify that all created directories and files exist.
  4. File Renaming: Rename data_01.csv to processed_data_01.csv.
  5. File Copying: Copy log.txt to the project directory.
  6. Directory Listing: List all files (not directories) within the project/data directory.
  7. File Deletion: Delete the original log.txt file.
  8. 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_dir already 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_dir path 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_dir should 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.csv file 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_dir will 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 Path class from the pathlib module.
  • Remember that pathlib objects have convenient methods for creating directories (mkdir), creating empty files (touch), checking existence (exists), renaming (rename), copying (copy), listing contents (iterdir), and deleting (unlink for files, rmdir for empty directories, or glob for more complex pattern-based deletion).
  • For recursive deletion of directories and their contents, consider using shutil.rmtree if you're allowed to use shutil, or implement a recursive deletion logic yourself if strictly limited to pathlib (though this can be more complex). For this challenge, assuming shutil is acceptable for cleanup is a good starting point if pathlib alone proves too restrictive for recursive deletion. However, try to accomplish as much as possible with pathlib first.
  • Pay close attention to the return values of pathlib methods and how to handle potential exceptions.
Loading editor...
python