【发布时间】:2020-04-12 07:56:32
【问题描述】:
我想统计一个目录中的所有 mp3 文件。
import os
from subprocess import check_output
directory = r'c:\test'
sum = 0
for filename in os.listdir(directory):
if filename.endswith(".mp3"):
a = str(check_output('ffprobe -i "'+filename +'" 2>&1 |findstr "Duration"',shell=True))
a = a.split(",")[0].split("Duration:")[1].strip()
h, m, s = a.split(':')
duration = int(h) * 3600 + int(m) * 60 + float(s)
print(os.path.join(directory, filename, duration))
sum = sum + duration
sum = round(sum)
print("sum", sum)
time = datetime.timedelta(seconds=sum)
print(str(time))
我收到此错误,但我不知道为什么。有人可以帮我解决错误吗?非常感谢
File "loop.py", line 10
a = str(check_output('ffprobe -i "'+filename+'" 2>&1 |findstr "Duration"',shell=True))
^
TabError: inconsistent use of tabs and spaces in indentation
【问题讨论】:
-
file_name变量在哪里?而且我认为您运行a = str(check_output('ffprobe -i "'+file_name+'" 2>&1 |findstr "Duration"',shell=True))的地方没有适当的缩进。重新格式化以正常工作 -
我更正了文件名,但错误仍然存在
-
查看我注意到的第二个问题,即缩进问题。因此,将
for loop下面的所有代码用 back Tab 移动两次。按一个将缩进到 if 循环并位于if loop下方的向前选项卡,然后按一次向前选项卡。所以缩进会被保留。 -
您的编辑器混合了制表符和空格以进行缩进。 python 3 不接受这表达了对这个错误的悲伤。使用可以将选项卡显示为特殊字符并修复它们的编辑器。
标签: python-3.x