-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_file_read.py
More file actions
63 lines (49 loc) · 1.34 KB
/
io_file_read.py
File metadata and controls
63 lines (49 loc) · 1.34 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-21 22:55:55
# @copyright by hoojo@2018
# @changelog Added python3 `io -> file read` 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')
# read 读取内容
try:
# 读取10 个字节
print('read(10): %s' % file.read(10))
# 指针移动到0
file.seek(0)
# 负则读取所有
print('read(-10): %s' % file.read(-10))
file.seek(0)
# 读取整行
line = file.readline()
print('read line: %s' % (line))
file.seek(0)
# 读取3个字节
line = file.readline(3)
print('read 3 line: %s' % (line))
file.seek(0)
# 读取所有行
lines = file.readlines()
for line in lines:
print('read lines: %s' % (line))
print('当前位置:%d' % file.tell())
file.seek(0)
print('当前位置:%d' % file.tell())
# 读取3个字节的行
lines = file.readlines(3)
for line in lines:
print('read 3 lines: %s' % (line))
except NameError as e:
print('error:', e)
else:
file.close()
print('file colse:', file)