2022年 11月 7日

Python创建目录、判断路径是否为目录、打开文件夹操作

1.Python创建目录

# 导入os模块
import os 

# 判断一个目录path是否存在
os.path.exists(path) 

# 创建目录path
os.mkdir(path) 

# 多层创建目录path
os.makedirs(path) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
import os

path = 'E:/test/'

if os.path.exists(path):
    pass
else:
    os.mkdir(path)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.判断路径是否为目录

# 导入os模块
import os 

# 判断路径是否为目录
os.path.isdir(path)

# 判断路径是否为文件
os.path.isfile(path) 

# 判断路径是否为链接
os.path.islink(path) 

# 判断是否为绝对路径
os.path.isabs(path) 

# 判断路径是否为挂载点
os.path.ismount(path)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3.Python打开文件夹

import os

strDirPath = r'D:\test'
os.startfile(strDirPath)

  • 1
  • 2
  • 3
  • 4
  • 5

获取更多技术干货,请访问大土土随笔

如果本文对您有所帮助,请关注微信公众号“捷创源科技”。