python中模块-os模块
python中的os模块
os模块提供了多数操作系统的功能接口函数,可以使用不同的操作平台
getcwd用法:获取当中执行命令下在所在目录
import os print(os.getcwd()) #结果:D:\pycharm\untitled\demo
isfile用法:
判断当中路径下是否为文件是就返回true 不是就返回false
print(os.path.isfile(r'D:\pycharm\untitled\demo'))
isdir用法:
判断当中路径下是否为目录是就返回true 不是就返回false
print(os.path.isdir(r'D:\pycharm\untitled\demo'))
existx用法:
判断目录或者文件是否存在是就返回true 不是就返回false
print(os.path.exists(r'D:\pycharm\untitled\demo'))
listdir用法:
列出指定目录下的所有目录和文件
print(os.listdir(r'D:\pycharm\untitled\demo'))
split用法:
分割文件和目录
print(os.path.split(r'D:\pycharm\untitled\demo\demo01.py'))
join用法:
拼接目录,可以两个进行拼接也可以多个路径进行拼接
print(os.path.join(r'D:\pycharm\untitled','demo\demo01.py'))
mkdir用法:
创建一个目录
os.mkdir(r'D:\pycharm\untitled\wuhan3')
dirname用法:显示文件上级目录
显示当前文件的上级目录
os.path.dirname(__file__)

浙公网安备 33010602011771号