python笔记5

一、time、datetime

  1. 1 >>> time.localtime()
    2 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=21, tm_sec=38, tm_wday=4, tm_yday=239, tm_isdst=0)
    3 >>> time.gmtime()
    4 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=8, tm_min=21, tm_sec=44, tm_wday=4, tm_yday=239, tm_isdst=0)
    时间戳转换为元组
    1 >>> a =time.localtime()
    2 >>> a
    3 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=23, tm_sec=31, tm_wday=4, tm_yday=239, tm_isdst=0)
    4 >>> time.mktime(a)
    5 1472199811.0
    元组转化为时间戳
    1 >> a
    2 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=23, tm_sec=31, tm_wday=4, tm_yday=239, tm_isdst=0)
    3 >>> time.strptime('2016-12-12 12:12:12',"%Y-%m-%d %H:%M:%S")
    4 time.struct_time(tm_year=2016, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=12, tm_sec=12, tm_wday=0, tm_yday=347, tm_isdst=-1)
    5 >>> time.strftime("%Y-%m-%d %H:%M:%S",a) 
    6 '2016-08-26 16:23:31'
    元组和格式化
    1 >>> a= time.localtime()
    2 >>> a
    3 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=26, tm_hour=16, tm_min=26, tm_sec=42, tm_wday=4, tm_yday=239, tm_isdst=0)
    4 >>> time.asctime(a)
    5 'Fri Aug 26 16:26:42 2016'
    元组转化为字符串
    1 >>> a = time.mktime(time.localtime())
    2 >>> a
    3 1472200092.0
    4 >>> time.ctime(a)
    5 'Fri Aug 26 16:28:12 2016'
    时间戳转化为字符串
    1 >>> datetime.datetime.now()
    2 datetime.datetime(2016, 8, 26, 16, 29, 6, 74390)
    3 >>> datetime.datetime.now() + datetime.timedelta(-4)
    4 datetime.datetime(2016, 8, 22, 16, 29, 12, 625394)
    5 >>> datetime.datetime.now() + datetime.timedelta(hours=9)
    6 datetime.datetime(2016, 8, 27, 1, 29, 16, 201594)
    datetime

二、random模块

random.randint(1,9) 1和9都在
random.randrange(1,8) 8不在

random.random() 0-1
random.uniform(1,10)

random.randrange(1,8)

random.choice('hellp') 从中随机选一个

random.sample('hello',2)

>>> a=[1,2,3,4,5]
>>> random.shuffle(a)
>>> a
[4, 1, 5, 3, 2]


三、os模块
>>> os.getcwd()
'/root/oldboy'
>>> os.chdir('..')
>>> os.getcwd()
'/root'

>>>os.chdir(r'')

>>> os.curdir
'.'
>>> os.pardir
'..'

os.makedirs() 多级目录
os.removedirs() 删除多级目录,删除后上一级目录为空,照样删除
os.mkdir() 只能创建单级目录
os.rmdir() 只删除单级

os.listdir() 列出当前目录
os.rename(old,new)
os.stat()


os.sep
os.pathsep
os.linesep

>>> os.name
'posix'

os.system()
os.environ
os.path.abspath(path)

>>> os.path.split(r'/root/1.c')
('/root', '1.c')

>>> os.path.basename('/root/1.c')
'1.c'

>>> os.path.dirname('/root/1.c')
'/root'

>>> os.path.exists('/root')
True

os.path.isabs('/root/1.c')

os.path.isfile() 是否是文件
os.path.isdir()
os.path.join(['','']) 多个路径组合返回

os.path.getatime()
os.path.getmtime()

四、shutil模块


shutil.rmtree()
shutil.copytree('test','newtest')
shutil.copystat()
shutil.copyfile()
shutil.move()
shutil.make_archive(n)

五、shelve模块

import shelve

d = shelve.open('shelve_test') #打开一个文件

class Test(object):
def __init__(self,n):
self.n = n


t = Test(123)
t2 = Test(123334)

name = ["alex","rain","test"]
d["test"] = name #持久化列表
d["t1"] = t #持久化类
d["t2"] = t2

d.close()

原文地址:https://www.cnblogs.com/cppb/p/5809607.html