当前位置: 首页 > 后端技术 > Python

02-02Python读写文件open-os-sys

时间:2023-03-26 17:52:00 Python

openPythonopen()函数常用技巧:withopen(path,encoding="utf-8")asfile:data=yaml.safe_load(file)returndata[key]osPythonOS文件/目录方法常用技巧:importos#current文件所在路径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__)#包含文件名#以数字方式创建名为path的文件夹。默认mode为0777(八进制)os.mkdir(path[,mode])#如果路径path存在,则返回True;如果路径path不存在,则返回Falseos.path.exists(path)#将目录和文件名合并为一个路径os.path.join(path1[,path2[,...]])#返回真实路径ofpathos.path.realpath(path)os.path.realpath(__file__)#包含文件名#返回文件路径os.path.dirname(path)#结合os.path.realpath(__file__);os.path.abspath(__file__)Useos.path.dirname(os.path.realpath(__file__))#不包含文件名os.path.dirname(os.path.abspath(__file__))#不包含filename#确定操作系统内核,win或Linuxos.nameos.pathsysPython中的sys模块importsys#获取运行版本信息,如python版本信息sys.version_info#获取运行平台信息,win或Linuxsys.platform