> For the complete documentation index, see [llms.txt](https://lwang010.gitbook.io/longw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lwang010.gitbook.io/longw/mlops/chap-3.-hardware-maintain/storage-data-center/case-monitor-storage-increase.md).

# \[Case] Monitor storage increase

def getFileSummaryByMonth(directory, path):

&#x20;folder = path

&#x20;filepaths = \[os.path.join(folder, f) for f in os.listdir(folder)]

stats = {}

&#x20;i = 0

for file in filepaths:

&#x20; \# Get the unique date for each file

&#x20; file\_name = file

&#x20; last\_Mod = os.stat(file).st\_ctime

&#x20; size = os.stat(file).st\_size

&#x20; str1 = time.ctime(os.path.getmtime(file\_name))&#x20;

&#x20; datetime\_object = datetime.datetime.strptime(str1, '%a %b %d %H:%M:%S %Y')

&#x20; date = datetime\_object.strftime("%Y-%m") # 06/07/2013

&#x20; \# Add values to dictionary

&#x20; stats = {'Index': i, 'file\_name': file\_name, 'date': date, 'size': size}

&#x20; print(stats)

&#x20; i = i+1

&#x20; dict\_obj.add(i, stats)

\# Create duplicate file info dictionary

&#x20;filestats = stats

\# Convert to dataframe

&#x20;filestats = pd.DataFrame.from\_dict(dict\_obj,orient='index')

&#x20;print(filestats)

\# Aggregate files by unique month and filesize

&#x20;filestats\["date"] = pd.to\_datetime(filestats\["date"])

&#x20;grouped\_by\_month\_stats = filestats.groupby(filestats\['date'].dt.strftime('%Y-%m'))\['size'].sum().sort\_values()

&#x20;print(grouped\_by\_month\_stats)

&#x20;df = pd.Series.to\_frame(grouped\_by\_month\_stats)

&#x20;df.sort\_values(by='date', inplace=True, ascending=False)

\# Write each filtered dataframe to a different worksheet in the same Excel file

&#x20;df.to\_excel(writer, sheet\_name=directory)

Usage:

getFileSummaryByMonth('reporting\_service', 'C://first\_path//')

getFileSummaryByMonth('archive\_service', 'C://second\_path//')

getFileSummaryByMonth('search\_service', 'C://third\_path//')

Ref: <https://towardsdatascience.com/projecting-and-visualizing-infrastructure-growth-trends-with-python-1e578d9e4260>
