Skip to content

Commit c287d28

Browse files
committed
p3
1 parent 11cadcf commit c287d28

2 files changed

Lines changed: 68 additions & 23 deletions

File tree

‎127.md‎

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
#文件(2)
44

5-
上一节,对文件有了初步认识。要牢记,文件无非也是一种类型的数据。
5+
文件,也是对象。
6+
7+
在Python 2中还是一种内建的类型,在Python 3中被取消了,可是,这并不意味这其地位降低。
68

79
##文件的状态
810

@@ -16,48 +18,81 @@
1618
>>> file_stat.st_ctime #这个是文件创建时间
1719
1407734600.0882277
1820

19-
这是什么时间?看不懂!别着急,换一种方式。在python中,有一个模块`time`,是专门针对时间设计的。
21+
这是什么时间?看不懂!别着急,换一种方式。在Python中,有一个模块`time`,是专门针对时间设计的。
2022

2123
>>> import time
2224
>>> time.localtime(file_stat.st_ctime) #这回看清楚了。
2325
time.struct_time(tm_year=2014, tm_mon=8, tm_mday=11, tm_hour=13, tm_min=23, tm_sec=20, tm_wday=0, tm_yday=223, tm_isdst=0)
2426

2527
##read/readline/readlines
2628

27-
上节中,简单演示了如何读取文件内容,但是,在用`dir(file)`的时候,会看到三个函数:read/readline/readlines,它们各自有什么特点,为什么要三个?一个不行吗?
29+
用`open()`能够打开文件,在用for循环,可以将文件的内容读取出来。
2830

29-
在读者向下看下面内容之前,请想一想,如果要回答这个问题,你要用什么方法?注意,我问的是用什么方法能够找到答案,不是问答案内容是什么。因为内容,肯定是在某个地方存放着呢,关键是用什么方法找到。
31+
但是,在查看文件的属性和方法的时候,会看到三个方法:`read, readline, readlines`。
3032

31-
搜索?是一个不错的方法。
33+
从名称上看,它们应该都是跟“读”有关的,但是,又应该有差别。
3234

33-
还有一种,就是在交互模式下使用的,你肯定也想到了。
35+
的确如此。
3436

35-
>>> help(file.read)
37+
还是对比着看一看,并且还要Python 2和Python 3的文档对比。
3638

37-
用这样的方法,可以分别得到三个函数的说明:
39+
Python 2:
3840

41+
>>> help(file.read)
42+
Help on method_descriptor:
3943
read(...)
4044
read([size]) -> read at most size bytes, returned as a string.
4145

4246
If the size argument is negative or omitted, read until EOF is reached.
4347
Notice that when in non-blocking mode, less data than what was requested
4448
may be returned, even if no size parameter was given.
45-
49+
50+
>>> help(file.readline)
51+
Help on method_descriptor:
4652
readline(...)
4753
readline([size]) -> next line from the file, as a string.
4854

4955
Retain newline. A non-negative size argument limits the maximum
5056
number of bytes to return (an incomplete line may be returned then).
5157
Return an empty string at EOF.
52-
58+
59+
>>> help(file.readlines)
60+
Help on method_descriptor:
5361
readlines(...)
5462
readlines([size]) -> list of strings, each a line from the file.
5563

5664
Call readline() repeatedly and return a list of the lines so read.
5765
The optional size argument, if given, is an approximate bound on the
5866
total number of bytes in the lines returned.
5967

60-
对照一下上面的说明,三个的异同就显现了。
68+
Python 3:
69+
70+
>>> f = open("130.txt")
71+
>>> help(f.read)
72+
Help on built-in function read:
73+
read(size=-1, /) method of _io.TextIOWrapper instance
74+
Read at most n characters from stream.
75+
76+
Read from underlying buffer until we have n characters or we hit EOF.
77+
If n is negative or omitted, read until EOF.
78+
79+
>>> help(f.readline)
80+
Help on built-in function readline:
81+
readline(size=-1, /) method of _io.TextIOWrapper instance
82+
Read until newline or EOF.
83+
84+
Returns an empty string if EOF is hit immediately.
85+
86+
>>> help(f.readlines)
87+
Help on built-in function readlines:
88+
readlines(hint=-1, /) method of _io.TextIOWrapper instance
89+
Return a list of lines from the stream.
90+
91+
hint can be specified to control the number of lines read: no more
92+
lines will be read if the total size (in bytes/characters) of all
93+
lines so far exceeds hint.
94+
95+
对照一下上面的说明,三个的异同就显现了。而且,要想了解Python 2和Python 3之间的不同,还可以将两个版本的对照一下。好像也没有什么不同哦。
6196

6297
EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/End-of-file)中居然有对它的解释:
6398

@@ -89,13 +124,13 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
89124
'You Raise Me Up\nWhen I am down and, oh my soul, so weary;\nWhen troubles come and my heart burdened be;\nThen, I am still and wait here in the silence,\nUntil you come and sit awhile with me.\nYou raise me up, so I can stand on mountains;\nYou raise me up, to walk on stormy seas;\nI am strong, when I am on your shoulders;\nYou raise me up: To more than I can be.\n'
90125
>>> f.close()
91126

