【发布时间】:2021-03-27 20:46:36
【问题描述】:
我想编写一个进入桌面文件夹的程序,并执行诸如创建文件夹之类的命令。我有这个:
import os
def run_command(command):
os.system(f'cmd /c "{command}"')
"""
This command is running in the folder of this Python file, thus I can only access folders from this directory.
But I want it to go to the Desktop folder and make a new directory there.
"""
run_command("cd Desktop") # It makes an error "The system cannot find the path specified."
run_command("md Folder")
r"""I want the directory to be C:\Users\<username>"""
正如您在 cmets 中看到的,它执行您运行此 Python 文件的目录中的所有操作。但我希望目录为“C:\Users
感谢您的帮助。
【问题讨论】:
标签: python cmd operating-system