File log

我正在尝试编写一个脚本来检查文件夹中的文件,然后在以后查看这些文件是否已更改。
我用这个来制作原始文件(包含文件、文件大小和日期的数据记录)。

选择 | 换行 | 行号
  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()

但是,当我试图在一个更大的脚本的函数中使用这个代码片段时,它给出了这个错误...

选择 | 换行 | 行号
  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

我正试着开始工作的剧本...

选择 | 换行 | 行号
  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()
# 回答1


变量
数据
是本地的
数据记录()
剧本永远不会看到
数据
除非您将其返回到模块全局变量,或者将导致错误的代码移到函数中。
# 回答2


关键是,我不想把代码打4次,然后把脚本写得很长。(我需要在每次输入datalog()时输入它)有没有一种方法可以通过不需要输入来缩短代码...

选择 | 换行 | 行号
  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次?

# 回答3


尝试修改代码以返回文件数据列表,并将联接列表写入文件。这是未经测试的。

选择 | 换行 | 行号
  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()

如果您需要上次修改每个文件的日期,则需要执行以下操作:

选择 | 换行 | 行号
  1. file_date = time.strftime("%m/%d/%Y %H:%M:%S", time.localtime(os.path.getmtime(fn)))

标签: python

添加新评论