一、os.path.dirname(os.path.abspath(__file__))和os.path.dirname(__file__)的区别与联系:
-
os.path.dirname(file)返回的是.py文件的目录
-
os.path.abspath(file)返回的是.py文件的绝对路径(完整路径,精确到py文件)
- 在命令行运行时,如果输入完整的执行的路径,则返回.py文件所在的目录,否则返回空目录。
-
os.path.dirname(os.path.abspath(file))组合使用,如果大家看过一些python架构的代码的话,会发现经常有这样的组合
-
os.path.join()进行路径拼接
例子:
#-*- coding:utf-8 -*-
import os
#os.path.dirname(__file__)返回的是.py文件的目录
path1=os.path.dirname(__file__)
print(path1)
#os.path.abspath(__file__)返回的是.py文件的绝对路径(完整路径)
path2=os.path.abspath(__file__)
print(path2)
#组合使用
path3=os.path.dirname(os.path.abspath(__file__))
print(path3)
#os.path.join()拼接路径
path4= os.path.join(os.path.dirname(os.path.abspath(__file__)),'001.py')
print(path4)
输出结果:
runfile('C:/Users/admin/Desktop/001.py', wdir='C:/Users/admin/Desktop')
C:/Users/admin/Desktop
C:\Users\admin\Desktop\001.py
C:\Users\admin\Desktop
C:\Users\admin\Desktop\001.py
总结:
os.path.dirname(file)
返回的是.py文件的目录os.path.abspath(file)
返回的是.py文件的绝对路径(完整路径)
二、time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
- time.time(): 获取时间戳
- time.localtime():格式化时间戳为本地时间
- time.strftime():格式化时间
三、移动python 文件到另一个文件夹时,会新增一个init.py的文件
有这个init.py 文件,这个文件夹会被当成一个包,不然如果其他文件导入这个包或者这个包的文件时,可能会出错。