3

Has anybody accomplished this with python?

Here's what I have so far...

if os.path.isdir(options.map_file_directory):
    searchedfile = glob.glob("*.map")
    files = sorted( searchedfile, key = lambda file: os.path.getctime(file))

    for i in files:
        print("{} - {}".format(i, time.ctime(os.path.getctime(i))) )
rockymonkey555
  • 7,520
  • 1
  • 13
  • 12

2 Answers2

1

Resolved my own problem. Had to do with the way I was "globbing"

if os.path.isdir(options.map_file_directory):
    print ("this is a test 4")
    searchedfile = glob.glob(r'{}\*.map'.format(options.map_file_directory))
    files = sorted( searchedfile, key = lambda file: os.path.getctime(file))

    for i in files:
        print("{} - {}".format(i, time.ctime(os.path.getctime(i))) )
rockymonkey555
  • 7,520
  • 1
  • 13
  • 12
  • 2
    (1) use `files = glob(os.path.join(options.map_file_directory, "*.map"))` (2) you could sort inplace e.g., by modification date: `files.sort(key=os.path.getmtime)` (3) [`os.path.getctime`](https://docs.python.org/3/library/os.path.html#os.path.getctime) does not (always) return a creation date on POSIX. – jfs Sep 08 '15 at 18:34
  • Got an error with (1). Module object isn't callable. – rockymonkey555 Sep 08 '15 at 19:24
  • `from glob import glob` – jfs Sep 08 '15 at 19:27
0

When you don't want to just sort on the time value but also print it out you can store the time in the list you want to sort and spare the second call to getting the time again:

import os
from glob import iglob

# ...

if os.path.isdir(options.map_file_directory):
    modification_times_and_filenames = sorted(
        (os.path.getmtime(n), n)
        for n in iglob(os.path.join(options.map_file_directory, '*.map'))
    )
    for modification_time, filename in modification_times_and_filenames:
        print('{0} - {1}'.format(filename, modification_time))
BlackJack
  • 4,476
  • 1
  • 20
  • 25