92-
**提示:养成一个好习惯,**只要打开文件,不用该文件了,就一定要随手关闭它。如果不关闭它,它还驻留在内存中,后面又没有对它的操作,是不是浪费内存空间了呢?同时也增加了文件安全的风险。
127+
**提示:养成一个好习惯,**只要打开文件,不用该文件了,就一定要随手关闭它。
93128

94-
>注意:在python中,'\n'表示换行,这也是UNIX系统中的规范。但是,在奇葩的windows中,用'\r\n'表示换行。python在处理这个的时候,会自动将'\r\n'转换为'\n'。
129+
>注意:在python中,'\n'表示换行,这也是UNIX系统中的规范。但是,在奇葩的windows中,用'\r\n'表示换行。Python在处理这个的时候,会自动将'\r\n'转换为'\n'。
95130
96131
请仔细观察,得到的就是一个大大的字符串,但是这个字符串里面包含着一些符号`\n`,因为原文中有换行符。如果用print输出这个字符串,就是这样的了,其中的`\n`起作用了。
97132

98-
>>> print content
133+
>>> print content #Python 3: print(content)
99134
You Raise Me Up
100135
When I am down and, oh my soul, so weary;
101136
When troubles come and my heart burdened be;
@@ -128,7 +163,7 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
128163
line = f.readline()
129164
if not line: #到EOF,返回空字符串,则终止循环
130165
break
131-
print line , #注意后面的逗号,去掉print语句后面的'\n',保留原文件中的换行
166+
print line , #Python 3: print(line, end='')
132167

133168
f.close() #别忘记关闭文件
134169

@@ -155,7 +190,7 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
155190
返回的是一个列表,列表中每个元素都是一个字符串,每个字符串中的内容就是文件的一行文字,含行末的符号。显而易见,它是可以用for来循环的。
156191

157192
>>> for line in content:
158-
... print line ,
193+
... print line , #Python 3: print(line, end='')
159194
...
160195
You Raise Me Up
161196
When I am down and, oh my soul, so weary;
@@ -172,11 +207,11 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
172207

173208
前面已经说明了,如果文件太大,就不能用`read()`或者`readlines()`一次性将全部内容读入内存,可以使用while循环和`readline()`来完成这个任务。
174209

175-
此外,还有一个方法:fileinput模块
210+
此外,还有一个方法:`fileinput`模块
176211

177212
>>> import fileinput
178213
>>> for line in fileinput.input("you.md"):
179-
... print line ,
214+
... print line , #Python 3: print(line, end='')
180215
...
181216
You Raise Me Up
182217
When I am down and, oh my soul, so weary;
@@ -195,7 +230,7 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
195230
还有一种方法,更为常用:
196231

197232
>>> for line in f:
198-
... print line ,
233+
... print line , #Python 3: print(line, end='')
199234
...
200235
You Raise Me Up
201236
When I am down and, oh my soul, so weary;
@@ -207,19 +242,19 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
207242
I am strong, when I am on your shoulders;
208243
You raise me up: To more than I can be.
209244

210-
之所以能够如此,是因为file是可迭代的数据类型,直接用for来迭代即可。
245+
之所以能够如此,是因为文件是可迭代的对象,直接用for来迭代即可。
211246

212247
##seek
213248

214-
这个函数的功能就是让指针移动。特别注意,它是以字节为单位进行移动的。比如:
249+
这个函数的功能就是让指针移动。比如:
215250

216251
>>> f = open("you.md")
217252
>>> f.readline()
218253
'You Raise Me Up\n'
219254
>>> f.readline()
220255
'When I am down and, oh my soul, so weary;\n'
221256

222-
现在已经移动到第四行末尾了,看`seek()`的能力:
257+
现在来看`seek()`的能力:
223258

224259
>>> f.seek(0)
225260

@@ -232,12 +267,17 @@ EOF什么意思?End-of-file。在[维基百科](http://en.wikipedia.org/wiki/E
232267

233268
>>> f.tell()
234269
17L
270+
271+
这不是在低17行。在Python 2中返回的是`17L`,很容易让人产生如此误解。但是在Python 3中返回的是`17`,又可能迷茫,单位是什么呢?
272+
273+
读者不妨数一数,是按照字符,`'You Raise Me Up\n'`从0开始,到最后,是不是正好为`17`。提醒注意的是,如果你用汉语的等文字,就需要注意编码问题了。请查看本教程有关编码的讨论。
274+
235275
>>> f.seek(4)
236276

237277
`f.seek(4)`就将位置定位到从开头算起的第四个字符后面,也就是"You "之后,字母"R"之前的位置。
238278

239279
>>> f.tell()
240-
4L
280+
4L #Python 3返回:4
241281

242282
`tell()`也是这么说的。这时候如果使用`readline()`,得到就是从当前位置开始到行末。
243283

@@ -265,6 +305,8 @@ whence的值:
265305
- 是1时,表示从当前位置开始计算偏移量。offset如果是负数,表示从当前位置向前移动,整数表示向后移动。
266306
- 是2时,表示相对文件末尾移动。
267307

308+
前面已经提到了,文件是可迭代的,并且还学过其它可迭代的对象,看来迭代是一个有必要讨论的问题。
309+
268310
------
269311

270312
[总目录](./index.md)   |   [上节:文件(1)](./126.md)   |   [下节:迭代](./128.md)

‎1code/127.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
����ʦhello
2+
����˫ܰ
3+
����ʦ

0 commit comments

Comments
 (0)