return more recent files to a folder

Asked

Viewed 26 times

1

I am trying to return the path and modification date of all files in a folder, but I am not able to return the date, only the path.

code so far:

from pathlib import Path
import pandas as pd

data_modificacao = lambda f: f.stat().st_mtime

directory = Path('path')
files = directory.rglob('*.*')
sorted_files = sorted(files, key = data_modificacao, reverse = True)

pathdf = list(sorted_files)
pathdf = pd.DataFrame(pathdf)
pathdf.rename(columns = {0:'path'}, inplace = True)

pd.set_option('max_colwidth', None)
pathdf.head(10)

the output is a Dataframe ordered by the modification date, but only with the path column... how do I return, also, a column with the modification date?

1 answer

0


from pathlib import Path
import pandas as pd
    
directory = Path('./')
files = list(directory.rglob('*.*'))

raw_data = [[item.name,item.stat().st_mtime] for item in files]

df = pd.DataFrame(raw_data, columns=['Nome', 'Data'])
df['Data'] = pd.to_datetime(df['Data'], unit='s')
df = df.sort_values(by='Data', ascending = False).reset_index(drop = True)
  1. Importing the packages
  2. Setting the directory and getting the list of files
  3. Creating a list of lists with the name and modification date
  4. Creating the Data Frame
  5. Converting the Date column to the datetime type
  6. Sorting the data according to the Date column
  • 1

    very good, it worked! , another solution I found, using my code, would be to create a Dataframe with the date from the sorted_files and then merge with the Dataframe already created for the path, so the date column would not have the stat() formatting. st_mtime next to datetime.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.