#递归遍历
import os
def getAllDir(path,sp=" " ):
filesList = os.listdir(path)
#print(filesList)
sp += " "
for fileName in filesList:
fileAbsPath = os.path.join(path,fileName)
if os.path.isdir(fileAbsPath):
print(sp,"└目录:%s"%fileName)
getAllDir(fileAbsPath,sp)
else:
print(sp, "| 文件%s"%fileName)
#getAllDir(r"C:\D_files")
#栈的深度遍历
def getAllDirDE(path):
stack = []
stack.append(path)
while len(stack)!=0:
dirPath = stack.pop()
fileList = os.listdir(dirPath)
for fileName in fileList:
fileAbsPath = os.path.join(dirPath,fileName)
if os.path.isdir(fileAbsPath):
print("目录:%s"% fileName)
stack.append(fileAbsPath)
else:
print("文件:%s"%fileName)
#getAllDirDE(r"C:\D_files")
#队列的广度遍历
import collections
def getAllDirWE(path):
queue = collections.deque()
queue.append(path)
while len(queue)!= 0:
dirPath = queue.popleft()
fileList = os.listdir(dirPath)
for fileName in fileList:
filrAbsPath = os.path.join(dirPath,fileName)
if os.path.isdir(filrAbsPath):
queue.append(filrAbsPath)
print("目录"+ fileName)
else:
print("文件"+ fileName)
getAllDirWE(r"C:\D_files")
time module:
import time
c = time.time()
print("c %f"%c)
p = time.ctime(c)#change to str
print(c)
t = time.gmtime(c)#change to stardard time
print(t)
b = time.localtime(c)#change to local time tuple
print(b)
s = time.asctime(b)#changge tuple to str
print(s)
m = time.mktime(b)
print(m)
q = time.strftime("%Y -%m -%d %H%M%S",b)#format tuple to a str
print(q)
w = time.strptime(q,"%Y -%m -%d %H%M%S")#callback a str to tuple
print(w)
# t1 = time.clock()
# print(t1)
# time.sleep(2)
# t2 =time.clock()
# print(t2)# in windows ,it make t1= 0,and make t2 = t2-t1
# #but in unix,make t1 the cpu time ,and t2 = t2-t1
# time.sleep(2)
# t3 =time.clock()
# print(t3)
datetime module
import datetime
"""
datetime:time and date
timedelta:time range
tzinfo:time area
time:time
date:date
"""
dl = datetime.datetime.now()#get the time now
print(dl)
d2 = datetime.datetime(1999,10,1,10,28,25,123456)
print(d2)
d3 = dl.strftime("%Y-%m-%d %X")
print(d3)
d7 = dl-d2
print(d7)
print(d7.days)#print the days range,
print(d7.seconds)#seconds except days
calendar
import calendar
print(calendar.month(2018,2))
print(calendar.calendar(2018))
print(calendar.isleap(2018))#jedge the year is the leap
print(calendar.monthrange(2018,6))#return the first weekday and the days in this month