测试环境:Ubuntu16.04+Pycharm+Python3.5
os.path.sep()
就是分隔符.路径的分隔符
Windows 下就是 \ 这个
import os
print(os.path.abspath('csdn.py')) # /home/yu/Desktop/JinYong/csdn.py
print(os.path.isabs('csdn.py')) # False
print(os.path.isfile('csdn.py')) # True
print(os.getcwd()) # /home/yu/Desktop/JinYong
print(os.getcwd() + os.path.sep+'csdn.py') # /home/yu/Desktop/JinYong/csdn.py
print(os.path.dirname('/home/yu/Desktop/JinYong/csdn.py')) # /home/yu/Desktop/JinYong 获取绝对路径
遍历os.walk()
分别得到是当前所处位置的绝对路径,子文件夹,当前路径下的子文件
for root,dir,walk in os.walk('/home/yu/Desktop/JinYong/CSDN'):
print(root)
print(dir)
print(walk)
Tree:
├── test1.py
├── test2.py
└── Yu
├── 1.py
└── Yu1
└── 2.py
2 directories, 4 files
输出结果:
'''
/home/yu/Desktop/JinYong/CSDN
['Yu']
['test1.py', 'test2.py']
/home/yu/Desktop/JinYong/CSDN/Yu
['Yu1']
['1.py']
/home/yu/Desktop/JinYong/CSDN/Yu/Yu1
[]
['2.py']'''
os.path.exists(file/dir)
print(os.path.exists('csdn.py')) # True
print(os.path.exists('CSDN.py')) # False
print(os.path.exists('/home/yu/Desktop/JinYong')) # True
print(os.path.exists('/home/yu/Desktop/JinYong/csdn.py')) # True
后续慢慢更新