2015-05-24 2 views
1

Я создаю программу, которая будет конвертировать файлы. Моя основная проблема заключается в том, что мне нужна моя программа для изменения рабочего каталога. У меня есть код для этого, но он продолжает говорить, что есть:Ошибка при использовании python для изменения каталогов в скрипте

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/it works.

+0

Также показать образец ввода, который вы передаете сценарию? – ZdaR

+0

@anmol_uppal Я внес изменения. – averyrc

+0

Значит, вы используете Ubuntu? – ZdaR

ответ

0

Почему бы не пропустить дополнительный шаг и не запросить путь к файлам напрямую? Вы можете получить каталог из этого при необходимости ... но perharps он не понадобится.

Когда я делал такие вещи, я предложил, чтобы пользователь просто перетащил файл или папку в командную строку, потому что легко сделать простую ошибку ввода в пути и не поймать ее.

+0

Спасибо, я понятия не имел, что вы можете это сделать. Вы, сэр, гений. – averyrc

+0

@averyrc: не забудьте принять ответ тогда ... –