python os.path经常使用方法

测试环境: python 2.7html

使用os相关,注意引用:import ospython

使用time相关,注意引用:import datetimewindows

官网:https://docs.python.org/3/library/os.path.html测试

os.path.abspathlua

# 返回绝对路径
print(os.path.abspath('path.py'))                       # G:\code\Demo\path.py
print(os.path.abspath('../Demo\\path.py'))              # G:\code\Demo\path.py
print(os.path.abspath('G:\code\Demo\path.py'))          # G:\code\Demo\path.py

os.path.isabsspa

# 是否为绝对路径,如果True,不然False
print(os.path.isabs('path.py'))                         # False
print(os.path.isabs('G:\code\Demo\path.py'))            # True

os.path.splitcode

# 将路径分割为目录和文件名
print(os.path.split('G:\code\Demo\path.py'))             # ('G:\\code\\Demo', 'path.py')

os.path.dirnameorm

# 返回文件目录
print(os.path.dirname('G:\code\Demo\path.py'))           # G:\code\Demo

os.path.isdirhtm

# 断定是不是一个存在的目录,如果True,不然False
print(os.path.isdir('path.py'))                         # False
print(os.path.isdir('HH:\code'))                        # False
print(os.path.isdir('C:\\windows'))                     # True

os.path.basenameblog

# 返回文件名
print(os.path.basename('../Demo\\path.py'))             # path.py
print(os.path.basename('G:\code\Demo\path.py'))         # path.py

os.path.splitext

# 分离文件名和后缀
print(os.path.splitext('path.py'))                     # ('path', '.py')
print(os.path.splitext('G:\code\Demo\path.py'))        # ('G:\\code\\Demo\\path', '.py')

os.path.isfile

# 断定是不是一个存在的文件,如果True,不然False
print(os.path.isfile('Fuck.text'))                     # False
print(os.path.isfile('path.py'))                       # True
print(os.path.isfile('G:\code\Demo\path.py'))          # True

os.path.commonprefix

# 返回多个路径中,全部path共有的路径(注意:路径必定要存在,不然会返回空)
pathTab = ['G:\code\LuaProject', 'G:\code\Demo', 'G:\code\csdDemo']     
print(os.path.commonprefix(pathTab))                    # G:\code\

os.path.join

# 将目录和文件名组合在一块儿
print(os.path.join('G:\Code\Demo', 'path.py'))          # G:\Code\Demo\path.py
print(os.path.join('G:\code\pathCode','.lua'))          # G:\code\pathCode\.lua
## 在第一个绝对路径前的参数忽略掉
print(os.path.join('windos','E:\code', 'demo.lua'))     # E:\code\demo.lua

os.path.normcase

# 转换路径的大小写和斜杠
print(os.path.normcase('D:/windows\\system32'))         # d:\windows\system32

os.path.getctime

# 返回文件的建立时间(浮点型秒数)
timestamp = os.path.getctime('path.py')                 
timestruct = datetime.datetime.fromtimestamp(timestamp)
print(timestruct.strftime('%Y-%m-%d %H:%M:%S'),timestamp)     
# ('2019-01-31 15:13:34', 1548918814.2969258)

os.path.getatime

# 返回文件最近的访问时间(浮点型秒数)
timestamp = os.path.getatime('path.py')                 
timestruct = datetime.datetime.fromtimestamp(timestamp)
print(timestruct.strftime('%Y-%m-%d %H:%M:%S'),timestamp)     
# ('2019-01-31 15:19:57', 1548919197.053918)

os.path.getmtime

# 返回文件最近修改时间(浮点型秒数)
timestamp = os.path.getmtime('path.py')                 
timestruct = datetime.datetime.fromtimestamp(timestamp)
print(timestruct.strftime('%Y-%m-%d %H:%M:%S'),timestamp)     
# ('2019-01-31 16:33:43', 1548923623.2079258)

os.path.getsize

# 返回文件的大小(字节),若是文件不存在就返回错误
print(os.path.getsize('path.py'))                      # 3061
print(os.path.getsize('G:\code\Demo\path.py'))         # 3061
#print(os.path.getsize('file.lua'))                    # WindowsError: [Error 2] : 'file.lua'

 

感谢原做者的分享:

http://www.runoob.com/python/python-os-path.html

http://www.javashuo.com/article/p-xjdrjsdt-w.html