Python简单实现安全开关文件的两种方式,
Python简单实现安全开关文件的两种方式,
本文实例讲述了Python简单实现安全开关文件的两种方式。分享给大家供大家参考,具体如下:
以下代码经Python3.3测试。
方式1:
try: file = open('config.ini', 'w') print("It's a text file", file=file) except IOError as err: print('File error: ' + str(err)) finally: if 'file' in locals(): file.close()
方式2:
try: with open('config.txt', 'w') as file: print("It's a text file", file=file) except IOError as err: print('File error: ' + str(err))
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python URL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
相关内容
- Python 爬虫学习笔记之正则表达式,python爬虫学习笔记
- Python 爬虫学习笔记之单线程爬虫,python学习笔记
- Python 爬虫学习笔记之多线程爬虫,python学习笔记
- 浅析Python中MySQLdb的事务处理功能,pythonmysqldb
- Python基于sftp及rsa密匙实现远程拷贝文件的方法,
- python中range()与xrange()用法分析,pythonxrange
- python if not in 多条件判断代码,pythonif
- python代码 if not x: 和 if x is not None: 和 if not x is None:使用
- Python 两个列表的差集、并集和交集实现代码,python交集
- Python中将字典转换为列表的方法,python中将字典列表
评论关闭