I have written a code that should display the folder name and file count inside the folder. The code works fine when keeping outside a loop but returns me count as zero when I keep the code inside the loop
root = 'C:/Users/PycharmProjects/Logs_of_Hell'
count_of_file = 0
import pathlib
noOfFiles = local_time_of_oldest = local_time_of_newest= 0
subdir=""
N = 0
def get():
for path, subdirs, files in os.walk(root):
N_c = len(files)
user = {}
user['filepath'] = path
user['count'] = N_c
print("Printing first User")
print(user) # prints {'filepath': 'C:/Users/PycharmProjects/Logs_of_Hell\\results\\0', 'count': 3}
print("second")
for name in subdirs:
print(user)# prints {'filepath': 'C:/Users/PycharmProjects/Logs_of_Hell\\results\\0', 'count': 0}
#.....
so here it works fine and gives me the correct count on the first print and gives me zero counts on the second. am able to retrieve the file path, but the value of count turns to zero. I tried debugging giving a static value that works fine on both prints, but when I try using it like this, the count value turns zero on the second print. Can anyone explain to me why am not able to get the count value on the second loop
this is output am observing , even though i use the same dict variable na , the value is different for both prints :
{'filepath': 'C:/Users/Logs_of_Hell\\pabot_results', 'count': 0}
second
{'filepath': 'C:/Users/Logs_of_Hell\\pabot_results', 'count': 0}
second
Printing first User
{'filepath': 'C:/Users/Logs_of_Hell\\pabot_results\\0', 'count': 3}
Printing first User
{'filepath': 'C:/Users/Logs_of_Hell\\pabot_results\\1', 'count': 3}
Process finished with exit code 0
Tried Editing Code :
import os
import time
import glob
root = 'C:/Users/PycharmProjects/Logs_of_Hell'
count_of_file = 0
import pathlib
noOfFiles = local_time_of_oldest = local_time_of_newest= 0
subdir=""
N = 0
nextone ={}
def get():
for path, subdirs, files in os.walk(root):
N_c = len(files)
user = {}
user['filepath'] = path
user['count'] = N_c
# print(N_c)
print(user)
for name in subdirs:
print(user)
# # print(os.path.join(path, name))
subdir = os.path.join(path, name)
if(os.path.isdir(subdir)):
taggedrootdir = pathlib.Path(subdir)
oldest_file = (min([f for f in taggedrootdir.resolve().glob('**/*') if f.is_file()], key=os.path.getmtime))
newest_file = (max([f for f in taggedrootdir.resolve().glob('**/*') if f.is_file()], key=os.path.getmtime))
c_time_of_oldest = os.path.getctime(oldest_file)
local_time_of_oldest = time.ctime(c_time_of_oldest)
c_time_of_newest = os.path.getctime(newest_file)
local_time_of_newest = time.ctime(c_time_of_newest)
nextone['filepath'] = subdir
nextone['newest'] = local_time_of_newest
nextone['oldest'] = local_time_of_oldest
# print(nextone)
mrgDict = mergeDict(user, nextone) # performing merging
but the second print only prints the last value
printto print different things, because you don't change any value insideuserbetween the two calls. After the second call, you might be changing the value in code you aren't showing us.