0

I would like to count all of all mp3 files in one directory.

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

I get this error, but I don't know why. can someone help me fix the error? many thanks

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
kuck
  • 97
  • 6
  • Where is `file_name` variable? and I think the place where your running the line `a = str(check_output('ffprobe -i "'+file_name+'" 2>&1 |findstr "Duration"',shell=True))` is NOT having proper indentation . Reformat it to work fine – Sreevathsabr Apr 12 '20 at 08:00
  • I corrected the filename, but the error still remains – kuck Apr 12 '20 at 08:04
  • Look to the second issue which , I had noticed, the Indentation issue. So move all the code which is below `for loop` with back Tab for two times . Press one Forward tab which will indent to if loop and come below the `if loop` and press forward Tab once . So indentation will be maintained . – Sreevathsabr Apr 12 '20 at 08:06
  • your editor mixed tabs and spaces for indentation. python 3 does not accept this expresses its sadness with this error. use an editor that can show tabs as special characters and fix them. – Patrick Artner Apr 12 '20 at 08:13

1 Answers1

0

This error is the bane of any programmer. Depending on your IDE, you can either use an online tool or select all, indent then unindent, however, for now, you can just copy the code below, which should have, in theory, fixed the error (you may notice it looks the same, but trust me):

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  "'+file_name+'" 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))

EDIT:
The source of your new error is the fact that duration is not a string. use str() to convert it:

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  "'+file_name+'" 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, str(duration)))
        sum = sum + duration

sum = round(sum)
print("sum", sum)
time = datetime.timedelta(seconds=sum)
print(str(time))
Robinson
  • 300
  • 3
  • 12
  • great, it works, but I have another error now: Traceback (most recent call last): File "loop3.py", line 14, in print(os.path.join(directory, filename, duration)) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\ntpath.py", line 117, in join genericpath._check_arg_types('join', path, *paths) File "C:\Users\xxx\AppData\Local\Programs\Python\Python38-32\lib\genericpath.py", line 152, in _check_arg_types raise TypeError(f'{funcname}() argument must be str, bytes, or ' TypeError: join() argument must be str, bytes, or os.PathLike object, not 'float' – kuck Apr 12 '20 at 08:10
  • @kuck I've updated the original to include a fix for your newest error. – Robinson Apr 12 '20 at 08:15
  • cool thanks, working now perfekt. – kuck Apr 12 '20 at 08:34