- 先open, 并赋值给一个中间变量(当然这个中间变量可以省略).
- 对中间变量进行操作,操作的结果对原来文本依然有效.
举例: 将 from_file. txt 复制到 to_file. txt 中
- 借助中间变量
from sys import argv
script, from_file, to_file = argv
in_file = open(from_file) # 打开 from_file 并赋值给 in_file.
out_file = open(to_file,'w') #打开 to_file 并赋值给 out_file.因为要“写” to_file,所以以 ‘w’ 的模式打开.
out_file.write(in_file())
out_file.close()
in_file.close()
- 不借助中间变量
from sys import argv
script, from_file, to_file = argv
open(to_file, 'w').write(open(from_file).read()) # 以‘写’的模式打开 to_file,并将 from_file 的内容直接写进去.