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:
-
my_os.makedirs(path, exist_ok=False): This function should create directories recursively. Ifexist_okisFalse(the default), it should raise aFileExistsErrorif the target directory already exists. Ifexist_okisTrue, it should do nothing if the directory already exists. -
my_os.remove(path): This function should remove a file. It should raise aFileNotFoundErrorif the file does not exist. -
my_os.rename(src, dst): This function should rename a file or directory fromsrctodst. It should raise aFileNotFoundErrorifsrcdoes not exist and aFileExistsErrorifdstalready exists. -
my_os.getcwd(): This function should return the current working directory as a string. Assume the current working directory is initially "/". -
my_os.chdir(path): This function should change the current working directory topath. Thepathcan be a relative path (e.g., "subdir") or an absolute path (e.g., "/newdir"). It should raise aFileNotFoundErrorif 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
osmodule 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
makedirsand then build upon it for the other functions.makedirsis 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.