Python Forum
How to get full path of specified hidden files matching pattern recursively
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get full path of specified hidden files matching pattern recursively
#1
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\\*\\*'
Reply
#2
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)
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I want to check in the Linux system.
Reply
#4
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
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks. Got it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  does not save in other path than opened files before icode 3 897 Jun-23-2023, 07:25 PM
Last Post: snippsat
  Copy only hidden files and folders with rsync Cannondale 2 997 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How to return the next page from json recursively? sandson 0 1,146 Apr-01-2022, 11:01 PM
Last Post: sandson
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 1,614 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  Finding files matching pattern GrahamL 1 1,283 Jan-14-2022, 01:16 PM
Last Post: DeaD_EyE
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,073 Jan-07-2022, 07:38 PM
Last Post: cubangt
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,471 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,198 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  get all the files in the path in a list? korenron 23 7,015 Jul-19-2021, 07:44 AM
Last Post: korenron
  Matching two files based on a spited elements tester_V 5 2,797 May-30-2021, 07:49 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020