List files in specified directory without subdirectories.

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

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)

huangapple
  • 本文由 发表于 2023年4月10日 18:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976396.html
匿名

发表评论

匿名网友

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

确定