forked from 521xueweihan/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex16.py
56 lines (45 loc) · 1.15 KB
/
ex16.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
47
48
49
50
51
52
53
54
55
56
#coding:utf-8
#################
# 习题16:读写文件
#################
# 前言
#
# 熟悉一下方法
# close--关闭文件
# read--读取文件内容
# readline--读取文本文件中的一行
# truncate--清空文件
# write(stuff)--将stuff写入文件
#
from sys import argv
script, filename = argv
print "We're going to erase %r." % filename #抹去
print "If you don't want that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."
raw_input("?")
print "Opening the file..."
target = open(filename, 'w+')
print "Truncating the file. Goodbye!"
target.truncate()
print "Now I'm going to ask you for three lines."
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
# 替换上面的写入
# target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
print "And finally, we close it."
target.close()
# 笔记
#
# open方法有很多模式:
# 'r'——读取
# 'w'——写入
# 'a'——追加
# 提示:'w+'等可以同时用读写的方式打开