python获取文件修改时间 错误_用 python3 写的检测文件是否改动的脚本运行出现错误,求大神解答...

# -*- coding:UTF-8 -*-import os

import os.path

import datetime

import stat

import shutil

import operator

# 文件全路径和对应最后修改时间写入到out.txt文档中;def add_log(path):

with open('out.txt', 'w') as f:

f.close

for root, dirs, files in os.walk(path):

for name in files:

temp_path = os.path.join(root, name)

file_name = temp_path.replace('F:/build_log/', '')

file_time = os.stat(temp_path).st_mtime

with open('out.txt', 'a') as f:

f.write(','.join(['%s' % file_name, '%s' % file_time]))

f.close()

def if_exit():

# 判断文件out.txt是否存在,不存在则创建 filename = 'out.txt'

if os.path.exists(filename):

message = 'Ok, the "%s" file exists '

else:

message = "Sorry, I cannot find the '%s' file..and I create it."

a = open('out.txt', 'w')

a.close()

print(message % filename)

files_name = 'update'

if os.path.exists(files_name):

message = 'OK, the "%s" file exists.'

else:

message = "Sorry, I cannot find the '%s' file.and I create it. "

os.mkdir('update')

print(message % files_name)

# path 待比较的文件夹路径

# 返回生成的txt(包含更新或者添加的文件路径)的路径def log_compare(path):

if_exit()

# 获取out.txt文件内容(文件全路径key和最后修改时间value),生成dict txt = open('out.txt', 'r+').readlines

myDic = {}

for row in txt:

(key, value) = row.split(',')

myDic[key] = value

print(myDic)

# 创建以时间命名的文件和文件夹 setup_filename = str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))

setup_file_path = '%s%s.txt' % ('F:/Test_log/', setup_filename)

setup_file_dir = '%s%s' % ('F:/Test_log/', setup_file_path)

# 判断key,比较value值是否变化rs/ # 原始需要有一个out.txt文件,才能比较value确定是否有更新 # 运行程序时,重新遍历一遍文件全路径和最后修改时间 for root, dirs, files in os.walk(path):

for name in files:

temp_path = os.path.join(root, name)

file_name = temp_path.replace('F:/build_log/', '')

time = os.stat(temp_path).st_mtime # 获取最后的修改时间 file_time = '%s\n' % time

# 加%s\n是为了与out.txt里值完全对应 if file_name in myDic:

if operator.eq(myDic[file_time], file_time):

print(

file_name,

file_time,

)

with open(setup_file_path, 'a') as f:

f.write('%s\n' % file_name)

f.close()

else:

print('add', file_name)

with open(setup_file_path, 'a') as f:

f.write('%s\n' % file_name)

f.close()

return (setup_filename, setup_file_dir, setup_file_path)

# 将src目录中的内容拷贝到dest目录

# 如果dest或者其子目录不存在,先创建

# txt_path为更新日志路径,有更新的文件才拷贝def copy_directory(src, dest, txt_path):

if not os.path.exists(txt_path):

print("no file update")

return

# 读更新日志,获取更新文件的全路径

txt = open(txt_path, 'r').readlines()

myDic = {}

myDic2 = {}

for row in txt:

myDic[row] = "1"

tempArray = os.path.split(row)

key = tempArray[0]

myDic2[key] = "1"

print("myDic2:", myDic2)

print("dict:", myDic)

# 遍历原始文件夹,得到所有文件的全路径 for root, dirs, files in os.walk(src):

for name in files:

# print "dirs:",dirs fpath = os.path.join(root, name)

newroot = root

newroot = newroot.replace(src,

dest) # 根据文件绝对路径,创建将要拷贝的路径(相对路径),没有则创建 # print newroot rel_dir = root.replace('F:/build_log/', '')

if not os.path.exists(newroot) and rel_dir in myDic2:

print("rel_dir:", rel_dir)

print(newroot)

os.makedirs(newroot)

os.chmod(newroot, stat.S_IWRITE)

temp = fpath

temp = temp.replace(src, dest)

rel_path = fpath.replace('F:/build_log/',

'') # 将绝对路径改为相对路径,便于遍历对比,挑出要拷贝的文件 rel_path += '\n'

if rel_path in myDic:

print("real_path:", rel_path)

# os.mkdir(rel_path) shutil.copy(fpath, temp)

print("copyfile:", fpath)

def main():

path_dir = 'F:/build_log/'

params = log_compare(path_dir)

add_log(path_dir)

copy_directory(path_dir, params[1], params[2])

if __name__ == '__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值