最近在Linux下测试芯片产生大量log文件,但是matlab需要操作txt,需要一步一步转换。。。
出于偷懒考虑,参考了一个python程序,实现同目录下的log转txt快速实现。 # -*- coding: utf-8 -*- """ Created on Thu Mar 28 12:58:44 2019 @author: yuanpeng """ import os f_path = r'C:\Users\yuanpeng\Downloads\cececx' def find_file(file_path, o_post, n_post, lis): ls = os.listdir(file_path) for i in ls: son_path = os.path.join(file_path,i) if os.path.isdir(son_path): find_file(son_path,o_post,n_post,lis) else: file_post = str(i.split('.')[-1]) if file_post == o_post: lis.append(i) os.rename(son_path,str(son_path.split('.')[0])+'.'+n_post) print('找到文件{srcnam},已修改成:{dicname}'.format(srcnam=son_path,dicname=str(i.split('.')[0])+'.'+n_post)) return lis old_post = 'log' new_post = 'txt' print('已修改文件:',find_file(f_path, old_post, new_post, []))