Python Batch convert of FLAC files

2019-09-08 16:40发布

问题:

I want to convert all FLAC files to OGG in the working directory:

This is what I ALREADY have.

for root, dirs, files in os.walk(args):
        flacs = [f for f in files if f.endswith('.flac')]
        oggs = [o for o in files if o.endswith('.ogg')]

        for flacfiles in flacs:
            id3 = ('id3v2', '-C', flacfiles)
            cmd = ('oggenc', '-q7', flacfiles)
            try:
                subprocess.check_call(id3, cwd=root)
                subprocess.check_call(cmd, cwd=root)
            except subprocess.CalledProcessError:
                print "subprocess.CalledProcessError: Command %s returned non-zero exit status 1" % cwd

Now I want to know how I can - in the directory containing my FLAC files - check if there is one .flac with a .cue and if that is the case do_something()

回答1:

for flacfiles in flacs:
    if os.path.exists(os.path.splitext(flacfiles)[0] + '.cue')):
        # do something