尝试覆盖文件时,原始数据会被删除但不会被替换

时间:2015-07-29 12:40:13

标签: python

我是初学者并学习Python。

我已经创建了这个来练习函数编写。我知道没有必要这样写。

我的问题是out_file数据被删除,而不是替换为预期的in_file数据。

from sys import argv
script, in_file, out_file = argv

print "Copying from filename '%s' to filename '%s'" % (in_file, out_file)

print "The input data reads as: \n"

in_file = open(in_file)

print in_file.read()

print "The file to be overwritten originally reads as \n"

out_file = open(out_file, 'w+')
print out_file.read()


print "Now, to practice function writing, we will use one to   overwrite:"

def overwrite():

    out_file.write(in_file.read())

    print out_file.read()


print "The overwritten file now reads as:\n"
overwrite()

因此> python ex20.py cat.txt dog.txt 的输出是:(其中dog.txt =“狗是最好的”,而cats.txt =“猫是最好的“)

Copying from filename 'cat' to filename 'dog'
The input data reads as: 

cats are the best

The file to be overwritten originally reads as 


Now, to practice function writing, we will use one to overwrite:
The overwritten file now reads as:

0 个答案:

没有答案