关于python:0202-Python-读写文件-openossys

open

Python open() 函数

罕用技巧:

with open(path, encoding="utf-8") as file:
    data = yaml.safe_load(file)
    return data[key]

os

Python OS 文件/目录办法

罕用技巧:

import os

# 以后文件所在门路
dir_path = os.path.dirname(os.path.abspath(__file__))

# 拼接门路
case_path = os.path.join(dir_path, "test_case")

# 返回当前工作目录
cur_path = os.getcwd()

# 返回绝对路径
os.path.abspath(path)
os.path.abspath(__file__)  # 蕴含文件名

# 以数字mode的mode创立一个名为path的文件夹.默认的 mode 是 0777 (八进制)
os.mkdir(path[, mode])

# 如果门路 path 存在,返回 True;如果门路 path 不存在,返回 False
os.path.exists(path)

# 把目录和文件名合成一个门路
os.path.join(path1[, path2[, ...]])

# 返回path的实在门路
os.path.realpath(path)
os.path.realpath(__file__)  # 蕴含文件名

# 返回文件门路
os.path.dirname(path)
# 联合 os.path.realpath(__file__); os.path.abspath(__file__) 应用
os.path.dirname(os.path.realpath(__file__))  # 不蕴含文件名
os.path.dirname(os.path.abspath(__file__))  # 不蕴含文件名

# 判断运行零碎内核,win or Linux
os.name

os.path

sys

Python中sys模块

import sys

# 获取运行版本信息,例如python的版本信息
sys.version_info

# 获取运行平台信息,win or Linux
sys.platform

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理