简体   繁体   中英

Python multiple functions with the same decorator execute in main

I created a decorator to walk through directories for several functions to do some file operations. Every time when more than one functions with the decorator in the script, only the first one will execute.

import os
import re
import sys


def oswalk_deco(func):
    def wrapper(filename, *args):
        subdirs = [os.path.abspath(x[0]) for x in os.walk(target_dir)]
        subdirs.remove(os.path.abspath(target_dir))
        for dir in subdirs:
            os.chdir(dir)
            for item in os.listdir('.'):
                p = re.match(filename, item)
                if isinstance(p, re.Match):
                    match = p.group()
                    func(match, *args)
    return wrapper


def str2uni(string):
    if isinstance(string, str):
        return string.encode('utf8').decode('unicode_escape')
    else:
        print('Function "str2uni(string)" only accept strings.')
        exit()


@oswalk_deco
def sub_string(filename, regex, substr):
    with open(filename, 'r') as file:
        content = file.read()
    with open(filename, 'w') as file:
        content = re.sub(regex, substr, content)
        file.write(content)


@oswalk_deco
def regex_print(filename, string):
    with open(filename, 'r') as file:
        content = file.read()
        relist = re.findall(string, content)

    if filename[0] == 'u':
        print({str2uni(f'\\u{filename[1:-4]}'): relist})
    elif isinstance(re.match(r'code\d{2}-u.+', filename), re.Match):
        print({str2uni(f'\\{re.search("u[0-9a-z]{4,5}", filename).group()}'): relist})


@oswalk_deco
def docname_format(filename):
    with open(filename, 'r') as file:
        content = file.read()
    with open(filename, 'w') as file:
        content = re.sub(r'docname=".*"', f'docname="{filename}"', content)
        file.write(content)


if __name__ == '__main__':
    if len(sys.argv) == 1:
        target_dir = '.'
    else:
        target_dir = sys.argv[1]

    regex_print('.*\.svg', 'docname=".*"')
    regex_print('.*\.svg', 'stroke:none')
    sub_string('.*\.svg', 'docname=".*"', 'docname="stackoverflow.svg')

It seems like I've missed some important properties in Python?

Your target_dir defaults to . , the current working directory, if there's no command line argument given, and in your wrapper function, the os.walk function is always called with target_dir , which, after the os.chdir call, would refer to one of the subfolders of the first call to the decorated function, so os.walk naturally would not be able to find any more subfolders under . , which is already a subfolder.

You can fix this by getting the absolute path of target_dir first:

if len(sys.argv) == 1:
    target_dir = '.'
else:
    target_dir = sys.argv[1]
target_dir = os.path.abspath(target_dir)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM