简体   繁体   English

python脚本中的子进程错误

[英]Error with subprocess in python script

I am creating a program where I will convert files using subprocesses . 我正在创建一个程序,将在其中使用subprocesses转换文件。 The code I am using for the conversion is: 我用于转换的代码是:

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

When i run the program now I get the error: 当我现在运行程序时,出现错误:

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

Any ideas as to how i can solve this. 关于如何解决这个问题的任何想法。 Code would be much appreciated, but at this point any help would be much appreciated. 代码将不胜感激,但是到那时任何帮助将不胜感激。

As the error suggests you have both a , and + in the array. 由于错误建议你同时拥有,+在数组中。 Based on the other things you're doing, you probably want to get rid of the , after Fileextension . 基于你在做其他的事情,你可能想摆脱,Fileextension You probably want to change all those lines to something like 您可能想将所有这些行更改为类似

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

Note that I also got rid of the space in "unoconv " because it will otherwise be looking for that space as part of the executable name. 注意,我还摆脱了“ unoconv”中的空格,因为否则它将在可执行文件名中查找该空格。

When passing a list to check_call each list element is treated as an argument to the process (which is the first list element). 将列表传递给check_call每个列表元素都被视为流程的参数(这是第一个列表元素)。 So if you want to run unoconv -f file.ext your list for check_call becomes a 3 element list: ['unoconv', '-f', '.txt', 'file.ext'] 因此,如果要运行unoconv -f file.ext ,则check_call的列表将成为3个元素的列表: ['unoconv', '-f', '.txt', 'file.ext']

You seem to be mixing up the string concatenation to put the extension on the filenames and building the list of arguments. 您似乎混淆了字符串串联以将扩展名放在文件名上并构建参数列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM