Skip to content

Instantly share code, notes, and snippets.

@flare9x
Created April 5, 2022 21:40
Show Gist options
  • Save flare9x/57d4518fa927e36ec7dc79f2f8c6ce9e to your computer and use it in GitHub Desktop.
Save flare9x/57d4518fa927e36ec7dc79f2f8c6ce9e to your computer and use it in GitHub Desktop.
list file and file size in a directory and export to .csv
# 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