1、遍历目录及文件方法
1)根据时间遍历指定目录下的文件
[root@04 b2c]# cat file_list.py
# !/usr/bin/env python
# coding: utf-8
import os
def get_file_list(file_path):
dir_list = os.listdir(file_path)
if not dir_list:
return
else :
# 注意,这里使用lambda表达式,将文件按照最后修改时间顺序升序排列
# os.path.getmtime() 函数是获取文件最后修改时间
# os.path.getctime() 函数是获取文件最后创建时间
dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
# print(dir_list)
return dir_list
print get_file_list(' /logarchive/b2c ' )
执行
[root@ 04 b2c]# python file_list.py
[' 2019-05-29.log ' , ' 2019-05-30.log ' , ' b2c_input.py ' , ' 2019-05-31.log ' , ' 2019-06-01.log ' , ' 2019-06-02.log ' , ' 2019-06-03.log ' , ' 2019-06-04.log ' , ' 2019-06-05.log ' , ' 2019-06-06.log ' , ' 2019-06-07.log ' , ' 2019-06-08.log ' , ' 2019-06-09.log ' , ' 2019-06-10.log ' , ' 2019-06-11.log ' , ' 2019-06-12.log ' , ' 2019-06-13.log ' , ' 2019-06-14.log ' , ' 2019-06-15.log ' , ' 2019-06-16.log ' , ' 2019-06-17.log ' , ' 2019-06-18.log ' , ' 2019-06-19.log ' , ' 2019-06-20.log ' , ' 2019-06-21.log ' , ' 2019-06-22.log ' , ' 2019-06-23.log ' , ' 2019-06-24.log ' , ' file.py ' , ' file_list.py ' ]
2)如果过滤以log为结尾的文件
方法一:
[root@ 04 b2c]# cat file_list.py
# !/usr/bin/env python
# coding: utf-8
import os
def get_file_list(file_path):
dir_list = os.listdir(file_path)
if not dir_list:
return
else :
# 注意,这里使用lambda表达式,将文件按照最后修改时间顺序升序排列
# os.path.getmtime() 函数是获取文件最后修改时间
# os.path.getctime() 函数是获取文件最后创建时间
dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
for filename in dir_list:
if filename.endswith(" .log " ):
print filename
get_file_list( ' /logarchive/b2c ' )
[root@app04 b2c] # python file_list.py
2019-05-29.log
2019-05-30.log
2019-05-31.log
2019-06-01.log
2019-06-02.log
2019-06-03.log
2019-06-04.log
2019-06-05.log
2019-06-06.log
2019-06-07.log
2019-06-08.log
2019-06-09.log
2019-06-10.log
2019-06-11.log
2019-06-12.log
2019-06-13.log
2019-06-14.log
2019-06-15.log
2019-06-16.log
2019-06-17.log
2019-06-18.log
2019-06-19.log
2019-06-20.log
2019-06-21.log
2019-06-22.log
2019-06-23.log
2019-06-24.log
方法二:
# !/usr/bin/env python
# coding: utf-8
import os, glob, time
# 定义要清理文件的目录
root = ' /logarchive/b2c '
_list = []
for folder in glob.glob(root):
for _log_name in glob.glob(folder + ' /*.log ' ):
_list.append(_log_name)
filelist = sorted(_list)
for log_name in filelist:
print log_name
f = open(log_name, ' r ' )
content = f.readlines()
f.close()
_time = []
for item in content:
time = int(item.strip().split(' >>> ' )[-1].split(' ms ' )[0])
_time.append(time)
access_time = sorted(_time)
print (" b2c 运价访问时间: " )
print (" min: %s ms " % access_time[0])
print (" max: %s ms " % access_time[-1])
sum = 0
access_count = len(_time)
for i in _time:
sum += i
avg = sum/access_count
print (" avg: %s ms " % avg)
print ' - ' *50
print filelist
方法1:
import glob
import os
os.chdir(“./”)
for file in glob.glob(“*.py”):
print file
方法2:
for file in os.listdir(“./”):
if file.endswith(“.py”):
print file
方法3:
for root, dirs, files in os.walk(“./”):
for file in files:
if file.endswith(“.py”):
print os.path.join(root, file)
4) os.walk()方法
import os
def all_path(dirname):
result = []# 所有的文件
for maindir, subdir, file_name_list in os.walk(dirname):
print (" 1: " ,maindir) # 当前主目录
print (" 2: " ,subdir) # 当前主目录下的所有目录
print (" 3: " ,file_name_list) # 当前主目录下的所有文件
for filename in file_name_list:
apath = os.path.join(maindir, filename)# 合并成一个完整路径
result.append(apath)
return result
print (all_path(" E:\myTest " ))
5) python遍历目录下的所有文件和目录
目录结构如下图:
test ---a------d------g--------g.txt
test ---a------d------a.txt
test ---a------e
--------b
--------c
--------1.txt
--------2.txt
一、使用os.walk遍历所有的目录和文件
1、获取test目录下的所有文件
for root,dirs,files in os.walk(r" D:\test " ):
for file in files:
# 获取文件所属目录
print (root)
# 获取文件路径
print (os.path.join(root,file))
2、获取test目录下的所有目录
for root,dirs,files in os.walk(r" D:\test " ):
for dir in dirs:
# 获取目录的名称
print (dir)
# 获取目录的路径
print (os.path.join(root,dir))
二、利用os.listdir递归获取所有的目录路径和文件路径
def get_file_path(root_path,file_list,dir_list):
# 获取该目录下所有的文件名称和目录名称
dir_or_files = os.listdir(root_path)
for dir_file in dir_or_files:
# 获取目录或者文件的路径
dir_file_path = os.path.join(root_path,dir_file)
# 判断该路径为文件还是路径
if os.path.isdir(dir_file_path):
dir_list.append(dir_file_path)
# 递归获取所有文件和目录的路径
get_file_path(dir_file_path,file_list,dir_list)
else :
file_list.append(dir_file_path)
if __name__ == " __main__ " :
# 根目录路径
root_path = r" D:\test "
# 用来存放所有的文件路径
file_list = []
# 用来存放所有的目录路径
dir_list = []
get_file_path(root_path,file_list,dir_list)
print (file_list)
print (dir_list)
转自
python 过滤出某后缀名文件 - 快乐多巴胺 - 博客园 https://www.cnblogs.com/pscc/p/10122517.html
python按时间排序目录下的文件 - Mr番茄蛋的博客 - CSDN博客 https://blog.csdn.net/qq_35203425/article/details/80903348
python遍历目录下的所有文件和目录详细介绍 - 修炼之路 - CSDN博客 https://blog.csdn.net/sinat_29957455/article/details/82778306
Windows列出磁盘文件及文件夹大小
简单版:提取指定盘和大小的文件及文件夹
# !/usr/bin/env python
# coding: utf-8
# 版本:提取指定盘和大小的文件及文件夹
import os
from os.path import join, getsize
def get_paths_size(dirs, maxnum):
# 提取指定文件夹和大小的函数
print (" {} -> 文件夹内文件占用空间: " .format(dirs))
size = 0
for root, dirs, files in os.walk(dirs):
sums = sum([getsize(join(root, file)) for file in files]) // 1024 // 1024
if sums > maxnum:
print (' {:>8,d} MB -> {} ' .format(sums, root))
size += sums
print (' {:>8,d} MB -> 总大小 ' .format(size))
def get_files_size(dirs, maxnum):
# 提取指定文件夹内文件和大小的函数
print (" {} -> files usage of disk: " .format(dirs))
size = 0
for root, dirs, files in os.walk(dirs):
for file in files:
fpth = join(root, file)
sums = getsize(fpth) // 1024 // 1024
if sums > maxnum:
print (' {:>8,d} MB -> {} ' .format(sums, fpth))
size += sums
print (' {:>8,d} MB -> 总大小 ' .format(size))
def main():
paths = r' C:\\ '
numbs = 1024 # -> MB
# paths = input(r'请输入盘符(如:D:\Python\Python38:')
# numbs = int(input(r'请)输入大小单位MB(如:1000):'))
get_paths_size(paths, numbs)
get_files_size(paths, numbs)
if __name__ == ' __main__ ' :
main()
高级版:直接提取C-I盘所有大于1GB(参数可以修改)的文件及文件夹,输出到CSV文件
# -*- coding:utf-8 -*-
import os
from os.path import join, getsize
import csv
"""
版本:直接提取C-I盘所有大于1GB的文件及文件夹
并输出CSV文件
盘符不存在则程序执行结束!
文件夹:size_C_path.csv
文件:size_C_file.csv
"""
def get_dirs_size(dirs, maxnum):
print (dirs)
# CSV文件名后缀
fname = dirs.replace(' \\ ' , ' _ ' ).replace(' : ' , '' ).replace(' / ' , ' _ ' )
path_size = [] # 路径大小列表
file_size = [] # 文件大小列表
size = 0 # 合计
for root, dirs, files in os.walk(dirs):
for f in files:
fp = join(root, f)
try :
su = getsize(fp) // 1024 // 1024
except :
pass
# continue
# su = getsize(fp) // 1024 // 1024
if su > maxnum:
file_size.append([su, fp])
print (' {:>8,d} MB --> {} ' .format(su, fp))
pass
sums = sum([getsize(join(root, file)) for file in files]) // 1024 // 1024
size += sums
if sums > maxnum:
path_size.append([sums, root])
print (' {:>8,d} MB --> {} ' .format(sums, root))
pass
print (' {:>8,d} MB --> {} ' .format(size, dirs))
# 调用导出CSV函数导出CSV文件
savecsvfile(path_size, [' 大小 ' , ' 文件夹 ' ], ' size_{}path.csv ' .format(fname))
savecsvfile(file_size, [ ' 大小 ' , ' 文件 ' ], ' size_{}file.csv ' .format(fname))
def savecsvfile(rows, header, csv_name):
# 导出CSV文件函数
# if not os.path.exists(csv_name):
with open(csv_name, ' w ' , newline='' , encoding=' utf-8 ' ) as f:
fc = csv.writer(f)
fc.writerow(header)
fc.writerows(rows)
print (csv_name, ' 导出成功! ' )
def main():
# 所有盘符列表推导式:从C盘到I盘
paths = [" {}:/ " .format(x) for x in ' CDEFGHI ' ]
for p in paths:
if not os.path.exists(p):
print (' 盘符 -> {} 不存在! ' .format(p))
continue
# 只提取大于1000MB的文件和文件夹
get_dirs_size(p, 1000)
if __name__ == ' __main__ ' :
main()
2、时间datetime、time模块
# 时间格式转换
time.strftime(' %Y-%m-%d %H:%M:%S ' )
' 2016-11-12 01:20:54 '
# 获取昨天日期
date = datetime.date.today()
>>> date.today() - timedelta(days=1)
datetime.date( 2016, 11, 11)
>>> date.isoformat(date.today() - timedelta(days=1))
' 2016-11-11 '
# 获取明天日期
>>> date.today() + timedelta(days=1)
datetime.date( 2016, 11, 13)
>>> date.isoformat(date.today() + timedelta(days=1))
' 2016-11-13 '
import datetime
# 获取前100天日期
(datetime.datetime.now() - datetime.timedelta(days = 100)).strftime(" %Y-%m-%d " )
Out[ 18]: ' 2019-12-13 '
# 获取昨天日期
(datetime.datetime.now() - datetime.timedelta(days = 1)).strftime(" %Y-%m-%d " )
Out[ 19]: ' 2020-03-21 '
3、多进程
from multiprocessing import Pool, current_process
import urllib2
urls = [
' http://www.baidu.com ' ,
' http://www.jd1.com ' ,
' http://www.sina.com ' ,
' http://www.163.com ' ,
]
def status_code(url):
print ( ' process name: ' , current_process().name)
try :
req = urllib2.urlopen(url, timeout=2)
return req.getcode()
except urllib2.URLError:
return
p = Pool(processes=4)
for url in urls:
r = p.apply_async(status_code, args=(url,))
# print r.get()
# if r.get(timeout=1) == 200:
if r.get() == 200:
print (" %s OK " %url)
else :
print (" %s NO " %url)
转自 博客地址:http://lizhenliang.blog.51cto.com and https://yq.aliyun.com/u/lizhenliang
4、如果文件不存在就创建
if not os.path.exists(' host_info ' ):
f = open(' host_info ' , ' w ' )
f.close()
5、压缩文件
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import tarfile
tar = tarfile.open(" /root/test/test.tar.gz " ," w:gz " ) # 创建压缩包名
for path,dir ,files in os.walk(" /root/test " ): # 递归文件目录
for file in files:
fullpath = os.path.join (path,file )
tar .add(fullpath) # 创建压缩包
tar .close()
3种压缩方法
gzip [解压缩gzip 删除原文件]
#压缩gzip
import gzip
f_in = open(' file.log ' , ' rb ' )
f_out = gzip .open(' file.log.gz ' , ' wb ' )
f_out.writelines(f_in)
f_out.close()
f_in.close()
#压缩gzip
File = ' xuesong_18.log '
g = gzip .GzipFile(filename="" , mode=' wb ' , compresslevel=9 , fileobj=open((r' %s.gz ' %File),' wb ' ))
g. write (open(r' %s ' %File).read())
g.close()
#解压gzip
g = gzip .GzipFile(mode=' rb ' , fileobj=open((r' xuesong_18.log.gz ' ),' rb ' ))
open((r ' xuesong_18.log ' ),' wb ' ).write (g.read())
tarfile [归档压缩tar.gz 保留原文件]
# 压缩tar.gz
import os
import tarfile
tar = tarfile.open(" /tmp/tartest.tar.gz " ," w:gz " ) # 创建压缩包名
for path,dir ,files in os.walk(" /tmp/tartest " ): # 递归文件目录
for file in files:
fullpath = os.path.join (path,file )
tar .add(fullpath) # 创建压缩包
tar .close()
# 解压tar.gz
import tarfile
tar = tarfile.open(" /tmp/tartest.tar.gz " )
# tar .extract(" /tmp " ) # 全部解压到指定路径
names = tar .getnames() # 包内文件名
for name in names:
tar .extract(name,path=" ./ " ) # 解压指定文件
tar .close()
zipfile [解压缩zip 最大2G]
# 压缩zip
import zipfile,os
f = zipfile.ZipFile(' filename.zip ' , ' w ' ,zipfile.ZIP_DEFLATED) # ZIP_STORE 为默认表不压缩. ZIP_DEFLATED 表压缩
#f. write (' file1.txt ' ) # 将文件写入压缩包
for path,dir ,files in os.walk(" tartest " ): # 递归压缩目录
for file in files:
f. write (os.path.join (path,file )) # 将文件逐个写入压缩包
f.close()
# 解压zip
if zipfile.is_zipfile(' filename.zip ' ): # 判断一个文件是不是zip文件
f = zipfile.ZipFile(' filename.zip ' )
for file in f.namelist(): # 返回文件列表
f.extract( file , r' /tmp/ ' ) # 解压指定文件
#f.extractall() # 解压全部 # 如果文件大于2G # 也可以使用context manager my_zip = zipfile.ZipFile('files.zip', 'w') # 如果文件大于2G zipfile.ZipFile('files.zip', 'w', zipfile.ZIP_DEFLATED, True)
python统计apache、nginx访问日志IP访问次数
前言:python统计apache、nginx访问日志IP访问次数并且排序(显示前20条)。其实用awk+sort等命令可以实现,用awk数组也可以实现,这里只是用python尝试下。
apache脚本:
nginx脚本:
ips = {}
with open("/root/access.log-20180629") as fh:
for line in fh:
ip = line.split(" ")[0]
if 6 < len(ip) <=15:
ips[ip] = ips.get(ip, 0) + 1
ip_num = []
for ipaddr,num in ips.items():
ip_num.append((ipaddr,num))
ip_num.sort(key=lambda x: x[1], reverse=True)
for ipaddr,num in ip_num[:20]:
print('IP地址为{}, 访问次数为{}'.format(ipaddr,num))
压缩和解压文件
# 压缩tar.gz
import os
import tarfile
tar = tarfile.open(" /root/test/test.tar.gz " ," w:gz " ) # 创建压缩包名
for path,dir ,files in os.walk(" /root/test " ): # 递归文件目录
for file in files:
fullpath = os.path.join (path,file )
tar .add(fullpath) # 创建压缩包
tar .close()
# 解压tar.gz
import tarfile
tar = tarfile.open(" /root/test/test.tar.gz " )
# tar.extract("/tmp") # 全部解压到指定路径
names = tar.getnames() # 包内文件名
for name in names:
tar.extract(name,path =" ./ " ) # 解压指定文件
tar.close()
gzip [解压缩gzip 删除原文件]
# 压缩gzip
import gzip
f_in = open(' file.log ' , ' rb ' )
f_out = gzip.open(' file.log.gz ' , ' wb ' )
f_out.writelines(f_in)
f_out.close()
f_in.close()
# 压缩gzip
File = ' xuesong_18.log '
g = gzip.GzipFile(filename="" , mode=' wb ' , compresslevel=9, fileobj=open((r' %s.gz ' %File),' wb ' ))
g.write(open(r ' %s ' %File).read())
g.close()
# 解压gzip
g = gzip.GzipFile(mode=' rb ' , fileobj=open((r' xuesong_18.log.gz ' ),' rb ' ))
open((r ' xuesong_18.log ' ),' wb ' ).write(g.read())
输入参数判断
try :
textfile1 =sys.argv[1]
textfile2 =sys.argv[2]
except Exception,e:
print " Error: " +str(e)
print " Usage: simple3.py filename1 filename2 "
sys.exit()
判断输入参数是否为2个
if len(sys.argv) > 2:
dir1 =sys.argv[1]
dir2 =sys.argv[2]
else :
print " Usage: " , sys.argv[0], " datadir backupdir "
sys.exit()
输入去掉空白
_input = input(' please input your opinion: ' )
ret = _input.strip()
Python执行linux命令并得到执行结果
subprocess
# !/usr/bin/env python
# coding: utf-8
import subprocess
def create_process(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
result = p.stdout.read()
code = p.wait()
return code, result
code,result = create_process(' ls -l test.py ' )
# print(code)
# print(result)
# 根据返回码判断是否执行成功
if code:
print (" 备份MySQL失败 " )
print (code, result)
else :
print (" 备份MySQL成功 " )
print (code, result)
利用执行命令后的返回代码判断
利用执行命令后的返回代码判断
# coding:utf-8
import os
import subprocess
cmd = ' ls -l test.txt '
retcode = subprocess.call(cmd, shell=True)
result = p.returncode
if retcode != 0:
print (" 备份MySQL失败 " )
# print(ret)
else :
print (" 备份MySQL成功 " )
commands
# !/usr/bin/env python
# coding: utf-8
import sys
import os
import commands
status,output =commands.getstatusoutput(' ls -l test.py ' )
if (status!=0):
print " command is failed, please check "
print output
sys.exit( 1)
else :
print " command is sucess. "
os.system
ftp客户端ftplib
from ftplib import FTP
ftp = FTP('10.112.13.98') # 连接ftp地址 FTP(host,port,timeout)
ftp.login(test1,f0W1V7kw) # 使用默认anonymous登录 login(user,passwd)
ftp.cwd('debian') # 切换到目录debian
ftp.retrlines('LIST') # 打印目录列表
ftp.retrbinary('RETR README', open('README', 'wb').write) # 下载文件写到本地 ftp.storbinary("STOR "+filename, fp, bufsize) # 上传目标文件
ftp.delete('filename') # 删除ftp中文件
ftp.mkd('dirname') # 在ftp上创建目录
ftp.size('filename') # 查看文件大小
ftp.quit()
示例
# !/usr/bin/env python
# coding: utf-8
import time
import os
import ftplib
from ftplib import FTP
user =' username '
password =' password '
filename = ' u_ex ' + time.strftime(' %y%m%d ' ) + ' .log '
for root,dirs,files in os.walk(r" C:\inetpub\logs\LogFiles\W3SVC2 " ):
for file in files:
# 获取文件所属目录
# print(root)
# 获取文件路径
# print(os.path.join(root,file))
if filename in os.path.join(root,file):
print (os.path.join(root,file))
local_file = os.path.join(root,file)
# print(type(local_file))
bufsize = 1024
fp = open(local_file, ' rb ' )
ftp = FTP(' IP ' ) # 连接ftp地址 FTP(host,port,timeout)
ftp.login(user, password) # 使用默认anonymous登录 login(user,passwd)
ftp.cwd(' debian ' ) # 切换到目录debian
ftp.storbinary(" STOR " +filename, fp, bufsize) # 上传目标文件
ftp.retrlines(' LIST ' ) # 打印目录列表
ftp.quit()
fp.close()
print (' upload file success. ' )
python按行读取文件,如何去掉换行符"\n"
for line in file.readlines():
line =line.strip(' \n ' )
1、列表与字符串转换
列表转字符串:
将列表中的内容拼接成一个字符串
将列表中的值转成字符串
字符串转列表:
用eval转换
将字符串每个字符转成列表中的值
将字符串按分割成列表
2、列表与字典转换
列表转字典:
将两个列表转成字典
将嵌套列表 转为字典
字典转列表:
字典中键、值转为列表
3、字典与字符串转换
字符串转字典:
用eval转换
用json.loads 转换
字典转字符串:
用json.dumps 转换
强转换
列表转换成字典
def GenPassword_reg(length=16, chars=string.ascii_letters + string.digits):
return '' .join([choice(chars) for i in range(length)])
dic = {}
# result 是个列表
for i in result:
dic[i] = GenPassword_reg()
print (dic)
shell与python间传递变量方法
python -> shell:
1.环境变量
import os
var =123或var=’123’
os.environ[’var’] =str(var) # environ的键值必须是字符串
os.system(’echo $var’)
2.字符串连接
import os
path =’/root/a.txt’
var =[1]
var =’bash’
os.system(’echo ’ + path) # 注意echo后有空格
os.system(‘echo ‘ + str(var[0]))
os.system(’echo ’ + var + ’ /root/c.sh’) # 注意echo后和/root前有空格
3.通过管道
import os
var =’123’
os.popen(’wc -c’, ’w’).write(var)
4.通过文件
output = open(‘/tmp/mytxt’, ‘w’)
output.write(S) # 把字符串S写入文件
output.writelines(L) # 将列表L中所有的行字符串写到文件中
output.close()
5.通过重定向标准备输出
buf = open(’/root/a.txt’, ’w’)
print >> buf, ‘123\n’, ‘abc’
或
print >> open(‘/root/a.txt’, ‘w’), ‘123\n’, ‘abc’ # 写入或生成文件
print >> open(‘/root/a.txt’, ‘a’), ‘123\n’, ‘abc’ # 追加
shell -> python:
1.管道
import os
var =os.popen(’echo -n 123’).read( )
print var
2.
import commands
var =commands.getoutput(’echo abc’) # 输出结果
var=commands.getstatusoutput(’echo abc’) # 退出状态和输出结果
3.文件
input = open(‘/tmp/mytxt’, ‘r’)
S = input.read( ) # 把整个文件读到一个字符串中
S = input.readline( ) # 读下一行(越过行结束标志)
L = input.readlines( ) # 读取整个文件到一个行字符串的列表中
转自:原文链接:https://blog.csdn.net/kpshare/article/details/7523511
python用变量拼接成 shell命令执行
def create_process(cmd): p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = p.stdout.read() code = p.wait() return code, result mysql_trunc = ' /usr/local/mysql/bin/mysql -uroot -p -h localhost -N -e "select * from moffice_hnair_schedule_log_his. ' + str(final_list[-1]) + ' limit 1" '
print (mysql_trunc)
code, result = create_process(mysql_trunc)
if code != 0:
print (" 执行MySQL清除log_interface_result失败 " )
print (result)
else :
print (" 执行MySQL清除log_interface_result成功 " )
print (result)
在windows下删除文件报错
WindowsError: [Error 123] : 'C:\\Users\\chh-huang\\Desktop\test\\2019-11-23.txt'
filename = r ' C:\Users\chh-huang\Desktop\test\\ ' + name
需要在路径前加r,这样就没问题了
filename = r' C:\Users\chh-huang\Desktop\test ' + os.sep + name
等于
filename = r' C:\Users\chh-huang\Desktop\test\\ ' + name 其中,os.sep根据你所处的平台,自动采用相应的分隔符号 >>> os.sep '/'
Python删除过期文件
# -*- coding:utf8 -*-
# !/usr/bin/python
import os
import datetime
import time
import shutil
for root, dirs, files in os.walk(r' C:\Users\chh-huang\Desktop\test ' ):
for name in files:
(y1, m1, d1) = (int(x) for x in name.split(' . ' )[0].split(' - ' ))
# print(y1, m1, d1)
date1 = datetime.date(y1, m1, d1)
datenow = time.strftime(' %Y%m%d ' )
y2 = int(datenow[:4])
m2 = int(datenow[4:6])
d2 = int(datenow[6:])
date2 = datetime.date(y2, m2, d2)
# print(date1, date2) # 删除大于等于2天的文件
if (date2 - date1).days >= 2:
print ' Expired file! Deleting file... ' , name
print (' os.remove(%s) ' % name)
# filename = r'C:\Users\chh-huang\Desktop\test\\' + name
filename = r' C:\Users\chh-huang\Desktop\test ' + os.sep + name
os.remove(filename)
import os
import time
import datetime
import shutil
filepath = r" xxx "
timestamp = os.path.getctime(filepath) # 获取文件创建的时间戳
timestruct = time .localtime(timestamp) # 将时间戳转换成本地时间
file_create_time = time .strftime(' %Y-%m-%d ' , timestruct) # 本地时间转换成字符串
current_time = time .strftime(' %Y-%m-%d ' ) # 获取当前时间
# 利用datetime来计算时间差
day1 = datetime.datetime.strptime(file_create_time, ' %Y-%m-%d ' )
day2 = datetime.datetime.strptime(current_time, ' %Y-%m-%d ' )
delta = day2 - day1
print(delta.days) # 获取当前时间与文档创建时间之差
if delta.days>3 : # 删除创建日期大于3天的文件
shutil.rmtree(filepath)
参考
(3条消息)python 根据时间间隔删除文件夹内修改时间比较早的文件_weixin_38383877的博客-CSDN博客 https://blog.csdn.net/weixin_38383877/article/details/82897359
Python实现删除文件夹内规定时间内的文件 - 爱我所艾 - 博客园 https://www.cnblogs.com/wangchy0927/archive/2013/08/30/3291304.html
(3条消息)python 删除指定时间间隔之前的文件_百恼专栏-CSDN博客 https://blog.csdn.net/zsy19881226/article/details/72638036
Python实现删除文件夹内规定时间内的文件 - 爱我所艾 - 博客园 https://www.cnblogs.com/wangchy0927/archive/2013/08/30/3291304.html
使用Python批量删除文件前缀的操作方法-百度经验 https://jingyan.baidu.com/article/1974b289eaab0eb4b0f7746a.html
清理以开头过期日志文件
#!/usr/bin/env python
# coding: utf -8
import os
import shutil
import datetime
import time
src_dir = r' C:\Windows\System32\LogFiles\Sum '
dst_dir = r' G:\log-svcbak '
for file in os.listdir(src_dir):
# 找出以svc开头的log文件
if file [:3 ] == ' Svc ' and file [-3 :] == ' log ' :
# 排除Svctmp.log或Svc.log文件
if file == ' Svctmp.log ' or file == ' Svc.log ' :
continue
# print( ' %s was been moved ' % file )
src_file = src_dir + ' \\ ' + file
# os.remove(src_file)
# print(src_file)
timestamp = os.path.getmtime(src_file)
timestruct = time .localtime(timestamp)
file_create_time = time .strftime(' %Y-%m-%d ' , timestruct)
current_time = time .strftime(' %Y-%m-%d ' )
day1 = datetime.datetime.strptime(file_create_time, ' %Y-%m-%d ' )
day2 = datetime.datetime.strptime(current_time, ' %Y-%m-%d ' )
delta = day2 - day1
# print(file_create_time, current_time, day1, day2, delta.days, src_file)
# 删除大于21天的文件
if delta.days > 21 :
print(src_file)
# os.remove(src_file)
# shutil.rmtree(filepath)
第二个版本
#!/usr/bin/env python
# coding: utf -8
import os
import shutil
import datetime
import time
src_dir = r' C:\Windows\System32\LogFiles\Sum '
dst_dir = r' G:\log-svcbak '
for file in os.listdir(src_dir):
# 找出以svc开头的log文件
if file [:3 ] == ' Svc ' and file [-3 :] == ' log ' :
# 排除Svctmp.log或Svc.log文件
if file == ' Svctmp.log ' or file == ' Svc.log ' :
continue
# print( ' %s was been moved ' % file )
src_file = src_dir + ' \\ ' + file
filedate = os.path.getmtime(src_file)
time1 = datetime.datetime.fromtimestamp(filedate).strftime(' %Y-%m-%d ' )
date1 = time .time ()
num1 = (date1 - filedate) / 60 / 60 / 24
if num1 >= 21 :
print(src_file)
函数执行的结果赋值给某个变量(使用return)
files = [' log_interface_view_20194 ' , ' log_interface_view_201905 ' ,' log_interface_view_201915 ' ]
def sort_strings(files):
sort_dict = {}
for file in files:
sort_num = int(file[19:])
sort_dict[file] = sort_num
sorted_dict = sorted(sort_dict.items(), key=lambda x:x[1])
print ([key[0] for key in sorted_dict])[0]
# 那如果我让把函数执行的结果赋值给某个变量
var = sort_strings(files)
print (var)
执行结果,变量没有赋值成功
[root@hch test 16:03:24]# python dbsort.py
log_interface_view_20194
None
这是就需要使用return了
def sort_strings(files):
sort_dict = {}
for file in files:
sort_num = int(file[19:])
sort_dict[file] = sort_num
sorted_dict = sorted(sort_dict.items(), key=lambda x:x[1])
# print([key[0] for key in sorted_dict])[0]
return ([key[0] for key in sorted_dict])[0]
var = sort_strings(files)
print (var)
执行
[root@hch test 16:05:56]# python dbsort.py
log_interface_view_20194
将列表split分成 prefix, suffix
_input = [' ip_10.2.223.5 ' , ' ip_10.2.220.3 ' , ' port_22 ' , ' port_8001 ' , ' port_80 ' , ' ip_172.16.8.35 ' ]
''' 如果ip开头:不变
port开头:把下划线去掉
列表推导式:第二种形态去完成 '''
答案:
for item in _input:
if item.startswith(' ip ' ):
_list.append(item)
elif item.startswith(' port ' ):
prefix, suffix = item.split(' _ ' )
item = prefix + suffix
_list.append(item)
print (_list)
列表推导式
[item.replace( ' _ ' ,'' ) if item.startswith(' port ' ) else item for item in _input ]
pymysql使用方法
# !/usr/bin/env python
# coding:utf-8
import pymysql
# 连接数据库
db = pymysql.connect(host = ' localhost ' , user = ' root ' , password = ' password ' , db = ' demo1 ' , port = 3306)
print (db)
cur = db.cursor()
sql = " select * from login where id = {} " .format(1)
print (sql)
try :
# 查询
cur.execute(sql)
# 查询所有数据
results = cur.fetchall()
# 获取单条数据
# results = cur.fetchone()
for i in results :
id = i[0]
username = i[1]
passwrod = i[2]
print (' id:{},username:{},password:{} ' .format(id, username, passwrod))
# 添加数据
sql_insert = " insert into login(username,password) values ({},{}) " .format(' "xiaowamg33" ' ,666)
print (' sql_insrt:{} ' .format(sql_insert))
id = cur.execute(sql_insert)
print (' id:{} ' .format(id))
db.commit()
# 修改
sql_update = ' update login set username = {} where id = {} ' .format(' "dagege" ' ,1)
id_update = cur.execute(sql_update)
print (' 修改的行数:{} ' .format(id_update))
db.commit()
# 删除
sql_delete = ' delete from login where id={} ' .format(2)
id_dedete = cur.execute(sql_delete)
print (' 删除的行数:{} ' .format(id_dedete))
db.commit()
except Exception as e:
print (e)
db.rollback()
finally :
db.close()
转自
pymysql的使用简单使用方法 - a599174211的博客 https://blog.csdn.net/a599174211/article/details/82702219
参考
关于安装pymysql - sike8 - 博客园 https://www.cnblogs.com/sike8/p/11059878.html
数据库查询并写入文件
# !/usr/bin/env python
# coding: utf-8
# 执行sql并获取内容
import string
import random
import pymysql
import logging
host = " 10.x.x.x "
user = ' root '
passwd = " xxx "
db_name = " 数据库名 "
charset = " utf8mb4 "
ret = []
if __name__ == ' __main__ ' :
db = pymysql.connect(
host =host, # 主机名
user=user, # 用户名
passwd=passwd, # 密码
db=db_name,
charset =charset) # 数据库名称
cur = db.cursor()
try :
cur.execute( " select * from USER_INFORMATION_20170418 limit 1 " )
# 也可以使用fetchmany获取指定数量的行数
# print(conn.fetchmany(10))
if cur.rowcount == 0:
print (" No result found " )
else :
f = open(' ret.log ' , ' w ' )
for row in cur.fetchall():
print (row)
f.write( ' %s %s %s %s\n ' % (str(row[0]), str(row[1]), str(row[2]), str(row[3])))
f.close()
except Exception as e:
logging.exception(e)
db.rollback()
finally :
cur.close()
db.close()
如果单独查询
# !/usr/bin/env python
# coding: utf-8
# 执行sql并获取内容
import string
import random
import pymysql
import logging
host = " 数据库IP "
user = ' root '
passwd = " 密码 "
# db_name = "数据库名称"
charset = " utf8mb4 "
ret = []
if __name__ == ' __main__ ' :
db = pymysql.connect(
host =host, # 主机名
user=user, # 用户名
passwd=passwd, # 密码
# db=db_name,
charset=charset) # 数据库名称
cur = db.cursor()
try :
cur.execute( " select * from 数据库名称.temp11111; " )
# 也可以使用fetchmany获取指定数量的行数
# print(conn.fetchmany(10))
if cur.rowcount == 0:
print (" No result found " )
else :
for row in cur.fetchall():
print (row)
except Exception as e:
logging.exception(e)
db.rollback()
finally :
cur.close()
db.close()
Python脚本:自动备份并压缩文件,同时删除过期文件
参考链接如下
(1条消息)python在windows中备份文件并压缩的解决办法 - qq_23664975的博客 - CSDN博客 https://blog.csdn.net/qq_23664975/article/details/77718843
(1条消息)(windows)python脚本:自动备份并压缩文件,同时删除过期文件 - 点点滴滴 - CSDN博客 https://blog.csdn.net/cogent2001/article/details/1953031
(1条消息)python压缩日志并删除原文件 - 独孤仁的专栏 - CSDN博客 https://blog.csdn.net/kong2030/article/details/84993544
文件的读、写
# !/usr/bin/env python
# coding:utf-8
# 读取文件
f = open(r' 2 ' , ' r ' )
content = f.readlines()
f.close()
print (content)
# 写入文件多行数据
with open(' /etc/yum.repos.d/redhat7_nanshu.repo ' , ' w ' ) as f: # 采用with语法结构可以让程序自动关闭打开的文件,不用自己处理
f.writelines([' [base]\n ' , ' name=CentOS-7 - Base\n ' ])
# 读取打印多行文件
with open(' text.txt ' ) as rows:
for row in rows:
print (row, end='' )
文件的写入多行
lines = [" 这是第一行内容\n " , " 这是第二行内容\n " , " 这是第三行内容\n " ]
file.writelines(lines)
文件的移动和复制
# !/usr/bin/env python
# coding:utf-8
src_dir = ' /etc/yum.repos.d '
dst_dir = ' /tmp/yum-bak '
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
if os.path.exists(dst_dir):
for root,dirs,files in os.walk(src_dir):
for file in files:
# 获取文件所属目录
print (root)
# 获取文件路径
print (os.path.join(root,file))
# 文件完整路径
src_file = os.path.join(root, file)
# 复制文件
shutil.copy(src_file, dst_dir)
# 移动文件
shutil.move(src_file, dst_dir)
# 移动文件并重命名
dst_file = os.path.join(dst_dir, file + time.strftime(' %Y-%m-%d %H:%M:%S ' ))
shutil.move(src_file, dst_file)
code,result = create_process(" cat /etc/redhat-release|awk '{print $1}' " )
判断文件是否存在
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
if os.path.exists(dst_dir):
判断文件是否存在,如果存在输出路径
# !/usr/bin/env python
# coding: utf-8
import os
with open( ' lost_file.txt ' ) as rows:
for row in rows:
row = row.strip(' \n ' )
dirname = row[:2]
filename = " /data/dfd/source/ " + dirname + " / " + row
if not os.path.exists(filename):
print (f" {row} is not exist. " )
else :
print (f" {row} is exist. " )
print (filename)
print (" \n " )
端口telnet
#!/usr/bin/env python
#coding:utf-8 import telnetlib
# 判断端口是否通
def port_scan(ip):
try:
telnetlib.Telnet(host=ip, port=80, timeout=2)
print(ip, 80, 'is avaliable')
except Exception, e:
print(ip, 80, 'is not avaliable')
raise e
exit()
else:
pass
port_scan('www.baidu.com')
多线程
# coding:utf8
import time
from concurrent.futures import ThreadPoolExecutor
TEST_COUNT = 100
THREADS_SUM = 50
thread_pool = ThreadPoolExecutor(THREADS_SUM)
def test():
time.sleep( 1)
print (' run test ' )
def runfunc(count):
for i in range(count):
thread_pool.submit(testi)
runfunc(TEST_COUNT)
windows输出中文乱码
def paint():
# os.system('cls')
print (" ================================================= " )
print (" | The Points Menu: | " )
print (" ================================================= " )
print (" | 1. gateway上传变更包 | " ).decode(" utf-8 " ).encode(" gbk " ) print (" | 2. shopping上传变更包 | " ).decode(" utf-8 " ).encode(" gbk " )
print (" | 0. 退出 | " ).decode(" utf-8 " ).encode(" gbk " )
print (" ================================================ " )
用.decode("utf-8").encode("gbk") 来解决。
Python启动windows服务或程序
启动服务
# !/usr/bin/python
# -*- coding: utf-8 -*-
import os
try :
os.system( ' NET STOP MSSQLSERVER ' )
except OSError:
print " system error "
os.system( ' NET START MSSQLSERVER ' )
启动程序
import os
def open_app(app_dir):
os.startfile(app_dir) # os.startfile()打开外部应该程序,与windows双击相同
if __name__ == " __main__ " :
app_dir = r' G:\yeshen\Nox\bin\Nox.exe ' # 指定应用程序目录
open_app(app_dir)
用enumerate对现有列表每项进行修改
对现有列表img_list,每项中添加新的字符串,不用新建列表,用enumerate就可以做到
for i,v in enumerate(img_list):
img_list[i] = " http://www.xueshengmai.com " + v
print(img_list)
Python爬虫
实例1:爬取豆瓣图书名
# coding: utf-8
import requests
import re
# 制作headers
head = {' User-Agent ' :' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 ' }
page_obj = requests.get(" https://book.douban.com/tag/%E5%B0%8F%E8%AF%B4 " , headers = head)
page = page_obj.text
# print(page)
regex = ' <a href="https://book.douban.com/subject/\d+/" title="(.+?)" '
data = re.findall(regex, page)
for i in data:
print (i.encode(' gbk ' ))
print (data)
实例2:爬取校花网站图片
# !/usr/bin/env python
# coding: utf-8
import requests
import re
from urllib import request
page = requests.get(" http://www.xueshengmai.com/hua/ " )
content = page.text
# print(content)
# 正则表达式
regex = ' <img width="210" alt=".+?" src="(.+?)" /> '
img_list = re.findall(regex, content)
print (img_list)
# 拼接图片地址
for i,v in enumerate(img_list):
img_list[i] = " http://www.xueshengmai.com " + v
print (img_list)
# 下载图片
count = 0
for i in img_list:
# i为下载地址 后面为保存的名字
request.urlretrieve(i, " 美女%s.jpg " % count)
print (" 第%s图片下载完成 " % (count + 1))
count += 1
with open写入Excel CSV文件
将数据写入guazi.csv文件
with open(r' guazi.csv ' , ' w ' ) as f:
for li in title_url:
name = li.xpath(' .//h2/text() ' ).extract()
year = li.xpath(' .//div/text() ' ).extract()
price = li.xpath(' .//div/p/text() ' ).extract()
price_real = li.xpath(' .//em/text() ' ).extract()
img = li.xpath(' .//img/@src ' ).extract()
print (' 车型:{}, 车况:{},{}, 价格:{}, 原价:{}, 图片:{} ' .format(name[0], year[0], year[1], price[0], price_real[0], img[0]))
f.write( ' {},{},{},{},{}\n ' .format(name[0], year[0], year[1], price[0], price_real[0]))
保存为中文csv文件
# 保存到CSV文件中, 保存为中文编码选择gb1803
with open(name+' .csv ' , ' w ' , encoding=' gb18030 ' ) as f:
f.write( ' 职位名,公司名,薪资,工作地点,职位连接,公司链接,发布日期\n ' )
Tomcat配置文件注释
# !/usr/bin/env python
# coding: utf-8
# Tomcat配置文件注释
import os
import re
with open(r ' server.xml.20200226 ' , ' r ' ) as fr, open(" server.xml.20200226.bak " , ' w ' ) as fw:
for line in fr:
regx = re.compile(r' \<Connector.*?protocol="AJP/1.3" redirectPort="8443" \/\> ' ) # 匹配替换的内容
ret = '' .join(re.findall(regx, line)) # 列表转成成字符串
if ret:
print (' 匹配成功,{} ' .format(ret))
ret1 = '' .join(ret.replace(' <Connector ' , ' <-- <Connector ' ))
line = '' .join(ret1.replace(' > ' , ' > --> ' ))
print (' 替换后,{} ' .format(line))
fw.write(line)
执行效果
Python对文件修改,替换,删除
一、Python使用replace修改原文件内容方式:
#!/usr/bin/env python
# -*- coding:utf8 -*-
old_str = "aaa" #老文件内容字段
new_str = "bbb" #要改成字段
file_data = ''
with open('/opt/1.txt', 'r', encoding='utf-8') as f:
for line in f:
if old_str in line:
line = line.replace(old_str, new_str)
file_data += line
with open('/opt/1.txt', 'w',encoding='utf-8') as f:
f.write(file_data)
二、python 使用正则表达式 替换文件内容 re.sub 方法替换
import re,os
def alter(file,old_str,new_str):
with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
for line in f1:
f2.write(re.sub(old_str,new_str,line))
os.remove(file)
os.rename("%s.bak" % file, file)
alter("file1", "admin", "password"
三、python 匹配IP及端口
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-08-30 20:38:23
# @Author : EnderZhou (zptxwd@gmail.com)
# @Link : http://www.cnblogs.com/enderzhou/
# @Version : $Id$
import requests
import re
def open_url(url):
req = requests.get(url=url,headers=headers)
html = req.content
return html
def get_ipandport(html):
# 默认网页内的IP地址位于端口号之前,并且中间至少隔了一个非数字的字符串
# (?:((?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])) 用于匹配IP地址
# (6[0-5]{2}[0-3][0-5]|[1-5]\d{4}|[1-9]\d{1,3}|[0-9]) 用于匹配端口号 注意端口号匹配规则应从大到校排序
# 使用 ([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-5]{2}[0-3][0-5]) 替换即可观察到原因。
# 使用\D+?匹配IP地址与端口号中间至少隔了一个非数字的字符串
p = r' (?:((?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5])\.(?:\d|[1-9]\d|1\d{2}|2[0-5][0-5]))\D+?(6[0-5]{2}[0-3][0-5]|[1-5]\d{4}|[1-9]\d{1,3}|[0-9])) '
iplist = re.findall(p,html)
for each in iplist:
print (each)
if __name__ == ' __main__ ' :
url = ' http://www.66ip.cn/ '
headers = {' user-agent ' :' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 ' }
get_ipandport(open_url(url))
对文件所有行前添加‘#’注释
# !/usr/bin/env python
# coding: utf-8
import os
import re
with open(r ' server.xml.20200226 ' , ' r ' ) as fr, open(" server.xml.20200226.bak " , ' w ' ) as fw:
for line in fr:
after = ' # ' + line
fw.write(after)
对文件进行注释#,空行不注释
# !/usr/bin/env python
# coding: utf-8
import os
import re
with open(r ' server.xml.20200226 ' , ' r ' ) as fr, open(" server.xml.20200226.bak " , ' w ' ) as fw:
for line in fr:
if not line.strip() or line.startswith(' # ' ): # 判断是否是空行或注释行
fw.write(line)
else :
after = ' # ' + line
print (after)
fw.write(after)
Python发送邮件
纯文本邮件
import smtplib
from email.mime.text import MIMEText
def send_text_only():
# 1.发件人、授权码,收件人信息
from_addr = " 9071xxxxx@qq.com "
pwd = " aqinixxxxxxxxxxx "
to_addr = " sunxxxxx@efee.com.cn " # 发送给多个,直接["邮箱1","邮箱2","邮箱3"]
# 2.发送内容
msg = MIMEText(" hello, I am suner!! " )
msg[ " Subject " ] = " 测试报告 "
msg[ " From " ] = from_addr
msg[ " To " ] = to_addr
# 3.连接smtp服务器,登录服务器并发送文本
smtp_server = " smtp.qq.com "
server = smtplib.SMTP(smtp_server, 25)
server.login(from_addr, pwd)
server.sendmail(from_addr, to_addr, msg.as_string()) # as_string()把MIMEText变成一个str
server.close()
if __name__ == ' __main__ ' :
send_text_only()
多个附件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_enclosure():
# 1.发件人、授权码,收件人信息
from_addr = " 907xxxxx@qq.com "
pwd = " aqinirkcqyxubdbf "
to_addr = " sunyingying@efee.com.cn "
# 2.创建实例对象,设置主题等信息
msg = MIMEMultipart()
msg[ " Subject " ] = " 收到一个文档文件和图片文件 "
msg[ " From " ] = from_addr
msg[ " To " ] = to_addr
# 邮件内容(按每个部分)
part1 = MIMEText(" 这是一个word文件和图片 " )
msg.attach(part1)
# 添加图片附件
part2 = MIMEApplication(open(" meimei.jpg " , " rb " ).read())
part2.add_header( " Content-Disposition " , " attachment " , filename=" meimei.jpg " )
msg.attach(part2)
# 添加word附件
part3 = MIMEApplication(open(" 1.docx " ," rb " ).read())
part3.add_header( " Content-Disposition " , " attachment " , filename=" 1.docx " )
msg.attach(part3)
# 3.连接smtp服务器,登录服务器并发送文本
smtp_server = " smtp.qq.com "
server = smtplib.SMTP(smtp_server,25)
server.login(from_addr,pwd)
server.sendmail(from_addr,to_addr,msg.as_string()) # as_string()把MIMEText变成一个str
server.close()
if __name__ == ' __main__ ' :
send_enclosure()
参考
(3条消息)Python基础-46-邮件发送(带附件)_Python_karry_孙二的博客-CSDN博客 https://blog.csdn.net/qq_39620483/article/details/90903782
Python输入参数执行脚本
l = len(sys.argv)
if l == 1:
s = """
请输入参数
参数说明:
up_his 更新历史记录表
up_hot 更新实时热搜
up_det 更新详细表
"""
print (s)
else :
order = sys.argv[1]
if order == " up_his " :
update_history()
elif order == " up_det " :
update_details()
elif order == " up_hot " :
get_baidu_hot()
update_hot_search() # 执行 python test.py up_his
使用python读取文件数据并转化为列表
# 首先找到需要读取的文件目录,绝对路径或者相对路径均可
filename = r" processed.cleveland.data "
# 先声明一下a
a=[]
try :
# 打开文件
fp=open(filename," r " )
print (' %s 文件打开成功 ' % filename)
for line in fp.readlines():
'''
当你读取文件数据时会经常遇见一种问题,
那就是每行数据末尾都会多个换行符‘\n’,
所以我们需要先把它们去掉
'''
line =line.replace(' \n ' ,'' )
# 或者line=line.strip('\n')
# 但是这种只能去掉两头的,可以根据情况选择使用哪一种
line =line.split(' , ' )
# 以逗号为分隔符把数据转化为列表
a.append(line)
fp.close()
print (" 文件内容为: " )
print (a)
except IOError:
print (" 文件打开失败,%s文件不存在 " % filename)
正则的使用和举例
import re
str = ' adbdaf124124d '
pattern = re.compile(r' \d ' )
print (re.findall(pattern, str))
[ ' 1 ' , ' 2 ' , ' 4 ' , ' 1 ' , ' 2 ' , ' 4 ' ]
r ' \d’中的r表示字符串中的转义
举例 s1 = r'\n'
print(s1)
输出\n
pattern1 = re.compile(r' \d+ ' )
print (re.findall(pattern1, str))
[ ' 124124 ' ]
# 匹配手机号
str = ' 13522600131\n0898-56156414 '
pattern = re.compile(r' \d{11} ' ) # 手机号是11位
print (re.findall(pattern, str))
[ ' 13522600131 ' ]
# 单词结界\b
str = ' verb people '
pattern = re.compile(r' le\b ' )
print (re.findall(pattern, str))
[ ' le ' ]
str = ' verb peoper '
pattern = re.compile(r' \w+er\b ' )
print (re.findall(pattern, str))
[ ' peoper ' ]
str = ' 13522600131,0898-56156414,17877777777,18888888888 '
pattern = re.compile(r' (1(35|88)\d{8}) ' )
print (re.findall(pattern, str))
[( ' 13522600131 ' , ' 35 ' ), (' 18888888888 ' , ' 88 ' )]
# 分组()
pattern = re.compile(r' (1(35|88)(\d+)) ' )
print (re.findall(pattern, str))
[( ' 13522600131 ' , ' 35 ' , ' 22600131 ' ), (' 18888888888 ' , ' 88 ' , ' 88888888 ' )]
解释
( 1(35|88)(\d+))这个分组匹配到,13522600131、18888888888
( 35|88) 这个分组匹配到,35、88
(\d +)这个分组匹配到,22600131、88888888
# re.I忽略大小写
str = ' abc,ABC '
pattern = re.compile(r' abc ' , re.I)
print (re.findall(pattern, str))
[ ' abc ' , ' ABC ' ]
# re.M多行匹配
pattern = re.compile(r' ab ' , re.I|re.M)
print (re.findall(pattern, str))
[ ' ab ' , ' AB ' ]
re.A、re.U
str = ' abc,我是中国人 '
# 匹配ASCII码
pattern = re.compile(r' \w ' , re.A)
print (re.findall(pattern, str))
[ ' a ' , ' b ' , ' c ' ]
# 匹配Unicode码
pattern = re.compile(r' \w ' , re.U)
print (re.findall(pattern, str))
[ ' a ' , ' b ' , ' c ' , ' 我 ' , ' 是 ' , ' 中 ' , ' 国 ' , ' 人 ' ]
# 贪婪与懒惰
str = ' abdcdefg '
pattern = re.compile(r' \w+ ' )
print (re.findall(pattern, str))
[ ' abdcdefg ' ]
# 懒惰模式
pattern = re.compile(r' \w+? ' )
print (re.findall(pattern, str))
[ ' a ' , ' b ' , ' d ' , ' c ' , ' d ' , ' e ' , ' f ' , ' g ' ]
python 判断文件夹内文件数量以及文件夹数量
path = ' C:/Users/Dell/Desktop/test '
print (' dirnum: ' ,len([lists for lists in os.listdir(path) if os.path.isdir(os.path.join(path, lists))]))
print (' filenum: ' ,len([lists for lists in os.listdir(path) if os.path.isfile(os.path.join(path, lists))]))
python 异常处理
自己常用的方法
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import paramiko
import time
hostname =' 10.x.x.122 '
username =' username '
password =' password '
port = 22
try:
t = paramiko.Transport((hostname , port))
t.connect(username =username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
dest_filename = " kams.war " + " . " + time .strftime(' %Y-%m-%d ' )
src = ' /update/kams.war '
dest = ' /kams/war120/ ' + dest_filename
sftp.put(src, dest) # 上传文件
print( ' kams.war is transfered to 122 successfully. ' )
t.close();
except Exception, e:
print str(e)
其他方法
假如我们知道异常可能是NameError或者TypeError,可以通过下面两种方法来保证能够捕获:
----方法一---
# /usr/bin/env python
try:
a *2
except NameError as err:
print(err)
except TypeError as err:
print(err)
print( ' hello,world ' )
----方法二----
# /usr/bin/env python
try:
a *2
except (NameError, TypeError) as err:
print(err)
print( ' hello,world ' )
如果我们连具体的异常类型都不知道呢?这个时候,就可以使用Python中的异常基类Exception来进行捕获:
----方法三---
# /usr/bin/env python
try:
a *2
except Exception as err:
print(err)
----方法四----(缺省表示Exception)
# /usr/bin/env python
try:
a *2
except:
print( ' err ' )
需要注意:当一个程序中try后面跟有多个exception的时候,会匹配第一个可以匹配的异常类型
04 容易忽略的finally
import sys
try :
f = open ( 'aaa.txt' , 'r' )
# some data processing
except OSError as err:
print ( 'OS error: {}' . format ( err) )
except:
print ( 'Unexpected error:' , sys. exc_info ( ) [ 0 ] )
finally :
f. close ( )
异常处理的使用需要根据场景来确定,不能不用,不用的话代码稳定性不高;也不能滥用,滥用的话,会显得代码很冗余。
例如下面的2个小场景:
-- -- 场景一-- --
try :
data = json. loads ( raw_data)
except JSONDecodeError as err:
print ( 'JSONDecodeError: {}' . format ( err) )
解析json前,对json进行合法性判断是有必要的,
否则json的解析就会报错。
-- - 场景二-- --
d = { 'name' : 'jason' , 'age' : 20 }
try :
value = d[ 'dob' ]
except KeyError as err:
print ( 'KeyError: {}' . format ( err) )
字典的key值解析这种写法也可以,但是显得有点繁琐
最好的写法是:
if 'dob' in d:
xxxxx
print输出结果的三种方法
格式化字符串:
print(“hello %s”%name)
保留2位小数点:
calc = 100 / 88
print( ' 计算结果:%.2f ' %calc)
1 )数据库使用
print( " 姓名:%s, 年龄:%d, 身高: %.2f " % (name, age, height))
2 )SQL使用不安全
print( " 姓名:{}, 年龄:{}, 身高: {} " .format(name, age, height))
3 )下面这俩种是相同的,形式不同
print(f " 姓名:{name}, 年龄:{age}, 身高: {height} " )
字符串格式化输出
name = “aliang”
age = 30
# 方法1
print( " 我的名字是%s,今年%s岁了。 " % (name, age))
# 方法2
print(f " 我的名字是{name},今年{age}岁了。 " )
字符串拼接
使用“ +”可以对多个字符串进行拼接。
示例:
str1 = " hello "
str2 = “world"
print(str1 + str2)
其他方式:
•格式化输出
•逗号
• join () 字符串的内置函数,将序列中的元素拼接成一个字符串。后面讲到
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步