这是个人在项目中抽取的代码,自己写的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 文件读写总结的更多相关文章

  1. python文件读写及形式转化和CGI的简单应用

    一丶python文件读写学习笔记 open() 将会返回一个 file 对象,基本语法格式如下: open(filename, mode) filename:包含了你要访问的文件名称的字符串值. mo ...

  2. python 文件读写操作(24)

    以前的代码都是直接将数据输出到控制台,实际上我们也可以通过读/写文件的方式读取/输出到磁盘文件中,文件读写简称I/O操作.文件I/O操作一共分为四部分:打开(open)/读取(read)/写入(wri ...

  3. Python 文件读写,条件循环(三次登录锁定账号实例)

    通过文件读写,条件循环相关语法,实现三次登录失败则锁定该账号的功能 需求一 """需求描述: 1.输入正确账号,密码,退出程序 2.登录失败,重新输入账号密码 3.同一账 ...

  4. 【学习】python文件读写,用with open as的好处,非常好【转载】

    原文链接:http://www.cnblogs.com/ymjyqsx/p/6554817.html 备注:博主还有很多值得学习的笔记,遇到问题可以拜读,非常感谢博主的总结 读写文件是最常见的IO操作 ...

  5. python文件读写,以后就用with open语句

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  6. Python文件读写(open(),close(),with open() as f...)

    Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题 ...

  7. python 文件读写方式

    一.普通文件读写方式 1.读取文件信息: with open('/path/to/file', 'r') as f: content = f.read() 2.写入文件中: with open('/U ...

  8. Python文件读写、StringIO和BytesIO

    1 IO的含义 在计算机中,IO是Input/Output的简写,也就是输入和输出. 由于程序和运行时数据是在内存中驻留,由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就 ...

  9. python文件读写小结

    读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') r表示是文本文件,rb是二进制文件 ...

  10. Python文件读写及网站显示

    一.关于文件读写的笔记 (一) 文件概述 文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容 文件都是按照2进制进行存储的,但在表现形式上有2种:文本文件和二进制文件. 1. 文本文件 文本 ...

随机推荐

  1. CrossOver for Mac v18.5 中文破解版下载-可以安装Windows软件

    CrossOver for Mac v18.5 中文破解版: http://h5ip.cn/kADD Crossover Mac 破解版是Mac 和 Windows 系统之间的兼容工具.使 Mac 操 ...

  2. Error: Cannot find module '@babel/runtime/core-js/object/keys'(npm start报错)

    1.问题描述: 在npm start启动react项目的时候,会出现Cannot find module '@babel/runtime/core-js/object/keys'的报错: 打开:项目根 ...

  3. 【过时】update progress has encountered a problem解决办法

    笔者第二次整理博客,已经抛弃MyEclipse了,我将公司项目转换成了idea的目录结构后大家都改换Idea进行开发,虽然我个人比较喜欢eclipse的简洁干净,但是Idea的确有很多方便开发的新功能 ...

  4. 设计和编写一个异步通用Picker选择器,用于时间日期、城市、商品分类的选择

    目录 一.功能规划 二.最底层基础实现 (1)Picker界面和功能实现 (2)不同类型的选择器基础实现 三.数据源层 (1)时间日期 (2)多级同步分类,如:城市 (3)多级异步分类,如:城市 四. ...

  5. HDU 5117:Fluorescent(状压DP + 思维)***

    题目链接 题意 给出n个灯,m个开关,每个开关控制一些灯,如果打开这个开关,这个开关控制的灯如果本来灭的就会亮,如果本来亮的就会灭.问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的 ...

  6. find文件删除

    find /root/title/test -type f -name '*.txt' -exec rm {} \;   查找并删除test文件夹下所有txt文件 find /root/title/t ...

  7. 源代码扫描工具Fortify SCA与FindBugs的简单对比

    前段时间因为工作原因需要对java源代码进行扫描,现结合使用经验对静态代码扫描工具Fortify SCA与FindBugs进行一个简单的对比. 一.Fortify SCA Fortify SCA是由全 ...

  8. java Springboot 生成 二维码 +logo

    上码,如有问题或者优化,劳请广友下方留言 1.工具类 import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHint ...

  9. spring的jar包的下载、说明

    spring的jar包官方下载地址:完整链接:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-lo ...

  10. springcloud-eureka客户端服务注册(含demo源码)

    1. 场景描述 前几天介绍了下springcloud的Eureka注册中心(springcloud-注册中心快速构建),今天结合springboot-web介绍下eureka客户端服务注册. 2. 解 ...