How to get file count as zero with recursive directory search?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to get file count as zero with recursive directory search?



I am trying to get a file count for a particular filename match and write to a dictionary. The below code works fine if all directories contain some files (match or no match). However, if there is an empty directory, it is not shown in the dictionary. Folder2 is empty and is not shown in the result.
I would also like to know if there is a way to print the result with one forward slash separator instead of combination of double back and forward slashes?
My code:


import os
import re
def file_count_search(root_dir,keyword):
dict=
for dirpath,dirnames,filenames in os.walk(root_dir,topdown=True):
matches = re.findall(keyword, str(filenames))
if keyword in matches:
dict[os.path.join(root_dir,dirpath)] = len(matches)
print dict
file_count_search("c://test","file")



My Result:



'c://test\folder3\subdir_folder3': 1,
'c://test': 1, 'c://test\folder1': 3,
'c://test\folder3': 1



Desired Result:



'c:/test/folder3/subdir_folder3': 1,
'c:/test': 1, 'c:/test/folder1': 3,
'c:/test/folder2': 0,
'c:/test/folder3': 1





dict is a reserved keyword and you shouldn't use it as a variable name.
– Dan Farrell
Aug 9 at 19:02


dict





Thanks, Dan. Noted not to use reserved keywords as variable name. I still look forward to a solution of showing count as 0 if the folder is empty.
– Sel_Python
Aug 10 at 0:15




1 Answer
1



If there are no matching files in a given directory, matches will be an empty list, so keyword in matches will evaluate to False, and nothing will be added to dict.


matches


keyword in matches


False


dict



Try replacing this line:


if keyword in matches:
dict[os.path.join(root_dir,dirpath)] = len(matches)



with just this (also replacing dict with a non-reserved variable name per Dan Farrell's note):


dict


path_to_match_count[os.path.join(root_dir,dirpath)] = len(matches)



... for an update script like this:


import os
import re
def file_count_search(root_dir,keyword):
path_to_match_count=
for dirpath,dirnames,filenames in os.walk(root_dir,topdown=True):
matches = re.findall(keyword, str(filenames))
path_to_match_count[os.path.join(root_dir,dirpath)] = len(matches)
print path_to_match_count
file_count_search("c://test","file")





Yes, I figured if keyword in matches: condition is not the correct one to use. I am looking for a better approach that could solve the issue
– Sel_Python
Aug 10 at 2:47





@Sel_Python can you help me better understand the issue you still need solved? Is it simply having / instead of `` as the delimiter?
– jgaul
Aug 10 at 3:56


/





Ah.. I had not noticed that the 'if' condition was taken out in the solution provided. Thank you, your suggestion worked. Yes, it still shows double backslashes instead of single forward slash but that is cosmetic. Thought os.sep (or something similar) would work but no luck yet. my result shows: 'C:/test': 1, 'C:/test\folder3\subdir_folder3': 1, 'C:/test\folder1': 3, 'C:/test\folder3': 1, 'C:/test\folder2': 0
– Sel_Python
Aug 10 at 4:19






Ah, I could have made what I changed clearer. Updated the answer for clarity. Regarding the backslashes, I think that's just Windows path notation, with the backslash escape character itself escaped in the printed representation. You could always split it and join with another separator, but there's probably not much value to that.
– jgaul
Aug 10 at 4:50



split


join






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered