
A CSV import script switches from glob.glob to pathlib, and suddenly a dot-prefixed export appears in the input list. Another run tries to open a directory named folder.csv. I would check the selection rules before changing the parser. A matching filename is not proof that a path is an intended input file.
Three decisions hiding inside one wildcard
Path.glob and Path.rglob can return matching paths of any kind, including directories. In pathlib, names beginning with a dot are not special. That differs from the glob module's default treatment of dot-prefixed names. Neither API promises the order you may happen to see on one machine.
Case matching is another separate decision. Pathlib normally follows platform-specific casing rules, typically case-sensitive on POSIX and case-insensitive on Windows. A script should not accidentally depend on whether someone named an export SALES.CSV or sales.csv.
Make the import policy explicit
This read-only example recursively lists regular CSV files, excludes symbolic-link entries, treats the extension without case sensitivity, and excludes any result with a dot-prefixed component below the chosen root:
from pathlib import Path
root = Path('exports')
if not root.is_dir():
raise NotADirectoryError(root)
candidates = sorted(
p.relative_to(root).as_posix()
for p in root.rglob('*')
if p.is_file()
and not p.is_symlink()
and p.suffix.lower() == '.csv'
and not any(
part.startswith('.')
for part in p.relative_to(root).parts
)
)
for name in candidates:
print(name)
Using rglob('*') and then checking the suffix makes the extension policy explicit without relying on a newer case_sensitive keyword. Testing every relative path component also excludes .cache/data.csv, not only files whose final name starts with a dot. Checking just p.name would miss that distinction.
The sort gives the displayed relative names a defined order. It does not establish which export is newest, which one supersedes another, or whether two files contain duplicate records. Those decisions need their own import contract.
Filtering results is not pruning traversal
This code filters discovered paths. It does not prevent rglob from visiting dot-prefixed directories while searching. For a large tree, use a walker with a deliberate directory-pruning policy instead. Python's os.walk supports editing the directory-name list in place when walking top-down.
Dot-prefix filtering is a naming convention, not a portable implementation of every operating system's hidden-file attributes. Likewise, excluding symbolic-link entries is not a security boundary for an untrusted or changing directory tree. Use a controlled input directory; this example is not a sandbox or a secure archive extractor.
What I checked locally
I created a temporary fixture with sales.csv, .private.csv, UPPER.CSV, a directory named folder.csv, a nested archive/old.csv, and CSV files beneath dot-prefixed components. In Python 3.10.12 on Linux, the top-level pathlib pattern included both .private.csv and folder.csv. The selection policy returned only UPPER.CSV, archive/old.csv and sales.csv, sorted as shown.
That small test checks the filtering logic, not Windows attributes, access failures or concurrent changes. It writes only the temporary fixture, not real exports. The selection code itself only lists paths.
Review before opening anything
Print the root and candidate list before connecting this to an importer. Confirm whether recursion and dot-prefixed exclusions match the actual export process. A legitimate producer may deliberately use a dot-prefixed name.
An empty list is not proof that a folder is empty or that scanning was complete. Current pathlib documentation says filesystem scanning errors are suppressed; behavior changed in Python 3.13. Completeness-sensitive jobs need explicit error reporting, file-count checks and validation when each file is opened. A file can disappear or change after selection.
Comments
Post a Comment