Hone logo
Hone
Problems

Mastering the OS Module: File and Directory Operations

The os module in Python is a powerful tool for interacting with the operating system, providing a portable way to use operating system dependent functionality. This challenge will test your ability to use key functions within the os module for common file and directory management tasks, a fundamental skill for any Python developer working with system-level operations.

Problem Description

Your task is to implement a Python script that performs several common file and directory operations using the os module. You will need to create directories, list their contents, rename files, and remove files and directories. The script should be robust enough to handle potential errors gracefully.

Key Requirements:

  1. Directory Creation: Create a new directory.
  2. File Creation: Create a new empty file within a specified directory.
  3. Directory Listing: List all files and subdirectories within a given directory.
  4. File Renaming: Rename an existing file.
  5. File Deletion: Remove a specified file.
  6. Directory Deletion: Remove an empty directory.
  7. Error Handling: Implement basic error handling for operations that might fail (e.g., trying to create a directory that already exists, deleting a non-existent file).

Expected Behavior:

The script should accept commands or inputs that specify the operation to perform, along with the necessary arguments (e.g., directory name, file name). It should then execute the operation and provide feedback to the user, indicating success or failure.

Edge Cases to Consider:

  • Attempting to create a file or directory that already exists.
  • Attempting to delete a file or directory that does not exist.
  • Attempting to rename a file to a name that already exists.
  • Attempting to delete a non-empty directory (this should ideally be handled, perhaps by refusing or by using a recursive delete if specified). For this challenge, focus on deleting empty directories.
  • Permissions issues (though for simplicity, assume sufficient permissions).

Examples

Example 1: Creating and Listing

Initial State:
- No 'my_test_dir' directory exists.

Input Operations:
1. Create directory: 'my_test_dir'
2. Create file: 'my_test_dir/example.txt'
3. List directory contents: 'my_test_dir'

Expected Output:
Directory 'my_test_dir' created successfully.
File 'my_test_dir/example.txt' created successfully.
Contents of 'my_test_dir': ['example.txt']

Example 2: Renaming and Deleting

Initial State:
- Directory 'my_test_dir' exists.
- File 'my_test_dir/example.txt' exists.

Input Operations:
1. Rename file: 'my_test_dir/example.txt' to 'my_test_dir/renamed_example.txt'
2. Delete file: 'my_test_dir/renamed_example.txt'
3. Delete directory: 'my_test_dir'

Expected Output:
File 'my_test_dir/example.txt' renamed to 'my_test_dir/renamed_example.txt' successfully.
File 'my_test_dir/renamed_example.txt' deleted successfully.
Directory 'my_test_dir' deleted successfully.

Example 3: Handling Existing Directories/Files

Initial State:
- Directory 'my_test_dir' exists.
- File 'my_test_dir/existing_file.txt' exists.

Input Operations:
1. Create directory: 'my_test_dir'
2. Create file: 'my_test_dir/existing_file.txt'

Expected Output:
Error: Directory 'my_test_dir' already exists.
Error: File 'my_test_dir/existing_file.txt' already exists.

Constraints

  • The script should use functions from Python's built-in os module.
  • All file and directory paths should be treated as relative paths.
  • The script should handle up to 10 operations in a single execution flow for testing purposes.
  • The maximum depth of directory nesting to consider is 2 levels (e.g., dir1/dir2).

Notes

  • Focus on the core os module functions: os.mkdir(), os.makedirs(), os.path.exists(), os.listdir(), os.rename(), os.remove(), os.rmdir().
  • Consider using os.path.join() for constructing paths to ensure cross-platform compatibility.
  • For creating empty files, you can open them in write mode ('w') and immediately close them.
  • When deleting directories, remember that os.rmdir() only works on empty directories. You might need to check for emptiness first or handle the OSError if it's not empty.
  • Think about how you will structure your code to accept and process the different operations. A function-based approach for each operation would be beneficial.
  • The primary goal is to demonstrate proficiency with the os module's file and directory manipulation capabilities.
Loading editor...
python