文件夹操作库os.path

官方文档The Python Standard library —os.path
获取当前的绝对路径:

import os
print(os.path.abspath('.'))
#常用函数的使用
#directory separator   目录分隔符      '/'
#concatenation    串联

#路径中用的都是  /

import os
print(os.path.abspath('..'))  #返回上一级绝对路径
print(os.path.exists('/users'))    #判断文件是否存在
print(os.path.isfile('/users'))    #判断是否是文件
print(os.path.isdir('/users'))    #判断是否是目录directory

#路径的拼接
print(os.path.join('/temp/a/','b/c/'))   #/temp/b/c
print(os.path.join('/temp/a','/b/c/'))   #/b/c      False
#pathlib库
#Querying path properties: 查询路径属性

from pathlib import Path
p= Path('.')
print(p.resolve())
 
q = Path('/tmp/a')   #把q变量用Path做个封装。。没说赋值
Path.mkdir(q,parents=True)   #创建一个新的目录dir

猜你喜欢

转载自blog.csdn.net/weixin_42610407/article/details/86699668
今日推荐