Python基础入门学习笔记 028 文件:因为懂你,所以永恒


大多数u程序都是:首先接收输入数据,然后按照要求进行处理,最后输出数据

虽然当前数据放在内存中存取的速度要比硬盘中快,但一旦断电则会丢失,所以尽量ctrl+s保持到硬盘中

什么是文件

打开文件

1 open(file, mode='r', buffering=1, encoding=None,errors=None, newline=None, closefd=True, opener=None) 2 3 open()的第一个参数是传入的文件名,第二个参数是指定文件的打开模式

文件对象方法

1 >>> f = open("D:\\python3.3.2\Hello.txt") 2 >>> f 3 <_io.TextIOWrapper name='D:\\python3.3.2\\Hello.txt' mode='r' encoding='cp936'> 4 >>> f.read() 5 "A. HISTORY OF THE SOFTWARE\n==========================\n\nPython was created in the early 1990s by Guido van Rossum at Stichting\nMathematisch Centrum (CWI, see cwi.nl) in the Netherlands\nas a successor of a language called ABC. Guido remains Python's\nprincipal author, although it includes many contributions fr others.\n\nIn 1995, Guido continued his work on Python at the Corporation for\nNational Research Initiatives (CNRI, see wwwri.reston.va.us)\nin Reston, Virginia where he released several versions of the\nsoftware." 6 >>> flose() 7 >>> f = open("D:\\python3.3.2\Hello.txt") 8 >>> f.read(5) 9 'A. HI' 10 >>> f.tell() #返回当前光标所在文件的位置 11 5 12 >>> f.readline() 13 'STORY OF THE SOFTWARE\n'

将f放入到列表

1 >>> f = open("D:\\python3.3.2\Hello.txt",'w')#w模式写入会覆盖已存在的文件(即原文件内容全部被删除),a模式则在末尾追加写入 2 >>> f.write('who are you') #返回的是写入的字符数 3 11 4 >>> flose()



上一篇:Python基础入门学习笔记 037 类和对象:面向对象编程

下一篇:Python基础入门学习笔记 019函数:我的地盘听我的(局部变量与全局变量)


Python
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图