使用python更改脚本

时间:2015-05-24 19:01:31

标签: python path cd

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

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 /它时,这确实有效。

1 个答案:

答案 0 :(得分:0)

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

当我完成这样的事情时,我建议用户只需将文件或文件夹拖到命令行,因为它很容易在路径中产生简单的输入错误而不是捕获它