Glob2¶
Use glob2 module for recursive globbing.
glob2.glob('**/*.txt')
File walking (with dates)¶
Standard output¶
#! /usr/bin/env python
import os
import datetime as dt
now = dt.datetime.now()
ago = now-dt.timedelta(minutes=30)
for root, dirs,files in os.walk('.'):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
print('%s modified %s'%(path, mtime))
Find all newly created modified and deleted files in all the dir
#! /usr/bin/env python
import os
import datetime as dt
now = dt.datetime.now()
# ago = now-dt.timedelta(minutes=30)
ago = now-dt.timedelta(days=5)
os.chdir('/home/human1/Downloads/Firefox')
print(os.getcwd())
for root, dirs,files in os.walk('.'):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago and os.path.splitext(fname)[1]=='.jpg':
print('%s modified %s'%(path, mtime))
PrettyTable output¶
#! /usr/bin/env python
import os, textwrap
import datetime as dt
from prettytable import PrettyTable
now = dt.datetime.now()
# ago = now-dt.timedelta(minutes=30)
ago = now-dt.timedelta(days=5)
os.chdir('/home/human1/Downloads/Firefox')
print(os.getcwd())
global listing
listing = []
for root, dirs,files in os.walk('.'):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago and os.path.splitext(fname)[1]=='.jpg':
# print('%s modified %s'%(path, mtime))
listing.append((textwrap.fill(path, width=35), mtime))
x = PrettyTable(["Path", "Modified"])
x.align["Path"] = "l" # Left align city names
x.padding_width = 1 # One space between column edges and contents (default)
for row in listing:
x.add_row(row)
print(x)