File log

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bouzy
    New Member
    • Jun 2007
    • 30

    File log

    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).

    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()
    But when I try to use this snippet in a function in a bigger script it gives me this error...

    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
    script I am trying to get to work...

    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()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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.

    Comment

    • Bouzy
      New Member
      • Jun 2007
      • 30

      #3
      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 ...

      Code:
      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)
      4 times?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Try modifying your code to return a list of file data, and write the joined list to file. This is untested.
        [code=Python]def datalog():
        dataList = []
        for root,dirs,files in os.walk(cwd):
        for folder in glob.glob(root) :
        for file in glob.glob(folde r + '/*.**'):
        file_path = os.path.split(f ile)[1]
        size = os.stat(file)
        file_date = time.strftime(" %m/%d/%y %H:%M:%S", time.localtime( ))
        dataList.append ('%s | %s bytes | %s ' % (os.path.split( file_path)[1], size.st_size, file_date))
        return dataList

        if os.path.exists( 'log_file.txt') :
        # Create a 'temporary' file to store current directory's dimensions
        log_file2 = open('log_file2 .txt', 'w')
        log_file2.write ('\n'.join(data log()))
        log_file2.close ()[/code]If you want the date each file was last modified, you need to do this:[code=Python]file_date = time.strftime(" %m/%d/%Y %H:%M:%S", time.localtime( os.path.getmtim e(fn)))[/code]

        Comment

        Working...