By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

File log

P: 30
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_sizes, and dates).

Expand|Select|Wrap|Line Numbers
  1. log_file = open('log_file.txt', 'w')
  2.  
  3. for root,dirs,files in os.walk(cwd):   
  4.    for folder in glob.glob(root):
  5.       for file in glob.glob(folder + '/*.**'):
  6.          file_path = os.path.split(file)[1]
  7.          size = os.stat(file)
  8.          file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  9.          data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
  10.          log_file.write(data+'\n')
  11.  
  12. log_file.close()
But when I try to use this snippet in a function in a bigger script it gives me this error...

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Documents and Settings\Scripts\testing_grounds\Log_file\log_file.py", line 27, in <module>
  3.     log_file2.write(data+'\n')
  4. NameError: name 'data' is not defined
script I am trying to get to work...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/python/bin
  2. # Filename: log_file.py
  3.  
  4. """
  5. 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
  6. """
  7.  
  8. import os, glob, time, filecmp, tempfile
  9.  
  10. cwd = os.getcwd()
  11.  
  12. def datalog():
  13.  
  14.    for root,dirs,files in os.walk(cwd):   
  15.       for folder in glob.glob(root):
  16.          for file in glob.glob(folder + '/*.**'):
  17.             file_path = os.path.split(file)[1]
  18.             size = os.stat(file)
  19.             file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  20.             data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
  21.  
  22. if os.path.exists('log_file.txt'):
  23.  
  24.    # Create a 'temporary' file to store current directory's dimensions
  25.    log_file2 = open('log_file2.txt', 'w')
  26.    datalog()
  27.    log_file2.write(data+'\n')
  28.    log_file2.close()
  29.  
  30.    # Compair the directory's current dimensions to previous modifications
  31.    if filecmp.cmp('log_file.txt', 'log_file2.txt'):
  32.       print 'No files have been modified'
  33.       os.remove('log_file2.txt')
  34.  
  35.    else:
  36.       print 'The directory has been modified'
  37.       done = raw_input("When done compariing the logs type 'done' ")
  38.       if done == done:
  39.          os.remove('log_file.txt')
  40.          os.remove('log_file2.txt')
  41.          log_file = open('log_file.txt', 'w')
  42.          datalog()
  43.          log_file.write(data+'\n')
  44.          log_file.close()
  45.  
  46. else:
  47.  
  48.    log_file = open('log_file.txt', 'w')
  49.    log_file = open('log_file.txt', 'w')
  50.    log_file.write(data+'\n')
  51.    log_file.close()
Nov 24 '08 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Variable data is local to datalog().The script will never see data unless you return it to a module global variable, or move the code causing the error into the function.
Nov 24 '08 #2

P: 30
The point was though I didn't want to have to type that code 4 times and make the script really long. (I would need it typed every time I typed datalog()) Is there a way to shorten the code by not needing to type ...

Expand|Select|Wrap|Line Numbers
  1. for root,dirs,files in os.walk(cwd):   
  2.    for folder in glob.glob(root):
  3.       for file in glob.glob(folder + '/*.**'):
  4.          file_path = os.path.split(file)[1]
  5.          size = os.stat(file)
  6.          file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  7.          data = '%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date)
4 times?
Nov 24 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Try modifying your code to return a list of file data, and write the joined list to file. This is untested.
Expand|Select|Wrap|Line Numbers
  1. def datalog():
  2.     dataList = []
  3.     for root,dirs,files in os.walk(cwd):   
  4.         for folder in glob.glob(root):
  5.             for file in glob.glob(folder + '/*.**'):
  6.                 file_path = os.path.split(file)[1]
  7.                 size = os.stat(file)
  8.                 file_date = time.strftime("%m/%d/%y %H:%M:%S", time.localtime())
  9.                 dataList.append('%s | %s bytes | %s ' % (os.path.split(file_path)[1], size.st_size, file_date))
  10.     return dataList
  11.  
  12. if os.path.exists('log_file.txt'):
  13.     # Create a 'temporary' file to store current directory's dimensions
  14.     log_file2 = open('log_file2.txt', 'w')
  15.     log_file2.write('\n'.join(datalog()))
  16.     log_file2.close()
If you want the date each file was last modified, you need to do this:
Expand|Select|Wrap|Line Numbers
  1. file_date = time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(os.path.getmtime(fn)))
Nov 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.