os模块和sys模块
- os模块负责程序与操作系统交互,提供访问操作系统的接口
- sys模块负责程序与Python解释器交互,用于操控Python运行环境
import os
import sys
os.getcwd() # 取得当前工作目录
# ------------------------------------
# output:
'E:\\project\\Jupyter\\Note'
dirname = 'E:/project/Jupyter/Note'
os.listdir(dirname) # 列出指定目录的文件
# ------------------------------------
# output:
['.ipynb_checkpoints', 'argparse模块.ipynb', 'os与sys模块.ipynb']
dirname = 'E:/project/Jupyter/Note'
os.path.basename(dirname) # 去掉目录路径,返回文件名
# ------------------------------------
# output:
'Note'
dirname ='E:/project/Jupyter/Note'
os.path.dirname(dirname) # 去掉文件名,返回目录路径
# ------------------------------------
# output:
'E:/project/Jupyter'
path1 = 'E://'
path2 = 'project/Jupyter'
path3 = 'Note'
path = os.path.join(path1,path2,path3) # 将分离的各部分组合成一个路径名
print(path)
os.listdir(path) # 列出指定目录的文件
# result = path.replace('\\', '/')
# print(result)
# ------------------------------------
# output:
'E://project/Jupyter\Note'
['.ipynb_checkpoints', 'argparse模块.ipynb', 'os与sys模块.ipynb']
filename = 'E:\\project\\Jupyter\\Note\\test.txt'
os.path.exists(filename) # 是否存在
# ------------------------------------
# output:
False
filename = 'E:\\project\\Jupyter\\Note\\test.txt'
os.path.isabs(filename) # 是否为绝对路径
# ------------------------------------
# output:
True
filename = 'E:\\project\\Jupyter\\Note\\test.txt'
os.path.isfile(filename) # 是否为文件
# ------------------------------------
# output:
False
filename = 'E:\\project\\Jupyter\\Note'
os.path.isdir(filename) # 是否为目录
# ------------------------------------
# output:
True
待完善