[PYTHON] Set the last modified date of the child file to the modified date of the parent directory

Overview

When managing a large number of files divided into folders, if the update date of the latest file directly under the folder is set, it is useful when sorting on Windows, so I wrote it. If you make one, you may not be at a loss when you look at the classification folder.

Source

import os
from pathlib import Path

#Find a directory based on the current path and apply the latest update date of files under that directory.
p = Path("./")
for d in list(p.glob('*')):
    if d.is_dir :
        filedts = []
        for filep in list(d.glob('*')):
            filedts.append((filep.stat()).st_mtime)
        filedts.sort(reverse=True)
        if len(filedts) > 0 :
            print(d.name + ' ' + str(filedts[0]))
            os.utime(d,(filedts[0],filedts[0]))

I am using it with NTFS mounted on Linux. I am using st_mtime, but please read it as atime or ctime as appropriate depending on the file system.

Impressions

The Path object can be used for anything and is convenient. Let's use it steadily.

Recommended Posts

Set the last modified date of the child file to the modified date of the parent directory
[Note] Import of a file in the parent directory in Python
Set the range of active strips to the preview range
[Python] Get the last updated date of the website
Recursively search the specified directory to see the file
Get the update date of the Python memo file.
Various ways to read the last line of a csv file in Python
I want to get the path of the directory where the running file is stored.
Template of python script to read the contents of the file
How to display the modification date of a file in C language up to nanoseconds
[Reintroduction to python] How to import via the parent directory
Set the form DateField to type = date in Django
[Blender] How to dynamically set the selection of EnumProperty
Set the specified column of QTableWidget to ReadOnly StyledItemDelegate
Script to get the expiration date of the SSL certificate
Spit out a list of file name, last modified date and character code in python3
[Ubuntu] How to delete the entire contents of a directory
How to see the contents of the Jupyter notebook ipynb file
How to set the extended iso8601 format date to the Dataframe index
Google search for the last line of the file in Python
File access under the directory
It is surprisingly troublesome to get a list of the last login date and time of Workspaces
Extract the index of the original set list that corresponds to the list of subsets.
Shell script (Linux, macOS) that outputs the date of the last week
Conditional branch due to the existence of a shell script file
[Python] How to get the first and last days of the month
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)
How to output the output result of the Linux man command to a file
Dig the directory and create a list of directory paths + file names
How to set up the development environment of ev3dev [Windows version]
I tried to open the latest data of the Excel file managed by date in the folder with Python
The story of the "hole" in the file
Supplement to the explanation of vscode
How to put a line number at the beginning of a CSV file
View the full path (absolute path) of a file in a directory in Linux Bash
python> Get file last modified date> mddt = time.ctime (os.path.getmtime (filepath))> Mon Mar 7 07:18:45 2016
I made a program to check the size of a file in Python