扫描所有目录并使用全球筛选文件。

huangapple go评论60阅读模式
英文:

scan all catalog and filtering files using globe

问题

Sure, here's the translated code:

有一个搜索目录中所有文件的函数如何应用它以仅返回我需要的文件

def scan_recursive(path):
    with os.scandir(path) as it:
        for entry in it:
            if entry.is_file():
                yield path + "/" + entry.name
            else:
                yield from scan_recursive(entry.path)

for file_path in scan_recursive('C:/test/1'):
    x.append(file_path)

例如只返回文件名近似为2018-*-photo.jpg的文件

我了解到了glob.glob的可能性但无法正确应用它们

我这样做了但我不明白如何将结果写入append列表不起作用

def scan_recursive(directory, path):
    # x = []
    for pathes in path:
        files = Path(directory).glob(f'**/{pathes}')
        for file in files:
            if file.is_file():
                print(file)

This code has been translated, but please note that some parts may require further explanation or context to fully understand the issue you're facing. If you have any specific questions about the code, feel free to ask.

英文:

there is a function that searches for all files in the directory, how do I apply it to it so that it returns only the files I need?

def scan_recursive(path):
    with os.scandir(path) as it:
        for entry in it:
            if entry.is_file():
                yield path + "/" + entry.name
            else:
                yield from scan_recursive(entry.path)

for file_path in scan_recursive('C:/test/1'):
    x.append(file_path)

for example, only files with the approximate name "2018-*-photo.jpg "

I read about the possibilities of "globe.globe" but could not apply them correctly

I did so, but I don't understand how I can write the results to the append list does not help

def scan_recursive(directory, path):
    # x = []
    for pathes in path:
        files = Path(directory).glob(f'**/{pathes}')
        for file in files:
            if file.is_file():
                print(file)

答案1

得分: 0

Using the pathlib library:

pathlib.Path("your_path").glob("2018-*-photo.jpg")

or if you want files in subdirectories too:

pathlib.Path("your_path").glob("**/2018-*-photo.jpg")
英文:

Using the pathlib library:

pathlib.Path("your_path").glob("2018-*-photo.jpg")

or if you want files in subdirectories too:

pathlib.Path("your_path").glob("**/2018-*-photo.jpg")

huangapple
  • 本文由 发表于 2023年4月17日 22:56:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定