I know how to list all subdirectories and files in a directory tree. But I am looking for way to list all newly created files, modified and (if possible) deleted files in all the directories in a directory tree starting from the root directory.
Asked
Active
Viewed 2.6k times
9
-
Please specify what newly created is for you. Within the last hour? The last day? Since a year? If you know how to build a directory tree, why don't you just use `os.lstat` to access file properties? – hochl Nov 10 '11 at 23:17
-
then use `st=os.lstat(filepath)` and the `st.st_mtime` field and check if the difference to the current time is less than 1800 -- that's it. – hochl Nov 10 '11 at 23:24
3 Answers
20
You could find all files created or modified in the last half-hour by looking at the "mtime" of each file:
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))
To generate a list of deleted files, you'd also have to have a list of files 30 minutes ago.
A more robust alternative is to use a revision control system like git
. Making a commit of all the files in the directory is like making a snapshot. Then the command
git status -s
would list all files that have changed since the last commit. This will list files that have been deleted too.
-
Running the code above, gives the following error: Traceback (most recent call last): File "tsck.py", line 13, in ? print('{p} modified {m}'.format(p=path,m=mtime)) AttributeError: 'str' object has no attribute 'format' – nsh Nov 11 '11 at 00:11
-
There is so slow, we can find another way, activate the system to log the files that newly created, and then parsing the log files. or better way is to add a trigger for new log entry.may help! – pylover Nov 11 '11 at 00:37
-
@nsh: `str.format` was introduced in Python2.6. For earlier version, you could use `%s`-style string formatting. I'll edit my post to show what I mean. – unutbu Nov 11 '11 at 01:17
2
from tempfile import mkstemp
import shutil
import os
import datetime as dt
import sys
# gets the time frame we are going to look back and builds a placeholder list to passover the info from our mtime to slay
now=dt.datetime.now()
ago=now-dt.timedelta(minutes=480)
passover=[]
# the '.' is the directory we want to look in leave it to '.' if you want to search the directory the file currently resides in
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:
passover.append(path)
def slay(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with open(abs_path,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
old_file.close()
#Remove original file
os.remove(file_path)
#Move new file
try:
shutil.move(abs_path, file_path)
except WindowsError:
pass
#we pass the passover list to the slay command in a for loop in order to do muiltple replaces in those files.
for i in passover:
slay(i,"String1","String2")

Powerboy2
- 69
- 5
-
I built this to look into a dir and select the modified files within the last bit of time and then replace text in those files. This script wasn't laying around and i had to piece it together from the answer above so i figured someone else might come looking for it. – Powerboy2 Sep 29 '15 at 21:15
-
Please edit your answer with this info. Also, a complete answer should hace a few lines describing what it does. Please read the following article: [How do I write a good answer?](http://stackoverflow.com/help/how-to-answer) – Mariano Sep 29 '15 at 21:36
0
Take a look at "man find"
create a temp file to compare
example:
find / -type f -newerB tempFile
some part of the man find
-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one
of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are place‐
holders for other letters, and these letters select which time belonging to how reference is used for the comparison.
a The access time of the file reference
B The birth time of the file reference
c The inode status change time of reference
m The modification time of the file reference
t reference is interpreted directly as a time

pylover
- 7,670
- 8
- 51
- 73