Python Forum

Full Version: How to get full path of specified hidden files matching pattern recursively
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to get the full path of hidden files of matching pattern (file name: .daily.log)
The hidden file name: ".daily.log"

from pathlib import Path

for filename in Path('D:\Backupdata\*\*').rglob('*.daily.log'):
    print(filename)
I am getting below error:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'D:\\Mekala_Backupdata\\*\\*'
Files which starts with a dot are not hidden on Windows-Systems.
The meaning of the dot comes from the unix world.
MS uses instead attributes, which are not easy accessible.
If a file is hidden or not, should not be any problem für Python.

This is not a valid path Path('D:\Backupdata\*\*').
You can't use a wildcard in a Path, but glob and rglob can do this.

If you have the file .daily.log in different paths, you could use rgblob without a wildcard.

from pathlib import Path
 
for filename in Path('D:\Backupdata').rglob('.daily.log'):
    print(filename)
This code will find also a .daily.log in D:\Backupdata\
To prevent this, you can calculate how deep a file is in the root path.

A more flexible function:

from pathlib import Path


def my_constraint(file, depth):
    """
    Returns True, if depth == 2
    On the same level, is level 1
    One directory deeper, is level 2
    ...
    """
    return depth == 2


def recursive_search(root, pattern, constraint=None):
    root = Path(root)
    for file in root.rglob(pattern):
        if constraint:
            depth = len(file.parts) - len(root.parts)
            if constraint(file, depth):
                yield file
        else:
            yield file


# no constraint
generator = recursive_search(r"D:\Backupdata", ".daily.txt")
for file in generator:
    print(file)

# with constraint
generator = recursive_search(r"D:\Backupdata", ".daily.txt", my_constraint)
for file in generator:
    print(file)
I want to check in the Linux system.
Your posted error code was a OSError: [WinError 123]
The path you've used is a path for Windows systems.

On a linux system you don't have a drive letter.
All absolute paths begins at / (the root directory).

Usually you run on linux your scripts as a user like on Windows.
So your Backupdata could be in /home/your_user_account/Backupdata
(On Windows it is C:\Users\your_user_account\Backupdata)

Using the pathlib, allows you to handle paths on windows, mac and linux in right way.

If you want to make your script os independent with the use of Path:

my_backup_dir = Path.home() / "Backupdata"
If your user home directory is named for example FooBar,
then the resulting directory on Linux is: /home/FooBar/Backupdata
And on Windows: C:\Users\FooBar\Backupdata
Thanks. Got it.