Hone logo
Hone
Problems

Simulating Core OS Module Functionality in Python

This challenge asks you to implement a subset of the Python os module's core functionalities. The goal is to understand how operating system interactions are abstracted and to gain a deeper appreciation for the underlying system calls. You'll be building simplified versions of functions that handle file system operations and process management.

Problem Description

You are tasked with creating a module named my_os that mimics some of the essential functions found in Python's built-in os module. Specifically, you need to implement the following:

  1. my_os.makedirs(path, exist_ok=False): This function should create directories recursively. If exist_ok is False (the default), it should raise a FileExistsError if the target directory already exists. If exist_ok is True, it should do nothing if the directory already exists.

  2. my_os.remove(path): This function should remove a file. It should raise a FileNotFoundError if the file does not exist.

  3. my_os.rename(src, dst): This function should rename a file or directory from src to dst. It should raise a FileNotFoundError if src does not exist and a FileExistsError if dst already exists.

  4. my_os.getcwd(): This function should return the current working directory as a string. Assume the current working directory is initially "/".

  5. my_os.chdir(path): This function should change the current working directory to path. The path can be a relative path (e.g., "subdir") or an absolute path (e.g., "/newdir"). It should raise a FileNotFoundError if the directory does not exist.

You should implement these functions using only basic Python data structures (lists, dictionaries) and control flow statements. Do not use the actual os module functions. You can simulate the file system using a dictionary where keys are paths (strings) and values are either None (for directories) or a string (for files).

Examples

Example 1:

Input:
my_os.makedirs("/a/b/c", exist_ok=False)
my_os.remove("/a/b/c")
Output: None
Explanation: Creates the directories /a, /a/b, and /a/b/c. Then removes /a/b/c.

Example 2:

Input:
my_os.makedirs("/a/b", exist_ok=True)
my_os.makedirs("/a/b", exist_ok=True)
Output: None
Explanation: Creates /a/b if it doesn't exist. The second call does nothing because /a/b already exists.

Example 3:

Input:
my_os.chdir("/a/b")
print(my_os.getcwd())
Output: /a/b
Explanation: Changes the current working directory to /a/b.

Example 4: (Edge Case)

Input:
my_os.remove("/nonexistent_file")
Output: FileNotFoundError
Explanation: Attempts to remove a file that doesn't exist, raising the expected error.

Constraints

  • The file system will be represented as a dictionary.
  • Paths will be strings, using "/" as a separator.
  • All paths will be absolute paths.
  • The current working directory will be stored as a string.
  • You are not allowed to use the os module directly.
  • Error messages should match the standard Python error types (e.g., FileNotFoundError, FileExistsError).

Notes

  • Think about how to represent the file system structure using a dictionary.
  • Consider how to handle relative paths when changing the current working directory.
  • Pay close attention to the error handling requirements.
  • Start with makedirs and then build upon it for the other functions. makedirs is the foundation for many of the other operations.
  • The initial current working directory is "/".
  • Assume that all operations are performed in the same process. You don't need to worry about concurrency or multi-threading.
Loading editor...
python