I am trying to make a script to check files in a folder, then see later if the files have been changed at all..
I have this working to make the original file (data log with files,file_size s, and dates).
But when I try to use this snippet in a function in a bigger script it gives me this error...
script I am trying to get to work...
I have this working to make the original file (data log with files,file_size s, and dates).
Code:
log_file = open('log_file.txt', 'w')
for root,dirs,files in os.walk(cwd):
for folder in glob.glob(root):
for file in glob.glob(folder + '/*.**'):
file_path = os.path.split(file)[1]
size = os.stat(file)
file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
log_file.write(data+'\n')
log_file.close()
Code:
Traceback (most recent call last):
File "C:\Documents and Settings\Scripts\testing_grounds\Log_file\log_file.py", line 27, in <module>
log_file2.write(data+'\n')
NameError: name 'data' is not defined
Code:
#!/usr/python/bin
# Filename: log_file.py
"""
This file takes a file in a directory and writes a log file containing all the filenames, modification times and filesizes. Then on a later date, the program will check this log file to detect any changes in the directory
"""
import os, glob, time, filecmp, tempfile
cwd = os.getcwd()
def datalog():
for root,dirs,files in os.walk(cwd):
for folder in glob.glob(root):
for file in glob.glob(folder + '/*.**'):
file_path = os.path.split(file)[1]
size = os.stat(file)
file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
if os.path.exists('log_file.txt'):
# Create a 'temporary' file to store current directory's dimensions
log_file2 = open('log_file2.txt', 'w')
datalog()
log_file2.write(data+'\n')
log_file2.close()
# Compair the directory's current dimensions to previous modifications
if filecmp.cmp('log_file.txt', 'log_file2.txt'):
print 'No files have been modified'
os.remove('log_file2.txt')
else:
print 'The directory has been modified'
done = raw_input("When done compariing the logs type 'done' ")
if done == done:
os.remove('log_file.txt')
os.remove('log_file2.txt')
log_file = open('log_file.txt', 'w')
datalog()
log_file.write(data+'\n')
log_file.close()
else:
log_file = open('log_file.txt', 'w')
log_file = open('log_file.txt', 'w')
log_file.write(data+'\n')
log_file.close()
Comment