pythondataframefilenamesfiletime

Python: creating dataframe with filename and file last modify time


i want to read file name in folder which i already did using file=glob.glob... function. and add in 'file_last_mod_t' column last modify file time.

my part of code:

df=pd.DataFrame(columns=['filename','file_last_mod_t','else'])

df.set_index('filename')
for file in glob.glob('folder_path'): #inside this folder is file.txt
    file_name=os.path.basename('folder_path')
    df.loc[file_name]= os.path.getmtime(file)

which gives me:

df:
filename,file_last_mod_t,else
file.txt,123456,123456          #123456 its time result example

i want to add this last modify time only to file_last_mod_t column, not for all.

i want to receive :

df:
filename,file_last_mod_t,else
file.txt,123456,

thanks in advice

after code modification:

df=pd.read_csv('C:/df.csv')
filename_list= pd.Series(result_from_other_definition)# it looks same as in #filename column
df['filename']=filename_list # so now i have dataframe with 3 columns and firs column have files list
df.set_index('filename')
      for file in glob.glob('folder_path'):#inside this folder is file.txt
      df['file_last_mod_t']=df['filename'].apply(lambda x: (os.path.getmtime(x)) #the way how getmtime is present is now no matter, could be #float numbers
      df.to_csv('C:/df.csv')

#printing samples: first run:

df['filename']=filename_list
print (df)
,'filename','file_last_mod_t','else'
0,file1.txt,NaN,NaN
1,file2.txt,NaN,NaN

code above works fine after first run when df is empty , only with headers. after next run when i run the code and df.csv have some content i am changing manually value of timestamp in file, i am receiving an error : TypeError: stat: path should be string, bytes, os.PathLike or integer,not float this code should replace manually modified cell with good timestamp. i think it's connected with apply also i dont know why index appear in df

**solved **


Solution

  • Please see comment on code as following:

    import os
    import pandas as pd
    import datetime as dt
    import glob
    
    # this is the function to get file time as string
    def getmtime(x):
        x= dt.datetime.fromtimestamp(os.path.getmtime(x)).strftime("%Y-%m-%d %H:%M:%d")
        return x
    
    df=pd.DataFrame(columns=['filename','file_last_mod_t','else'])
    
    df.set_index('filename')
    
    # I set filename list to df['filename']
    df['filename'] = pd.Series([file for file in glob.glob('*')])
    
    # I applied a time modified file to df['file_last_mod_t'] by getmtime function
    df['file_last_mod_t'] = df['filename'].apply(lambda x: getmtime(x))
    
    print (df)
    

    The result is

              filename      file_last_mod_t else
    0        dataframe  2019-05-04 18:43:04  NaN
    1      fer2013.csv  2018-05-26 12:18:26  NaN
    2         file.txt  2019-05-04 18:49:04  NaN
    3        file2.txt  2019-05-04 18:51:04  NaN
    4   Untitled.ipynb  2019-05-04 17:41:04  NaN
    5  Untitled1.ipynb  2019-05-04 20:51:04  NaN
    

    For the updated question, I started with df.csv what have data as following:

    filename,file_last_mod_t,else
    file1.txt,,
    

    And, I think you want to add new files. So, I made the code as following:

    import os
    import pandas as pd
    
    df=pd.read_csv('df.csv')
    
    df_adding=pd.DataFrame(columns=['filename','file_last_mod_t','else'])
    df_adding['filename'] = pd.Series(['file2.txt'])
    df = df.append(df_adding)
    df = df.drop_duplicates('filename')
    
    df['file_last_mod_t']=df['filename'].apply(lambda x: (os.path.getmtime(x))) #the way how getmtime is present is now no matter, could be #float numbers
    df.to_csv('df.csv', index=False)
    

    I created df_adding dataframe for new files and I appended it to df which read df.csv. Finally, we can apply getmtime and save if to df.csv.