13rd step for python:recursion && breadth traversal && ergodic traversal && time module

本文介绍了Python中三种不同的文件遍历方法:递归遍历、栈的深度遍历和队列的广度遍历,并展示了如何使用time模块进行时间操作,包括获取当前时间、转换时间格式和计算时间差等。
摘要由CSDN通过智能技术生成
#递归遍历
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值