Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get the date a file was created into my dataframe in Python?

My current df looks like-

Company State Address Zip    City
ABC      CA   1 st    86284  LA

All this data is being pulled from files that are monthly dropped in a directory.

I want another column that denotes the month the file was created/dropped in the directory minus 1(File Date minus 1 month). So basically, if the file was created on 5/5/22. I want the below

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Company State Address Zip    City  File Month
ABC      CA   1 st    86284  LA    04-2022

My attempt-

import datetime
import os
# Path to the file
path = r"E:\demos\files_demos\sample.txt"

# file creation timestamp in float
c_time = os.path.getctime(path)
# convert creation timestamp into DateTime object
dt_c = datetime.datetime.fromtimestamp(c_time)
df['File Month'] = dt_c
2022-05-05 17:21:57.914774

But this is giving me the above. Is there a better way to do this?

>Solution :

You can subtract a pd.tseries.offsets.MonthEnd from each date, and use strftime:

df['File Month'] = df['File Month'].sub(pd.tseries.offsets.MonthEnd()).dt.strftime('%m-%Y')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading