我正在创建一个程序,我将使用subprocesses
转换文件。我用于转换的代码是:
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'
关于如何解决这个问题的任何想法。代码将非常感激,但在这一点上任何帮助将不胜感激。
答案 0 :(得分:1)
由于错误表明数组中同时包含,
和+
。根据您正在做的其他事情,您可能希望在,
之后摆脱Fileextension
。您可能希望将所有这些行更改为
subprocess.check_call(['unoconv', '-f', Fileextension, filename])
请注意,我也摆脱了&#34; unoconv&#34;因为否则它会将该空格作为可执行文件名的一部分。
将列表传递给check_call
时,每个列表元素都被视为进程的参数(这是第一个列表元素)。因此,如果您要运行unoconv -f file.ext
,check_call
的列表将成为3个元素列表:['unoconv', '-f', '.txt', 'file.ext']
您似乎正在混合字符串连接以将扩展名放在文件名上并构建参数列表。