一、时间模块
1.时间的显示
import time
s = '2019-7-14'
print(time.strptime(s,'%Y-%m-%d')) ##返回的时元组格式时间
s_time = '09:00:00'
print(time.strptime(s_time,'%H:%M:%S')) ##返回的是元组格式时间且为unix时间
2.时间显示的格式转化
import time
#把元组的时间转换为时间戳
tuple_time = time.localtime()
print(tuple_time)
print(time.mktime(tuple_time))
#把元组时间转换为字符串时间
print(time.strftime('%m-%d',tuple_time))
print(time.strftime('%Y-%m-%d',tuple_time))
print(time.strftime('%F',tuple_time)) ##输出的时间格式可以在linux中用 date --help 查看
print(time.strftime('%T',tuple_time))
import os
import time
#把时间戳类型转换为字符串时间
pwd_time = os.path.getctime('/etc/passwd')
print('pwd_time',pwd_time)
print(time.ctime(pwd_time))
#将时间戳转换为元组
print(time.localtime(pwd_time))
#元组转换为时间戳
tuple_time = time.localtime()
print(time.mktime(tuple_time))
3.关于时间的计算
from datetime import date
from datetime import datetime
from datetime import timedelta
print(date.today())
print(datetime.now())
#计算几天前的时间
d = date.today()
delta = timedelta(days=3)
print(d + delta)
print(d - delta)
"""
atime(access time):对文件的访问都会更改这个时间
ctime(change time):文件的内容或者属性更改的时间,
mtime(modification time):文件的内容被修改的时间。
"""
import os
from datetime import date
from datetime import datetime
from datetime import timedelta
#计算几个小时前或后的时间
now_time = datetime.now() ##当前时间
delta = timedelta(hours=2)
print(now_time + delta) ##2个小时之后的时间
print(now_time - delta) ##2个小时之前的时间
now_time = datetime.now()
print(now_time)
pwd_time = os.path.getmtime('/etc/passwd') ##/etc/passwd文件内容被修改的时间
print(pwd_time)
pwd_time_obj = datetime.fromtimestamp(pwd_time)
print(pwd_time_obj)
delta = now_time - pwd_time_obj
print(delta)
二、difflib文件比较
"""
'+' 包含在第二个系列行中,但不包含第一个
'-' 包含在第一个系列行中,但不包含第二个
' ' 两个系列行一致
'?' 存在增量差异
'^' 存在差异字符
"""
import difflib
text1 = ''' 1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
'''.splitlines(keepends=False) ##是否保留结尾的'\n'换行符,Flase表示不保留
text2 = ''' 1. Beautiful is better than ugly.
3. Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
'''.splitlines(keepends=True)
d = difflib.Differ()
print(''.join(d.compare(text1,text2))) ##原格式的不同比较难以快速查看出来
d = difflib.HtmlDiff()
htmlContent = d.make_file(text1,text2)
# print(htmlContent)
with open('diff.html','w') as f: ##以html格式可以很方便的看到不同
f.write(htmlContent)
1.linux中的文件用python中的命令方法来比较
'''
执行程序后生成文件,所以不会有别的现象发生,一般没有报错即视为程序没问题,打开文件最终确认
'''
import difflib
filename1 = '/home/kiosk/passwd'
filename2 = '/home/kiosk/passwd1'
with open(filename1) as f1,open(filename2) as f2:
content1 = f1.read().splitlines(keepends=True) ##保留换行符
content2 = f2.read().splitlines(keepends=True)
d = difflib.HtmlDiff()
htmlContent = d.make_file(content1,content2)
with open('passwdDiff.html','w') as f:
f.write(htmlContent)
三、sys模块
"""
sys模块:system,接收操作系统调用解释器传入的参数
"""
import os
import sys
print(sys.argv) ##查看程序执行文件的绝对路径位置,输出为一个列表
print(sys.argv[0])
print(sys.version) ##判断使用的python版本(包括编译器等信息)
if sys.version[0] == '2':
print('running in python2...')
elif sys.version[0] == '3':
print('running in python3')
print(sys.path) ##系统环境信息
print(sys.platform) ##系统的操作系统
if sys.platform == 'linux': ##查看系统的ip信息
os.system('ifconfig')
else:
os.system('ipconfig')
四、第三方模块
##需要自己在联网状态下打开pycharm的设置下载模块。
import qrcode
img = qrcode.make('http://www.baidu.com')
img.save('hello.png') ##生成一个图片,会跳转到上面指定的网址
import random
import time
import itchat ##与微信相关的模块
hotReload = True ##保持登陆
itchat.auto_login() ##登陆网页微信
while True:
itchat.send('hello westos',toUserName='filehelper')
itchat.send_file('/tmp/passwd',toUserName='filehelper')
time.sleep(random.randint(1,3))
friends = itchat.get_friends()
print(friends) ##对方扫描二维码登陆后,这里可以给对方的文件传输助手发送指定的内容
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female', 0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info) ##对方扫描二维码登陆后,统计对方微信中的总人数及男女比例