os.getcwd()os.path.abspath(path)os.path.abspath('.') 相当于 os.getcwd()
返回一个 tuple(目录, 文件名),以最后一个斜杠作为分割
os.path.split(path)返回一个 tuple
os.path.splitdirve(path)os.path.splitext(path)以 path 中最后一个 “.” 分割,常用于获取文件后缀名
os.path.dirname(path)os.path.basename(path)os.path.exists(path)os.path.isfile(path)os.path.isdir(path)注意:当目录不存在时,也返回 False
os.path.isabs(path)根据系统自动补齐 “\” 或 “/”
os.path.join(path, *paths)os.path.getmtime(path)os.path.getatime(path)os.path.getctime(path)os.path.getsize(path)若是文件夹,返回结果不知道是怎么算的
os.remove(path)仅可删除单个文件,如果path 是一个文件夹,将抛出 OSError
os.rename(src, dst)os.rmdir(path)如果目录为非空,则抛出 OSError:目录不是空的
os.removedirs(path)跟 rmdir 一样,目录仅能包含文件夹,不能有任何文件,否则抛出 OSError:目录不是空的
os.mkdir(path)当目录已存在时,报 FileExistsError:当文件已存在时,无法创建该文件
os.makedirs(path, exist_ok=False)当 exists_ok=False 时,若目录已存在,报 FileExistsError:当文件已存在时,无法创建该文件,exists_ok=True 时,不会报错。
os.listdir(path)