Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
#http://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python
from stat import S_ISREG, ST_MTIME, ST_MODE
import os, sys, time
import bisect
RING_BUFFER_SIZE = 5
# path to the directory (relative or absolute)
dirpath = "/space/projects/Live_Viewer/target/local"
# get all entries in the directory
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
# get the corresponding stats
entries = ((os.stat(path), path) for path in entries)
# leave only regular files, insert modification date
entries = [[stat[ST_MTIME], path]
for stat, path in entries if S_ISREG(stat[ST_MODE])]
entries = sorted(entries, reverse=True)
len_entries = len(entries)
print entries
#print entries
#print len_entries
#targetFilepath = "/space/projects/Live_Viewer/test.tif"
#entries[:0] = [[os.stat(path)[ST_MTIME], targetFilepath]]
#print "after prepend"
#print entries
filename = "/space/projects/Live_Viewer/test.tif"
#fileModTime = os.stat(filename)[ST_MTIME]
fileModTime = 1436956680
entries[:0] = [[fileModTime, filename]]
print "after insort"
print entries
entries = sorted(entries, reverse=True)
print "sort again"
print entries
if len_entries > RING_BUFFER_SIZE:
print "files to remove"
for mod_time, path in entries[RING_BUFFER_SIZE:]:
print mod_time, path
pass
# os.remove(path)
# entries.remove([mod_time, path])
print
print "content"
for cdate, path in entries:
print time.ctime(cdate), os.path.basename(path)