Created
April 5, 2022 21:40
-
-
Save flare9x/57d4518fa927e36ec7dc79f2f8c6ce9e to your computer and use it in GitHub Desktop.
list file and file size in a directory and export to .csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import python modules | |
import os | |
import pandas as pd | |
# directory name from which | |
# we are going to extract our files with its size | |
path = "C:/Users/andre/Downloads/OneDrive_1_4-5-2022" | |
# Get list of all files only in the given directory | |
fun = lambda x: os.path.isfile(os.path.join(path, x)) | |
files_list = filter(fun, os.listdir(path)) | |
# Create a list of files in directory along with the size | |
size_of_file = [ | |
(f, os.stat(os.path.join(path, f)).st_size) | |
for f in files_list | |
] | |
df = pd.DataFrame (size_of_file, columns = ['name','size in bytes']) | |
print (df) | |
df.to_csv (r'C:/Users/andre/Downloads/export_dataframe.csv', index = False, header=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment