虽然当前数据放在内存中存取的速度要比硬盘中快,但一旦断电则会丢失,所以尽量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基础入门学习笔记 012列表:一个打了激素的数组3
下一篇:Python基础入门学习笔记 020函数:内嵌函数和闭包
python









