以下是使用 Python 语言在当前用户主目录下创建两个子目录 `test` 和 `test2` 的代码示例: ```python import os # 获取当前用户主目录 home_directory = os.path.expanduser("~") # 创建 test 目录 test_directory = os.path.join(home_directory, "test") if not os.path.exists(test_directory): os.makedirs(test_directory) # 创建 test2 目录 test2_directory = os.path.join(home_directory, "test2") if not os.path.exists(test2_directory): os.makedirs(test2_directory) ``` 以下是三个改进建议: 1. 可以添加错误处理,以更优雅地处理目录创建过程中可能出现的错误。 2. 考虑将创建目录的代码封装为一个函数,提高代码的复用性。 3. 对于路径的拼接,可以使用更简洁的方式,例如 `pathlib` 模块。 [2024-12-23 19:10:51 | AI写代码神器 | 210点数解答]