我只是在学习如何在 Python 中编码,并且无法找到解决方案或答案,说明为什么当我尝试读取刚刚写入的文件时带有额外的字符。
代码
#-*-coding:utf-8-*-
from sys import argv
from os.path import exists
script, source, copy = argv
print "We'll be opening, reading, writing to and closing a file"
opensource = open(source)
readsource = opensource.read()
print readsource
print "Great. We opened and read file"
opencopy = open(copy, 'w+') #we want to write and read file
opencopy.write(readsource) #copy the contents of the source file
opencopy.read()
opensource.close()
opencopy.close()
输出
内容
test °D ΃ ø U ø U ` 6 ` 6 0M Ð
我在 Windows 7 Professional 64bit 上运行 Python 2.7 版。