os.path.join合并 os.path.dirname返回上一级目录 os.path.exists(path) os.stat('path/filename')获取文件/目录信息

 import os
str1 = "grsdgfd"
str2 = "wddf"
str3 = "gddgs"
# print(str1 + str2 + str3)
ret = os.path.join(str1,str2,str3)
print(ret)
结果:grsdgfd\wddf\gddgs

os.path.join合并    os.path.dirname返回上一级目录    os.path.exists(path)    os.stat('path/filename')获取文件/目录信息

os.path.dirname返回上一级目录相当于os.path.split(path)

 import os
str1 = "grsdgfd"
str2 = "wddf"
str3 = "gddgs"
# print(str1 + str2 + str3)
ret = os.path.join(str1,str2,str3)
print(ret)
r = os.path.dirname(ret)
print(r)

结果:

 C:\Python35\python3.exe F:/Python/json序列化/a2.py
grsdgfd\wddf\gddgs
grsdgfd\wddf
Process finished with exit code 0

os.path.exists(path)如果path存在,返回True;如果path不存在,返回False

 r = os.path.exists("C:\Python35\DLLs")
print(r)
结果:True

os.stat('path/filename')获取文件/目录信息

 r = os.stat("C:\Python35\DLLs")
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)
上一篇:python os操作


下一篇:python中操作csv文件