-4

How to get all the words of the doc2 in the doc1?

file_stop = open('doc1','r')
isi_stop = file_stop.read()
file_doc1 = open('doc2','r')
isi_doc1 = file_doc1.read()
Gilang Pratama
  • 439
  • 6
  • 18
  • Show what you have tried and define "word". – timgeb Mar 15 '17 at 17:31
  • What have you tried ? I suggest you to read the answer of that question : http://stackoverflow.com/questions/7409780/reading-entire-file-in-python – Malik Fassi Mar 15 '17 at 17:31
  • How to get all the words of the doc2 in the doc1? im sorry if i dont have complete my question. – Gilang Pratama Mar 15 '17 at 17:39
  • Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Sabito stands with Ukraine Feb 08 '21 at 07:00
  • I'm sorry, I don't know about that. – Gilang Pratama Feb 08 '21 at 07:43

3 Answers3

1

If you want to copy the text of doc2 TO doc1 use:

with open('doc2', 'r') as doc2:
    read = doc2.readlines()
    with open('doc1', 'w') as doc2:   # Not 'r' (read), use 'w' (write)
         doc1.writelines(read)

Explanation (sorry if is bad):

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)

I hope that it help you and sorry for my bad english.

Ender Look
  • 2,303
  • 2
  • 17
  • 41
  • thank you for answer. but i need to find the doc1 word in the doc 2 or vice verca. – Gilang Pratama Mar 16 '17 at 09:42
  • @GilangPratama Sorry but i don't understand your question. You want to: copy the text of doc1 to doc2 or to doc2 to doc1 OR you want compare them and print its differences?. – Ender Look Mar 17 '17 at 16:26
0

Your question is not clear at all. Im just guessing here:

You want to read all the contents of doc1 and write them to doc2.

with open('doc2', 'r') as doc2:
    reader = doc2.readlines()
    with open('doc1', 'a') as doc1:
        doc1.writelines(reader)
Piero Marini
  • 131
  • 1
  • 11
0

Do you really give a try to understand your code. You should open your destination file in a write mode first to be able to write.

So your third line of code should be.

file_doc1=open('doc2','w')

and as well in your fourth line of code your are using read function where you should be using write function as you have to copy the contents of doc1 to it it would be as below.

file_doc1.write(isi_stop)

here you would put to doc2 what you have read from doc1.

Naseer Mohammad
  • 389
  • 4
  • 14