2015-05-26 85 views
1

我正在創建一個程序,我將使用subprocesses轉換文件。我使用了轉換的代碼是:Python腳本中的子進程錯誤

import tornado.ioloop 
import tornado.web 
import os 

print "If at any point you wish to quit the program hit Ctrl + C" 

filetype = raw_input("What kind of file would you like to convert? Audio, Image, Video or Document: ") 

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 
    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]) 

當我運行該程序,現在我得到的錯誤:

File "conversion.py", line 15, in <module> 
    check_call(["unoconv " ,"-f ", Fileextension , + filename]) 
TypeError: bad operand type for unary +: 'str' 

任何想法,我怎麼能解決這個問題。代碼將不勝感激,但在這一點上任何幫助將不勝感激。

+1

刪除文件名前的+。錯誤消息明確指出。把它叫做check_call([「unoconv」,「 - f」,Fileextension,filename)) –

回答

1

由於錯誤提示您在數組中有,+。根據你正在做的其他事情,你可能想在Fileextension之後擺脫,。你可能想改變所有這些行類似

subprocess.check_call(['unoconv', '-f', Fileextension, filename]) 

請注意,我還得到了在「unoconv」擺脫了空間的,因爲它會以其他方式來尋找一個空間作爲可執行名稱的一部分。

將列表傳遞給check_call時,每個列表元素都被視爲進程的參數(它是第一個列表元素)。所以,如果你想運行unoconv -f file.ext列表爲check_call變成了3元素列表:['unoconv', '-f', '.txt', 'file.ext']

你似乎混合了字符串連接穿上文件名擴展和建設的參數列表。

+0

我明白你的意思了,但是當轉換unoconv中的東西時,你必須把信息作爲'unoconv -f .txt file.ext'你知道我會怎麼說嗎。 –

+0

@AveryRipollCrocker對不起,我不熟悉'unoconv'。我已更新我的答案,包括額外的論點,我認爲是你想要的 –

+0

謝謝@Eric Renouf。 –