英文:
List files in specified directory without subdirectories
问题
The code below goes through all Excel files, including files in sub-folders. How can I only go through files without including those in subfolders? Thanks.
dirpath = Path(pl.PureWindowsPath(values['-FIN-']))
for path in dirpath.rglob("*.xls*"):
英文:
Below code goes thru all excel files including files in sub-folders, how can i only go thru files without files in subfolders. thanks
dirpath=Path(pl.PureWindowsPath(values['-FIN-']))
for path in dirpath.rglob("*.xls*"):
答案1
得分: 1
使用 glob
方法而不是 rglob
,还将使用 is_file()
方法来检查路径是否是文件而不是目录。
from pathlib import Path
import os
dirpath = Path(values['-FIN-'])
for path in dirpath.glob("*.xls*"):
if path.is_file():
print(path)
英文:
Use the glob
method instead of rglob
also we gonna use is_file()
method to checks if the path is a file and not a directory.
from pathlib import Path
import os
dirpath = Path(values['-FIN-'])
for path in dirpath.glob("*.xls*"):
if path.is_file():
print(path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论