patternpythonMinor
Selecting folders based on last modified date
Viewed 0 times
lastfoldersdatemodifiedbasedselecting
Problem
I am attempting to find folders based on their last modified dates.
This is what I've got and it seems to be right in that they're comparing the same attributes. If there is a better, more Pythonic way for this, please advise.
import os, shutil, stat, errno, time, datetime
srcFolders = "C:\Users\Test\Desktop\Test"
archiveDate = datetime.datetime.strptime("2016/11/20", '%Y/%m/%d')
os.chdir(srcFolders)
for name in os.listdir('.'):
if os.path.isdir(name):
modifiedDate = time.strftime('%Y/%m/%d', time.gmtime(os.path.getmtime(name)))
strLastModified = datetime.datetime.strptime(modifiedDate, '%Y/%m/%d')
if strLastModified > archiveDate:
print nameThis is what I've got and it seems to be right in that they're comparing the same attributes. If there is a better, more Pythonic way for this, please advise.
Solution
-
Do not put all imports on the same line and use snake_case rather than camelCase for variable names.
Read PEP8, the official Python style guide to make your code look like Python code.
-
Use functions for better reusability. Instead of using hardcoded values like your folder path or archive date, use parameters. You will more easily be able to test various values in the interactive interpreter.
This also means using the
-
return values instead of printing them in the function. Again, better reusability.
For starter you can build a list and return it.
-
when using
-
you don't need to move to the desired directory before listing its content.
Revised code
Is a first aproach. But using
Do not put all imports on the same line and use snake_case rather than camelCase for variable names.
Read PEP8, the official Python style guide to make your code look like Python code.
-
Use functions for better reusability. Instead of using hardcoded values like your folder path or archive date, use parameters. You will more easily be able to test various values in the interactive interpreter.
This also means using the
if __name__ == '__main__' construct.-
return values instead of printing them in the function. Again, better reusability.
For starter you can build a list and return it.
-
when using
datetime objects, you can compare them directly. It is cleaner and probably faster than comparing their string representation. Building dates is also as simple as using the right constructor.-
you don't need to move to the desired directory before listing its content.
os.listdir can take the absolute path and work from that.Revised code
import os
import datetime
def filter_by_date(src_folder, archive_date):
relevant_folders = []
for name in os.listdir(src_folder):
full_name = os.path.join(src_folder, name)
if os.path.isdir(full_name):
if datetime.fromtimestamp(os.path.getmtime(full_name)) > archive_date:
relevant_folders.append(name)
return relevant_folders
if __name__ == '__main__':
print filter_by_date("C:\Users\Test\Desktop\Folder", datetime.datetime(2016, 11, 10))Is a first aproach. But using
append in a for loop is deemed unPythonic, better use a generator or a list-comprehension here:import os
import datetime
def filter_by_date(src_folder, archive_date):
os.chdir(src_folder)
return [
name for name in os.listdir('.')
if os.path.isdir(name)
and datetime.fromtimestamp(os.path.getmtime(name)) > archive_date
]
if __name__ == '__main__':
print filter_by_date("C:\Users\Test\Desktop\Folder", datetime.datetime(2016, 11, 10))Code Snippets
import os
import datetime
def filter_by_date(src_folder, archive_date):
relevant_folders = []
for name in os.listdir(src_folder):
full_name = os.path.join(src_folder, name)
if os.path.isdir(full_name):
if datetime.fromtimestamp(os.path.getmtime(full_name)) > archive_date:
relevant_folders.append(name)
return relevant_folders
if __name__ == '__main__':
print filter_by_date("C:\Users\Test\Desktop\Folder", datetime.datetime(2016, 11, 10))import os
import datetime
def filter_by_date(src_folder, archive_date):
os.chdir(src_folder)
return [
name for name in os.listdir('.')
if os.path.isdir(name)
and datetime.fromtimestamp(os.path.getmtime(name)) > archive_date
]
if __name__ == '__main__':
print filter_by_date("C:\Users\Test\Desktop\Folder", datetime.datetime(2016, 11, 10))Context
StackExchange Code Review Q#149045, answer score: 4
Revisions (0)
No revisions yet.