使用 python 更改脚本中的目录时出错

Error when using python to change directories in script

我正在创建一个可以转换文件的程序。我的主要问题是我需要我的程序来更改工作目录。我有相应的代码,但它一直说有:

OSError: [Errno 2] No such file or directory:

你们有什么想法吗

这是我当前的代码:

if filetype == "Document":
    path = raw_input("Please enter the path to the folder your file is stored in. An example path is /home/Documents/: ")
    os.chdir(path)
    filename = raw_input("Please enter the name of the file you would like to convert, including the filetype. e.g. test.txt:")


elif filetype == "Audio":
    path = raw_input("Please enter the path to the folder your file is stored in. An example path is /home/Music/: ")
    os.chdir(path)
    filename = raw_input("Please enter the name of the file you would like to convert, including the filetype. e.g. test.txt:")


elif filetype == "Video":
    path = raw_input("Please enter the path to the folder your file is stored in. An example path is /home/Movies/: ")
    os.chdir(path)
    filename = raw_input("Please enter the name of the file you would like to convert, including the filetype. e.g. test.txt:")


elif filetype == "Image":
    path = raw_input("Please enter the path to the folder your file is stored in. An example path is /home/Pictures/: ")
    os.chdir(path)
    filename = raw_input("Please enter the name of the file you would like to convert, including the filetype. e.g. test.txt:")

当我输入路径时:

/home/Pictures/

我收到错误:

Traceback (most recent call last):
  File "conversion.py", line 29, in <module>
    os.chdir(path)
OSError: [Errno 2] No such file or directory: '/home/Pictures/'

我已经检查过,当我把它写成 cd /home/Pictures/ 它确实有效。

为什么不跳过额外的步骤直接询问文件路径? 如果需要,您可以从中导出目录...但可能不需要它。

当我完成类似的操作后,我建议用户只需将文件或文件夹拖到命令行,因为很容易在路径中输入一个简单的错误而不会被捕获。