gpt4 book ai didi

python - python 脚本中的子进程错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:41:38 25 4
gpt4 key购买 nike

我正在创建一个程序,我将在其中使用子进程转换文件。我用于转换的代码是:

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'

关于如何解决这个问题的任何想法。代码将不胜感激,但此时任何帮助将不胜感激。

最佳答案

正如错误所示,数组中同时包含 +。根据您正在做的其他事情,您可能希望删除 Fileextension 之后的 ,。您可能想将所有这些行更改为类似

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

请注意,我还删除了“unoconv”中的空格,因为否则它会查找该空格作为可执行文件名称的一部分。

将列表传递给 check_call 时,每个列表元素都被视为进程的参数(这是第一个列表元素)。因此,如果您想运行 unoconv -f file.ext,您的 check_call 列表将变成一个 3 元素列表:['unoconv', '-f', ' .txt', '文件.ext']

您似乎混淆了字符串连接以将扩展名放在文件名上并构建参数列表。

关于python - python 脚本中的子进程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30459232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com