os
[TOC]
Python os模块
获取当前路径
os.getcwd()
path
当前文件路径
# __file__是当前执行的文件
# 获取当前文件__file__的路径
os.path.realpath(__file__)
os.path.abspath(__file__)
# 获取当前文件__file__的所在目录
os.path.dirname(os.path.realpath(__file__))realpath 和 abspath
如果是链接文件, 那么realpath显示的是所链接文件的路径.
rename
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
重命名文件/文件夹
src为dst如果
dst是一个存在的文件夹, 会抛出OSError异常如果
dst是一个存在的文件, 默认会被src覆盖注意:在
windows系统中, 如果dst存在, 无论是文件还是文件夹都会抛出OSError异常
makedirs
递归建立文件夹
如果文件夹已经存在, 会抛出
FileExistsError错误
最后更新于
这有帮助吗?