os.path.join合并 os.path.dirname返回上一级目录 os.path.exists(path) os.stat('path/filename')获取文件/目录信...
1 import os 2 str1 = "grsdgfd" 3 str2 = "wddf" 4 str3 = "gddgs" 5 # print(str1 + str2 + str3) 6 ret = os.path.join(str1,str2,str3) 7 print(ret)
结果:grsdgfd\wddf\gddgs
os.path.dirname返回上一级目录相当于os.path.split(path)
1 import os 2 str1 = "grsdgfd" 3 str2 = "wddf" 4 str3 = "gddgs" 5 # print(str1 + str2 + str3) 6 ret = os.path.join(str1,str2,str3) 7 print(ret) 8 r = os.path.dirname(ret) 9 print(r)
结果:
1 C:\Python35\python3.exe F:/Python/json序列化/a2.py 2 grsdgfd\wddf\gddgs 3 grsdgfd\wddf 4 Process finished with exit code 0
os.path.exists(path)如果path存在,返回True
;如果path不存在,返回
False
1 r = os.path.exists("C:\Python35\DLLs") 2 print(r)
结果:True
os.stat('path/filename')获取文件/目录信息
1 r = os.stat("C:\Python35\DLLs") 2 print(r)
结果:os.stat_result(st_mode=16895, st_ino=1688849860327555, st_dev=2351922400, st_nlink=1, st_uid=0, st_gid=0,
st_size=4096, st_atime=1481069134, st_mtime=1481069134, st_ctime=1481069126)