2015-05-26 83 views
0

我剛剛成立了一個新的Python腳本,當我運行它,我得到的錯誤代碼:有錯誤代碼,如果語句

File "conversion.py", line 17 
    elif filetype == "Audio": 
    ^

我的代碼是:

if filetype == "Document": 
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:") 
    os.chdir(path[1:-2]) 
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:") 
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .txt: ") 
    from subprocess import check_call 
subprocess.check_call(['unoconv', '-f', Fileextension, filename]) 

elif filetype == "Audio": 
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:") 
    os.chdir(path[1:-2]) 
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:") 
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .mp3: ") 
    body, ext = os.path.splitext("filename") 
    check_call(["ffmpeg" ,"-i", filename, body Fileextension]) 

elif filetype == "Video": 
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:") 
    os.chdir(path[1:-2]) 
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:") 
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .mp4: ") 
    body, ext = os.path.splitext("filename") 
    from subprocess import check_call 
    check_call(["ffmpeg" ,"-i", filename, body Fileextension]) 

elif filetype == "Image": 
    path = raw_input("Please drag and drop the directory in which the file is stored into the terminal:") 
    os.chdir(path[1:-2]) 
    filename = raw_input("Please enter the name of the file you would like to convert, including the file-type. e.g. test.txt, however please do make sure that the file-name does not have any spaces:") 
    Fileextension = raw_input("What filetype would you like the program to convert your file to. E.g. .Jpeg: ") 
    body, ext = os.path.splitext("filename") 
    from subprocess import check_call 
    check_call(["ffmpeg" ,"-i", filename, body Fileextension]) 

沒有任何人有任何想法至於這裏的錯誤是什麼。任何解決方案將非常感激。我一直試圖解決它一個小時,我仍然不知道爲什麼會發生。

+0

'subprocess.check_call(['unoconv','-f',Fileextension,filename])'識別,是錯誤類型? –

+3

什麼是完整的錯誤信息,並且您的縮進是否正確?你的'subprocess'行不縮進。如果應該,請修復它。如果不是,那是你的錯誤。 – Andy

+1

這並不能解決你目前的問題,但是當[你以前的問題](http://stackoverflow.com/questions/30459232/error-with-subprocess-in-python-script)的回答者告訴你刪除你的'+'操作符,我認爲他的意思是隻在逗號後面出現的那些操作符。你應該把check_call([「ffmpeg」,「 - i」,filename,+ body + Fileextension])'變成'check_call([「ffmpeg」,「 - i」,filename,body + Fileextension]), 'check_call([「ffmpeg」,「 - i」,filename,body Fileextension])'。 (或者也許它應該是'check_call([「ffmpeg」,「 - i」,filename,body,Fileextension])?) – Kevin

回答

2

您在混合製表符和空格,因此您的subprocess.check_call(['unoconv', '-f', Fileextension, filename])行的縮進程度不夠。 Python的擴展選項卡每個8位匹配,但你似乎已經配置編輯器縮進爲4個空格的標籤,而不是:

>>> lines = '''\ 
...    from subprocess import check_call  
...  subprocess.check_call(['unoconv', '-f', Fileextension, filename]) 
... ''' 
>>> lines.splitlines()[1] 
" subprocess.check_call(['unoconv', '-f', Fileextension, filename])" 
>>> lines.splitlines()[0] 
' \tfrom subprocess import check_call\t' 

注意\t性格上import線,而下一行(首先打印以更好地調出標籤)。所有縮進行使用製表符,但subprocess.call()行除外。

配置您的編輯器以將選項卡擴展爲空格;當您避免縮進標籤時,Python效果最佳。 Python style guide強烈建議您在製表符上使用空格:

空格是首選的縮進方法。

選項卡應該完全用於與已用標籤縮進的代碼 保持一致。

+1

或者只使用標籤:) – YoYoYonnY

+2

@YoYoYonnY:然後任何潛入的空間都會破壞你的碼。 *再次*。檢測雜散標籤比檢測雜散空間更容易。 –

+1

奇怪的是我從來沒有像elif那樣崩潰,但總是出現縮進錯誤...因爲我總是使用製表符。 – YoYoYonnY