with open('doc2', 'r') as doc2:
read = doc2.readlines()
with open('doc1', 'w') as doc2: # Not 'r' (read), use 'w' (write)
doc1.writelines(read)
解释(如果不好,请道歉):
open(file that you want to open, mode of opening)
# open the file with an specific mode, ('r' read, 'w' write, there are other like 'r+', 'w+', etc)
# you have to use open with a variable like:
file = open('doc1', 'r')
with open('doc1', 'r') as doc:
# you open the doc1 in the variable doc for a shot while, i mean, when you finish to use the file, it will automatly close.
read = doc2.readlines()
# you read the file (in this case 'doc2') (only can be done with reading modes [or readding and writting modes]) and load it in a variable ('read')
doc1.writelines(read)
# you write in the file ('doc1') all the text or values loaded in the variable ('read'), (you can only write in writting modes (or writting and readding)
如果要将doc2的文本复制到doc1,请使用:
解释(如果不好,请道歉):
我希望这对你有帮助,也为我的英语不好感到抱歉。你知道吗
你真的试着去理解你的代码吗。您应该首先以写模式打开目标文件,以便能够进行写操作。你知道吗
所以你的第三行代码应该是。你知道吗
在第四行代码中,您使用的是read函数,您应该使用write函数,因为您必须将doc1的内容复制到其中 具体如下。你知道吗
在这里你将把你从doc1读到的东西放到doc2。你知道吗
你的问题一点也不清楚。我只是在猜:
您希望读取doc1的所有内容并将其写入doc2。你知道吗
相关问题 更多 >
编程相关推荐