英文:
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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论