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))