一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
大多数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 http://www.cwi.nl) in the Netherlands\nas a successor of a language called ABC. Guido remains Python's\nprincipal author, although it includes many contributions from others.\n\nIn 1995, Guido continued his work on Python at the Corporation for\nNational Research Initiatives (CNRI, see http://www.cnri.reston.va.us)\nin Reston, Virginia where he released several versions of the\nsoftware."
 6 >>> f.close()
 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 >>> f.close()

posted on 2023-08-23 10:00  一杯清酒邀明月  阅读(17)  评论(0编辑  收藏  举报