我刚刚设置了一个新的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 :(得分: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强烈建议您在标签上使用空格:
空格是首选的缩进方法。
选项卡应仅用于与代码保持一致 已经缩进了标签。