“Python和运维”的版本间的差异
跳到导航
跳到搜索
第1行: | 第1行: | ||
=python常用的运维脚本= | =python常用的运维脚本= | ||
+ | ==从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看 == | ||
+ | <pre> | ||
+ | |||
+ | |||
+ | |||
+ | 数据处理与脚本 之 Python 脚本,从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看。 | ||
+ | |||
+ | |||
+ | # errmv.py | ||
+ | def extract_errors(log_file,output_file): | ||
+ | with open(log_file,'r') as infile, open(output_file,'w') as outfile: | ||
+ | for line in infile: | ||
+ | # //if "ERROR" in line: | ||
+ | if "failed" in line: | ||
+ | outfile.write(line) | ||
+ | |||
+ | extract_errors('nglog.log','error.log') | ||
+ | |||
+ | |||
+ | ➜ tmp cat nglog.log | ||
+ | 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 | ||
+ | 2024/09/06 17:44:59 [notice] 65501#65501: using inherited sockets from "" | ||
+ | |||
+ | tmp py3 errmv.py | ||
+ | |||
+ | |||
+ | ➜ tmp cat error.log | ||
+ | 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 | ||
+ | |||
+ | </pre> | ||
+ | |||
== py3 批量修改文件后缀名 或者去掉后缀名== | == py3 批量修改文件后缀名 或者去掉后缀名== | ||
2024年10月22日 (二) 03:12的版本
目录
python常用的运维脚本
从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看
数据处理与脚本 之 Python 脚本,从日志文件中提取所有的错误日志并存储到另一个文件中 方便给开发人员查看。 # errmv.py def extract_errors(log_file,output_file): with open(log_file,'r') as infile, open(output_file,'w') as outfile: for line in infile: # //if "ERROR" in line: if "failed" in line: outfile.write(line) extract_errors('nglog.log','error.log') ➜ tmp cat nglog.log 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61 2024/09/06 17:44:59 [notice] 65501#65501: using inherited sockets from "" tmp py3 errmv.py ➜ tmp cat error.log 2024/09/06 17:42:07 [emerg] 64755#64755: open() "/etc/nginx/sites-enabled/default" failed (2: No such file or directory) in /etc/nginx/nginx.conf:61
py3 批量修改文件后缀名 或者去掉后缀名
#!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 注意 需要把脚本放在与修改的文件同一目录下,因为filenames只是获取了程序目录的文件名。 import os # 列出当前目录下所有的文件 files = os.listdir('.') for filename in files: portion = os.path.splitext(filename) # 如果后缀是.dat if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" #os.rename(filename,newname) os.replace(filename,newname) # As of Python version 3.3 and later, it is generally preferred to use os.replace instead of os.rename so FileExistsError is not raised if the destination file already exists. #!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 进入目录的版本 利用os.chdir import os import sys os.chdir(r'~/tmp') # 列出当前目录下所有的文件 files = os.listdir('./') print('files',files) for filename in files: portion = os.path.splitext(filename) # 如果后缀是.txt if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" os.rename(filename,newname) #os.rename(filename,newname)
python批量更换后缀名 会返回脚本所在目录的版本
#!/usr/bin/python3 # -*- coding:utf-8 -*- # python批量更换后缀名 会返回脚本所在目录的版本 import os import sys # Set the path path ='~' # save current working directory saved_cwd=os.getcwd() # change your cwd to the directory which contains files os.chdir(path) # 列出当前目录下所有的文件 files = os.listdir('./') print('files',files) for filename in files: portion = os.path.splitext(filename) # 如果后缀是.txt if portion[1] == ".txt": # 这里是把 .txt去掉 重新组合文件名和后缀名 newname = portion[0] #txt 文件改为sh 要用这个记得打开下行注释 #newname = portion[0] + ".sh" os.rename(filename,newname) #os.rename(filename,newname) os.chdir(saved_cwd) # https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python#2491232
os.path.splitext
os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
https://www.cnblogs.com/rychh/articles/9745932.html
https://blog.csdn.net/u011509971/article/details/70244688