Python-文件操作
1 输出字典
# -*- coding:utf -8 -*- 文件内容: 12,aa 13,bb 14,cc 15,dd def wdict(fname): d = {} try: with open(fname) as fobj: for line in fobj: (key,value) = line.strip().split(',') d[key] = value except Exception,e: print e return d if __name__ == '__main__': print wdict('D:\t\d\1.txt') 结果: {'11': 'aa', '33': 'cc', '22': 'bb'}
2 Linux下目录复制
# -*- coding: utf -8 -*- # 将'/bin/ls'目录复制到'/tmp/ls'目录 src_fname = '/bin/ls' dst_fname = '/tmp/ls' # rb wb 读二进制在windows中用,linux中不需要加 src_fobj = open(src_fname,'rb') dst_fobj = open(dst_fname, 'wb') while True: # 每次最大读取4096字节 data = src_fobj.read(4096) # 如果为空,没有可读取的字节,终止循环 if data == '': break dst_fobj.write(data) src_fobj.close() dst_fobj.close()
3 监控程序
import time point = 0 while True: ipdict={} with open(r'D:\P\T\d\access.log') as fobj: fobj.seek(point) for line in fobj: ip = line.split()[0] if ip not in ipdict: ipdict[ip] = 1 else: ipdict[ip]+=1 point = fobj.tell() for k,v in ipdict.items(): if v > 100: print(k,v) time.sleep(60)
4 遍历两个目录下文件,找出相同的文件
# -*- coding:utf -8 -*- import os def read_file(dirname): try: for root, dirs, files in os.walk(dirname): # print(root) # 目录 # print(dirs) # 目录下的文件夹 # print(files) #目录下的文件 # for dir in dirs: # 遍历文件夹 # print(os.path.join(root, dir)) # for file in files: # 遍历文件 # print(os.path.join(root, file)) # print('----------------') return files except Exception,e: print(e) if __name__ == '__main__': f1 = r'D:\P\T\d' f2 = r'D:\P\T' f1_list = read_file(f1) f2_list = read_file(f2) print(read_file(f1)) print(read_file(f2)) f3_list = [] for i in f1_list:if i in f2_list: f3_list.append(i) # compare_file(os.path.join(f1,i), os.path.join(f2,i)) print(f3_list)
5 比较两个文件的不同之处,输入html文件
# -*- coding: utf-8 -*- import difflib import sys import time import datetime reload(sys) sys.setdefaultencoding('utf-8') # 读取建表语句或配置文件 def read_file(file_name): try: file_desc = open(file_name, 'r') # 读取后按行分割 text = file_desc.read().splitlines() file_desc.close() return text except IOError as e: print('Read input file Error: {0}'.format(e)) sys.exit() # 比较两个文件并把结果生成一份html文本 def compare_file(file1, file2): if file1 == "" or file2 == "": print '文件路径不能为空:第一个文件的路径:{0}, 第二个文件的路径:{1} .'.format(file1, file2) sys.exit() else: print "正在比较文件{0} 和 {1}".format(file1, file2) text1_lines = read_file(file1) text2_lines = read_file(file2) diff = difflib.HtmlDiff() # 创建HtmlDiff 对象 result = diff.make_file(text1_lines, text2_lines) # 通过make_file 方法输出 html 格式的对比结果 # 将结果写入到result_comparation.html文件中 try: now_time = str(datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')) haomiao = int(round(time.time() * 1000)) % 1000 result_compare = '{0}{1}{2}'.format(now_time, haomiao, ".html") with open(result_compare, 'w') as result_file: result_file.write(result) print("0==}==========> Successfully Finished\n")except Exception, e: print(e) # if __name__ == '__main__': # # f1 = r'D:\P\T\d\88.txt' # f2 = r'D:\P\T\88.txt' # compare_file(f1, f2) # print(read_file(f2))
本文来自博客园,作者:术科术,转载请注明原文链接:https://www.cnblogs.com/shukeshu/p/11349446.html