how to consolidate all program in to function using python and store that output in text file

Viewed 20
import os 
import glob
import collections
import json

path_list=[]
totalFiles = 0
totalDirectories = 0

def convert_bytes(num):

    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)
        num /= 1024.0
        

for line in open("config.ici",'r'):
    li=line.strip()
    if not li.startswith("#"):
        path_list.append(line.rstrip())
        
if len(path_list)==0:
    print("No path is Incerted in config file please")
    path=input("Please enter path to get detailes")
else:
    for path in path_list:
        print("-"*100)
        print(f"We are proceding with {path}")
        print("-"*100)
        for base, dirs, files in os.walk(path):
            for directories in dirs:
                totalDirectories += 1
            for Files in files:
                totalFiles += 1
        print("Directory Information")
        print()
        print('Total number of files =',totalFiles)
        print("-"*100)
        print('Total Number of directories =',totalDirectories)
        print("-"*100)
        print('Total:',(totalDirectories + totalFiles))
        print("-"*100)



        # Get a list of files (file paths) in the given directory 
        list_of_files = filter( os.path.isfile,
                                glob.glob(path + '/**/*', recursive=True) )
        # Sort list of files in directory by size 
        list_of_files = sorted( list_of_files,
                                key =  lambda x: os.stat(x).st_size)
        # Iterate over sorted list of files in directory and 
        # print them one by one along with size
        print("top 10 smalest files in tree")
        print("-"*100)
        for elem in list_of_files[0:9]:
            file_size  = os.stat(elem).st_size 
            print(convert_bytes (file_size), ' -->', elem)
        print("-"*100)

        print("top 5 largest files in a tree")
        print("-"*100)
        for elem in list_of_files[:-6:-1]:
            file_size  = os.stat(elem).st_size 
            print(convert_bytes (file_size), ' -->', elem)
        print("-"*100)

        #extantion
        print("all files with extantion detailes")
        print("-"*100)
        cnt = collections.Counter()
        for base,dirs,files in os.walk(path):

            for filename in glob.glob1(base,"*"):
                name, ext = os.path.splitext(filename)
                cnt[ext] += 1

        #print(str(dict(cnt)))
        dict = {str(k):int (v)  for k,v in cnt.items()}
        json1 = json.dumps(dict,indent=4,sort_keys=False)

        print('extention = {}'.format( json1))
        print("-"*100)



Their will be multiple path in the config fill need to extract all file and folders information for that each path.

Requirement: for each path specific text file like(temp.txt) need to be generate and stored that in report folder Path : direct path(c:/temp) or server path(//localhost/c$:/temp)

can any one guide me how to do this

**Thank you in Advance **

0 Answers
Related