There is a code:

def go(cur): for dr in listdir(cur): if(isdir(cur.join(dr))): go(cur.join(dr)) elif(dr[-4:] == 'flac'): query = 'cuebreakpoints {}.cue | shnsplit -o flac {}.flac'.format(dr[-5:], dr[-5:]) os.system(query) x = os.getcwd() go(x) 

I need to recurse bypass the subfolders and cut each .flac file.

The problem is that the names of folders may contain Russian characters. As you understand, the code does not work (no changes occur). Is the code not working due to poor localization or a problem with something else?

PS How to display a string, transferring it to UTF-8 before it?

    2 answers 2

    Your algorithm is wrong, try this:

     import os def go(cur): print('go ' + cur) for dr in os.listdir(cur): abs_path = os.path.join(cur, dr) print(' go abs_path ' + abs_path) if os.path.isdir(abs_path): print('dir') go(abs_path) elif 'flac' in dr[-4:]: print('cmd file') # query = 'cuebreakpoints {}.cue | shnsplit -o flac {}.flac'.format(dr[-5:], dr[-5:]) # os.system(query) go(r'D:\Users') 
    • Yes, indeed, late last night I noticed that I was using the wrong join. Apparently, it is worth writing more often) - Jyree
    • If another os.walk or glob to crawl folders and get a list of files) - gil9red

    On Python 3, your code names file names as Unicode already presented — not only Russian letters, but a million more other characters you can use. Read more How to work with paths with Russian characters?

    To execute an external command for each flac file in the current directory tree:

     #!/usr/bin/env python3 import pathlib import shlex import subprocess import sys for path in pathlib.Path().glob("**/*.flac"): paths = [shlex.quote(str(p)) for p in (path.with_suffix('.cue'), path)] command = "cuebreakpoints {} | shnsplit -o flac {}".format(*paths) process = subprocess.run(command, shell=True) if process.returncode != 0: print('warning: failed to split {path}, ' '`{command}` returned {process.returncode}'.format(**vars()), file=sys.stderr) 

    shlex.quote() used to escape special characters in the shell, such as spaces in filenames. If the command ends with a non-zero status (error), a warning is displayed.