python 文件读写总结
这是个人在项目中抽取的代码,自己写的utils的通用模块,使用的框架是tronado,包括了文件的读写操作,api格式的统一函数,如有特别需要可以联系我或者自己扩展,刚学python不久,仅供参考,例子如下。
import time
import json
import logging
import traceback
import codecs
import os conf_log = logging def resp_dict(data='', status="success", message="successful"):
"""return a dict format response"""
return dict(status=status, message=message, data=data, timestamp=time.time()) def read_json(filename):
# Load data from file
content = []
try:
conf_log.info("Reading input from file: " + str(filename))
if os.path.exists(filename):
with open(filename, "r") as fp:
content = json.load(fp)
else:
conf_log.warning("File %s does not exist." % filename) except Exception as e:
conf_log.error(e)
ex_str = traceback.format_exc()
conf_log.error(ex_str)
return content def read_json_config(filename, def_res):
# Load data from file
content = def_res
try:
conf_log.info("Reading input from file: " + str(filename))
if os.path.exists(filename):
with open(filename, "r") as fp:
content = json.load(fp)
else:
conf_log.warning("File %s does not exist." % filename) except Exception as e:
conf_log.error(e)
ex_str = traceback.format_exc()
conf_log.error(ex_str) return content def write_json(j_dict, file_name):
# Dump data into file
try:
conf_log.info("Writing file: " + str(file_name))
with codecs.open(file_name, "w", "utf-8") as outputFile:
json.dump(j_dict, outputFile, ensure_ascii=False, encoding="utf-8")
except Exception as e:
conf_log.error(e)
ex_str = traceback.format_exc()
conf_log.error(ex_str)
with codecs.open(file_name, "w", "utf-8") as outputFile:
outputFile.write(str(j_dict))
python 文件读写总结的更多相关文章
- python文件读写及形式转化和CGI的简单应用
一丶python文件读写学习笔记 open() 将会返回一个 file 对象,基本语法格式如下: open(filename, mode) filename:包含了你要访问的文件名称的字符串值. mo ...
- python 文件读写操作(24)
以前的代码都是直接将数据输出到控制台,实际上我们也可以通过读/写文件的方式读取/输出到磁盘文件中,文件读写简称I/O操作.文件I/O操作一共分为四部分:打开(open)/读取(read)/写入(wri ...
- Python 文件读写,条件循环(三次登录锁定账号实例)
通过文件读写,条件循环相关语法,实现三次登录失败则锁定该账号的功能 需求一 """需求描述: 1.输入正确账号,密码,退出程序 2.登录失败,重新输入账号密码 3.同一账 ...
- 【学习】python文件读写,用with open as的好处,非常好【转载】
原文链接:http://www.cnblogs.com/ymjyqsx/p/6554817.html 备注:博主还有很多值得学习的笔记,遇到问题可以拜读,非常感谢博主的总结 读写文件是最常见的IO操作 ...
- python文件读写,以后就用with open语句
读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...
- Python文件读写(open(),close(),with open() as f...)
Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题 ...
- python 文件读写方式
一.普通文件读写方式 1.读取文件信息: with open('/path/to/file', 'r') as f: content = f.read() 2.写入文件中: with open('/U ...
- Python文件读写、StringIO和BytesIO
1 IO的含义 在计算机中,IO是Input/Output的简写,也就是输入和输出. 由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就 ...
- python文件读写小结
读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') r表示是文本文件,rb是二进制文件 ...
- Python文件读写及网站显示
一.关于文件读写的笔记 (一) 文件概述 文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容 文件都是按照2进制进行存储的,但在表现形式上有2种:文本文件和二进制文件. 1. 文本文件 文本 ...
随机推荐
- Codility---BinaryGap
Task description A binary gap within a positive integer N is any maximal sequence of consecutive zer ...
- arcgis api for js 4.X 出现跨域问题
arcgis api for js 4.X 出现跨域问题 XMLHttpRequest cannot load http://localhost/4.3/4.3/esri/workers/mutabl ...
- hadoop之hive&hbase互操作
大家都知道,hive的SQL操作非常方便,但是查询过程中需要启动MapReduce,无法做到实时响应. hbase是hadoop家族中的分布式数据库,与传统关系数据库不同,它底层采用列存储格式,扩展性 ...
- 基于Common.Logging + Log4Net实现的日志管理
前言 Common.Logging 是Commons-Logging(apache最早提供的日志门面接口,提供了简单的日志实现以及日志解耦功能) 项目的.net版本.其目的是为 "所有的.n ...
- 每周分享五个 PyCharm 使用技巧(五)
文章首发于 微信公众号:Python编程时光 大家好,这是本系列 PyCharm 的高效使用技巧的第五篇.按照惯例,本次还是分享 5 个. 本系列前四篇如下,若还没看的,你可以点击查阅 21. 随处折 ...
- ZooKeeper类说明
ZooKeeper 类是ZooKeeper 客户端库的主要类.要使用ZooKeeper服务,应用程序必须首先实例化ZooKeeper类的对象.所有的迭代都将通过调用ZooKeeper类的方法来完成.除 ...
- 学习体验centos7 下根目录扩容操作(步骤详细!!!)
转自 苗尼玛乔 感谢你!
- 🔨揭秘vue-sfc-cli: 组件研发利器
前言 本文将揭示vue单文件组件的工具 vue-sfc-cli 的内涵,说明它是如何在整个组件研发流程中提升效率的. 本文可以看成是
- cur.execute(sql,args)和cur.execute(sql)的区别
轉:https://blog.csdn.net/mjjyszazc/article/details/88932664 方式一: userid = “123”sql = “select id,name ...
- Leetcode 195 Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...