Exclude hidden files in Python

1.2k Views Asked by At

Well, there's a thing I have to do: I have to count files with or without hidden files, with or without recursion, with certain extension or without (it's up to a user)(CLI). The problem is with hidden files. My method:

if namespace.recursive == True:
    for files in os.walk(top=namespace.path, topdown=True): 
        for i in files[2]:
            countF += 1 
    print('Number of files in directory (with recursion): ', countF)
else: 
    p = Path(namespace.path)
    for subdirs in p.iterdir():
        if (subdirs.is_file()):
            count += 1
    print('Number of files in directory (without recursion): ', count)

counts files WITH the hidden ones. What I want to do: I want this method to count files WITHOUT the hidden ones. But if a user inputs -h parameter, I want to count ONLY hidden files. So I tried to do a check-method for it:

def check_attributes(filename):
    if(os.path.isfile(filename)):
        return win32api.GetFileAttributes(filename) & win32con.FILE_ATTRIBUTE_HIDDEN
    else:
        return 0

and then I tried to modify my method and add after

for i in files[2]:

something like:

if check_attributes(f) == 0: #if it's not hidden - then count

But it still counts with hidden files. I want to understand how to do it right. Thank you so much in advance for every answer! EDIT: full function with checking

def countFiles():
countF = int(0)
count = int(0)
c = int(0)
try:
    if namespace.extension == '.':
        if namespace.recursive == True:
            if namespace.hidden == False:
                for files in os.walk(top=namespace.path, topdown=True): 
                    for i in files[2]:
                        if check_attributes(i) == 0:
                            countF += 1 
                print('Number of files in directory (with recursion): ', countF)
        else: 
            if namespace.hidden == False:
                p = Path(namespace.path)
                for subdirs in p.iterdir():
                    if (subdirs.is_file()):
                        count += 1
                print('Number of files in directory (without recursion): ', count)
    else:
        if namespace.recursive == True:
            for files in os.walk(namespace.path):
                for f in files[2]:
                    if os.path.splitext(f)[1] == namespace.extension:
                        c += 1
            print('Number if files with extension ' + namespace.extension + ' in directory (without recursion):', c)
        else:
            for files in os.listdir(namespace.path):
                if os.path.splitext(files)[1] == namespace.extension:
                    c += 1
            print('Number if files with extension ' + namespace.extension + ' in directory (without recursion): ', c)
except Exception as e:
    print('Error:\n', e)
    sys.exit(0)
1

There are 1 best solutions below

1
On BEST ANSWER

In your original code, there are multiple boolean args creating different paths. Your extension == '.' path was the only one where check_attributes was being called from what I can tell, so that might have been the issue. I decided to take a crack at rewriting it. The way I rewrote it has 2 phases: 1. get the files, either recursively or not then 2. filter the files with the args provided. Here's what I came up with:

import argparse
import os
import win32api
import win32con


def count_files(args):
    files = []
    
    # Get the files differently based on whether recursive or not.
    if args.recursive:
        # Note here I changed how you're iterating. os.walk returns a list of tuples, so
        # you can unpack the tuple in your for. current_dir is the current dir it's in
        # while walking and found_files are all the files in that dir
        for current_dir, dirs, found_files in os.walk(top=args.path, topdown=True):
            files += [os.path.join(current_dir, found_file) for found_file in found_files]
    else
        # Note the os.path.join with the dir each file is in. It's important to store the
        # absolute path of each file.
        files += [os.path.join(args.path, found_file) for found_file in os.listdir(args.path)
                  if os.path.isfile(os.path.join(args.path, found_file))]
       
    filtered_files = []
    for found_file in files:
        print(found_file)
        if not args.hidden and (win32api.GetFileAttributes(found_file) & win32con.FILE_ATTRIBUTE_HIDDEN):
            continue  # hidden == False and file has hidden attribute, go to next one
        
        if args.extension and not found_file.endswith(args.extension):
            continue  # File doesn't end in provided extension
            
        filtered_files.append(found_file)
    
    print(f'Length: {len(filtered_files)}')
    return len(filtered_files)
    

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Process some integers.')
    # Note that I took advantage of some other argparse features here like
    # required vs optional arguments and boolean types
    parser.add_argument('path')
    parser.add_argument('--recursive', action='store_true', default=False)
    parser.add_argument('--hidden', action='store_true', default=False)
    parser.add_argument('--extension', type=str)

    args = parser.parse_args()
    count_files(args)