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)
In your original code, there are multiple boolean args creating different paths. Your
extension == '.'
path was the only one wherecheck_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: