Reputation: 329
My requirement is
I'm calling a function to extract files/folders from a .Zip file
files_extract_with_structure(file_source, file_dest)
file_source
and file_dest
are the variables I'm passing to the above function and the value of the file_source
variable is defined as below.
file_source = "/dbfs/mnt/devadls/pre/Source_Files/2022-10/767676.XXX.XXX.XXXX.20221010090858.txt.zip"
where 767676.XXX.XXX.XXXX.20221010090858.txt.zip
is the zip file name
The above function works fine if I pass the file_source
variable value as above (hardcoded the zip file name)
My requirement is instead of hard coding the zip file name, Can we specify the wild card file names as below?
file_source = "/dbfs/mnt/devadls/pre/Source_Files/2022-10/767676.XXX.XXX.XXXX.*.txt.zip"
because I will receive the same file with a different date in the next month and so on...
But when I specify the wildcard names as "767676.XXX.XXX.XXXX.*.txt.zip"
, it is throwing a `No such file or directory error.
Kindly help to resolve this issue. Thanks.
Upvotes: 0
Views: 251
Reputation: 66
You could use the fnmatch
module from the standard library, it will let you filter and match filename based on unix-like rules.
With your example, something like this should work:
from fnmatch import fnmatch
file_source = "/dbfs/mnt/devadls/pre/Source_Files/2022-10/767676.XXX.XXX.XXXX.20221010090858.txt.zip"
pattern = "/dbfs/mnt/devadls/pre/Source_Files/2022-10/767676.XXX.XXX.XXXX.*.txt.zip"
if fnmatch(file_source, pattern):
files_extract_with_structure(file_source, file_dest)
else:
print("No file found")
The next step might be to list the files in the source directory, and for each to test against the pattern.
Hope this will help!
Upvotes: 2