简明Python教程学习笔记6
10、解决问题
(1)time模块的使用
1 import time 2 3 today = time.strftime("%Y-%m-%d") 4 print "Today:{}".format(today) 5 6 now = time.strftime("%H:%M:%S") 7 print "Now:{}".format(now)
输出:
(2)os.sep的使用
1 import time 2 import os 3 4 today = time.strftime("%Y-%m-%d") 5 now = time.strftime("%H:%M:%S") 6 target = today + os.sep + now + ".zip" 7 print "Target:**{}**".format(target)
输出:
os.sep目录分隔符,会根据当前操作系统是windows还是linux,自动转为\或/
(3)os.path的使用
1 import os 2 import sys 3 4 print "__file__:{}".format(__file__) 5 print "os.path.realpath(__file__):{}".format(os.path.realpath(__file__)) 6 print "os.path.dirname(os.path.realpath(__file__)):{}".format(os.path.dirname(os.path.realpath(__file__))) 7 print "os.path.split(os.path.realpath(__file__))[0]:{}".format(os.path.split(os.path.realpath(__file__))[0]) 8 print "os.path.abspath(__file__):{}".format(os.path.abspath(__file__)) 9 print "os.getcwd():{}".format(os.getcwd()) 10 print "sys.path[0]:{}".format(sys.path[0]) 11 print "sys.argv[0]:{}".format(sys.argv[0])
输出: