In: Computer Science
Write a program using python that loops over each file in a specified directory and checks the size of each file.You should create 2-tuple with the filename and size, should append the 2-tuple to a list, and then store all the lists in a dictionary.
RAW CODE
import os
path = "/home/abhishek/Pictures/" ###Directory path here
files = os.listdir(path) ### This will list all the files in a directory
list_of_tuples = []
dictionary = {}
for file in files:
size = os.path.getsize(path+file) ### + operator makes the absolute path
data = (file,size/1000) ### This will convert byte to kilobyte (KB)
list_of_tuples.append(data) ## data is stored in list
for file,data in list_of_tuples:
dictionary[file] = data ### now list of tuples are converted into dictionary with key as a file name and value as a file size.
print(list_of_tuples)
print("-------------------------------------------------------------")
print(dictionary)
SCREENSHOTS (DIRECTORY, CODE AND OUTPUT)
CODE
DIRECTORY
OUTPUT (SIZE IN KB)
##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####