-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_file_next.py
46 lines (35 loc) · 995 Bytes
/
io_file_next.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-21 22:42:09
# @copyright by hoojo@2018
# @changelog Added python3 `io -> file next` example
try:
file = open('/tmp/open2.txt', 'r+')
except:
try:
print('retry open file')
file = open('/tmp/open2.txt', 'wb')
except:
print('not open file')
# next 读取文件内容
try:
print('next 读取内容')
for index in range(5):
line = next(file)
print('读取文件 %s 第 %d 行,内容:%s' % (file.name, index, line))
# 指针移动到0
file.seek(0)
print('read 读取内容')
# 读取10 个字节
print('read(10): %s' % file.read(10))
file.seek(0)
# 负则读取所有
print('read(-10): %s' % file.read(-10))
except NameError as e:
print('error:', e)
else:
file.close()
print('file colse:', file)