-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_file_write.py
More file actions
56 lines (41 loc) · 1.31 KB
/
io_file_write.py
File metadata and controls
56 lines (41 loc) · 1.31 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
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-03-22 22:49:15
# @copyright by hoojo@2018
# @changelog Added python3 `io -> file write` 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')
try:
file.truncate() # 清空文件内容
# 指针移动到最开始位置
file.seek(0, 0)
print('1-tell: ', file.tell())
file.write('This is a new line\n')
file.write('This is a new line2\n')
file.write('This is a new line3\n')
file.write('This is a new line4\n')
file.write('This is a new line5\n')
# 0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。
file.seek(0, 0)
print('readline: %s' % file.readline())
print('2-tell: ', file.tell())
# 代表从文件末尾算起
file.seek(0, 2)
file.write('This is a new line666\n')
# 读取所有
file.seek(0, 0)
print('readline: %s' % file.read(-1))
except NameError as e:
print('error:', e)
else:
file.close()
print('file colse:', file)