import os
def traverse(root):
list_dirs = os.walk(root)
for root,dirs,files in list_dirs:
for d in dirs:
print(os.path.join(root,d))
for f in files:
print(os.path.join(root,f))
#第二种:使用os.listdir:
def traverse1(root):
for lists in os.listdir(root):
path = os.path.join(root, lists)
print(path)
if os.path.isdir(path):
traverse1(path)
#第二种:使用os.listdir格式化输出:
def traverse2(root, level=1):
if level==1: print(root)
for lists in os.listdir(root):
path = os.path.join(root, lists)
print( '│'*(level-1)+'│--'+lists)
if os.path.isdir(path):
traverse2(path, level+1)
traverse('G:\\test')
print( '=======================================')
traverse1('G:\\test')
print( '=======================================')
traverse2('G:\\test')
输出结果:
==
G:\test\a
G:\test\a\b
G:\test\a\a.txt
G:\test\a\b.txt
G:\test\a\c.txt
G:\test\a\d.txt
G:\test\a\b\a.txt
G:\test\a\b\b.txt
G:\test\a\b\c.txt
=======================================
G:\test\a
G:\test\a\a.txt
G:\test\a\b
G:\test\a\b\a.txt
G:\test\a\b\b.txt
G:\test\a\b\c.txt
G:\test\a\b.txt
G:\test\a\c.txt
G:\test\a\d.txt
=======================================
G:\test
│--a
││--a.txt
││--b
│││--a.txt
│││--b.txt
│││--c.txt
││--b.txt
││--c.txt
││--d.txt
import datetime
import pytz
tz_utc = datetime.timezone.utc #标准时区
print( 'str:',str(tz_utc)) #str: UTC
print( 'repr:',repr(tz_utc)) #repr: datetime.timezone.utc
print('-------------')
tz_china = datetime.timezone(datetime.timedelta(hours=8),'asia/Beijing') #北京(+8区)
print( 'str:',str(tz_china)) #str: asia/Beijing
print( 'repr:',repr(tz_china)) #repr: datetime.timezone(datetime.timedelta(0, 28800), 'asia/Beijing')
print('-------------')
tz_America = datetime.timezone(datetime.timedelta(hours=-8),'America/Los_angeles') #北京(+8区)
print( 'str:',str(tz_America)) #str: America/Los_angeles
print( 'repr:',repr(tz_America)) #repr: datetime.timezone(datetime.timedelta(-1, 57600), 'America/Los_angeles')
print('-------------')
cn_dt = datetime.datetime(2018,3,2,10,30,tzinfo=tz_china)
utc_dt = cn_dt.astimezone(datetime.timezone.utc)
print( 'str:',str(utc_dt)) #str: 2018-03-02 02:30:00+00:00
print( 'repr:',repr(utc_dt)) #repr: datetime.datetime(2018, 3, 2, 2, 30, tzinfo=datetime.timezone.utc)
print('-------------')
us_dt = cn_dt.astimezone(tz_America)
print( 'str:',str(us_dt)) #str: 2018-03-01 18:30:00-08:00
print( 'repr:',repr(us_dt)) #repr: datetime.datetime(2018, 3, 1, 18, 30, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600), 'America/Los_angeles'))
#第二种:库
tc = pytz.country_timezones('cn')
print( 'str:',str(tc))
print('repr:',repr(tc))
print('-------------')
tz = pytz.timezone('Asia/Shanghai')
print( 'str:',str(tz))
print('repr:',repr(tz))
print('-------------')
d = datetime.datetime(2009,2,21,23,18,5,tzinfo=tz)
print( 'str:',str(d))
print('repr:',repr(d))
print('-------------')
x = d.astimezone(pytz.utc)
print( 'str:',str(x))
print('repr:',repr(x))
print('-------------')
tt=datetime.datetime.now(tz)
print( 'str:',str(tt))
print('repr:',repr(tt))
输出结果:
str: UTC
repr: datetime.timezone.utc
-------------
str: asia/Beijing
repr: datetime.timezone(datetime.timedelta(0, 28800), 'asia/Beijing')
-------------
str: America/Los_angeles
repr: datetime.timezone(datetime.timedelta(-1, 57600), 'America/Los_angeles')
-------------
str: 2018-03-02 02:30:00+00:00
repr: datetime.datetime(2018, 3, 2, 2, 30, tzinfo=datetime.timezone.utc)
-------------
str: 2018-03-01 18:30:00-08:00
repr: datetime.datetime(2018, 3, 1, 18, 30, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600), 'America/Los_angeles'))
str: ['Asia/Shanghai', 'Asia/Urumqi']
repr: ['Asia/Shanghai', 'Asia/Urumqi']
-------------
str: Asia/Shanghai
repr: 'Asia/Shanghai' LMT+8:06:00 STD>
-------------
str: 2009-02-21 23:18:05+08:06
repr: datetime.datetime(2009, 2, 21, 23, 18, 5, tzinfo='Asia/Shanghai' LMT+8:06:00 STD>)
-------------
str: 2009-02-21 15:12:05+00:00
repr: datetime.datetime(2009, 2, 21, 15, 12, 5, tzinfo=)
-------------
str: 2018-03-02 11:28:01.274820+08:00
repr: datetime.datetime(2018, 3, 2, 11, 28, 1, 274820, tzinfo='Asia/Shanghai' CST+8:00:00 STD>)
>>>