一、time、datetime
时间戳转化为元组
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 >>> a3 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元组和格式化
>> atime.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)>>> time.strptime('2016-12-12 12:12:12',"%Y-%m-%d %H:%M:%S")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)>>> time.strftime("%Y-%m-%d %H:%M:%S",a) '2016-08-26 16:23:31'元组转化为字符串
1 >>> a= time.localtime()2 >>> a3 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 >>> a3 1472200092.04 >>> time.ctime(a)5 'Fri Aug 26 16:28:12 2016'datatime
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)二、random模块
random.randint(1,9) 1和9都在random.randrange(1,8) 8不在random.random() 0-1random.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.sepos.pathsepos.linesep>>> os.name'posix'os.system()os.environos.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') Trueos.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 shelved = shelve.open('shelve_test') #打开一个文件class Test(object):def __init__(self,n):self.n = nt = Test(123) t2 = Test(123334)name = ["alex","rain","test"]d["test"] = name #持久化列表d["t1"] = t #持久化类d["t2"] = t2d.close()