Python3:文件读写

open

f = open('filename','r') # 读模式
f = open('filename','w') # 写模式
f = open('filename','a') # 追加模式 注:rb 是以二进制读取
现在你觉得没用对吧,我也这么觉得。。。
but
在以后用到socket的时候,传输文件,读取和写入用的都是二进制形式
rb和wb可以更快速的进行文件的传输

读取内容方式

f.read() # 一次读取整个文件,文件大不适用
f.readline() # 一次只读取一行,占内存小,速度慢
f.readlines() # 一次性读取,将内容分析成一个行的列表,可以由for...in...处理

写文件方式

f.write(content) # 不会换行哦
f.writeline(content) # 下次会写在下一行

close

f.close()

示例代码一:

f = open('cdays-4-test.txt', 'r')                   #以读方式打开文件
result = list()
for line in f.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
if not len(line) or line.startswith('#'): #判断是否是空行或注释行
continue #是的话,跳过不处理
result.append(line) #保存
result.sort() #排序结果
f.close()                  #关闭文件
with open('new_file.txt','w') as fw: #with方式不需要再进行close
  fw.write('%s' % '\n'.join(result))     #保存入结果文件

示例代码二:

#txtName:文件名,包含完整路径
#dataList:数据
def saveTxt(txtName,dataList):
yyb_data = []
if len(dataList)>0:
for i in range(0,len(dataList)):
yyb_data.append(dataList[i].v1 + "," + dataList[i].v2 + "," + dataList[i].v3 + "," + dataList[i].v4 + "," + dataList[i].v5 + ","
+ dataList[i].v6 + "," + dataList[i].v7 + ","+ dataList[i].v8 + "," + dataList[i].v9 + "," + dataList[i].v10 + ","
+ dataList[i].v11 + "," + dataList[i].v12 + "," + dataList[i].v13 + "," + dataList[i].v14+"\n")
dbcfg = dbconfig()
vrg_savePath=dbcfg[4]
file = vrg_savePath+txtName
if os.path.exists(file):
os.remove(file)
#解决编码问题encoding='utf-8'
out = open(file, "a",encoding='utf-8')
for row in yyb_data:
out.writelines(row)
out.close()

Python3:文件读写的更多相关文章

  1. python3 文件读写,编码错误UnicodeDecodeError

    问题:python3 with open文件进行读写,报编码错误 /usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Version ...

  2. Python3 文件读写r,w,a

    # Author;Tsukasa ''' f = open('yesterday','w') #文件句柄...注意open分为‘r’读模式,‘w’写模式(d会先创建文件或者覆盖文件),‘a’为追加模式 ...

  3. python3 文件读写操作中的文件指针seek()使用

    python中可以使用seek()移动文件指针到指定位置,然后读/写.通常配合 r+ .w+.a+ 模式,在此三种模式下,seek指针移动只能从头开始移动,即seek(x,0) . 模式 默认 写方式 ...

  4. Python3 文件读写注意事项(指针问题)

    C:\Users\Administrator\AppData\Local\Programs\Python\Python35\python.exe E:/python/day2/op.py Someho ...

  5. python3的文件读写模式

    任何一种语言,文件的读写都是非常常见的.python的文件读写非常简单,仅仅一个函数open(file也可以,但是我不常用). 先看看官网的解释: open(file, mode='r', buffe ...

  6. python3:文件读写+with open as语句

    转载请表明出处:https://www.cnblogs.com/shapeL/p/9141238.html 前提:文中例子介绍test.json内容: hello 我们 326342 1.文件读取 ( ...

  7. python3学习--文件读写

    这一篇我们来看文件读写操作. 打开和创建文件主要是open()函数: f = open('filename','r') # 读模式 f = open('filename','w') # 写模式 f = ...

  8. Python3 IO编程之文件读写

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

  9. python3:文件读写+with open as语句(转)

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

随机推荐

  1. 第四篇:“ 不确定 "限制值的使用

    前言 前篇文章解释了限制值的五种类型以及获取它们的方法.但是对于其中可能不确定的类型( 45类型 ),当限制值获取函数返回-1的时候,我们无法仅通过这个函数返回值-1来判断是限制值获取失败还是限制值是 ...

  2. 一个Demo展示Storyboard的强大

    本文转载至http://www.cocoachina.com/ios/20150330/11440.html 今天我通过完成一个长按cell删除的Demo,向你们展示熟练运用storyboard和Au ...

  3. 69、ViewPagerIndicator+ViewPager实现Tab

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  4. c++调用python函数时,使用PyArray_SimpleNewFromData(nd, dims, typenum, data)函数时出现内存错误的问题

    示例程序: int main(int argc, char *argv[]){ PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,* ...

  5. Google I/O 2013 – Volley: Easy, Fast Networking for Android

    1.什么是volley          Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮.Volle ...

  6. 编写高质量代码--改善python程序的建议(七)

    原文发表在我的博客主页,转载请注明出处! 建议三十四:掌握字符串的基本用法 编程有两件事,一件是处理数值,另一件是处理字符串,在商业应用编程来说,处理字符串的代码超过八成,所以需要重点掌握. 首先有个 ...

  7. [HEOI2015]兔子与樱花[贪心]

    4027: [HEOI2015]兔子与樱花 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1043  Solved: 598[Submit][Stat ...

  8. Android异步处理之AsyncTaskLoader简单使用

    简介 不管是在Android应用开发还是Android平台开发中,异步处理通常是最基本的coding要求.如果你还在主线程中写一些数据库,网络请求,读写本地文件等操作的话那说明你还不是一个合格的And ...

  9. 第二课作业——redis常用命令

    第二课时作业 静哥 by 2016.2.23~2016.2.22   [作业描述] 1.key string list hash结构中,每个至少完成5个命令,包含插入 修改 删除 查询,list 和h ...

  10. VB 十六进制转汉字的函数

    Public Function HexToStr(ByVal strs As String) As String Dim i As Integer, tmp As String, n If Len(s ...