JohnTheBadge
JohnTheBadge

Reputation: 67

Copy .txt file onto a separate .txt file - python

Hello this question will probably be rather straight forward.

I have a .txt file, lets stay fruits.txt , In the code when each fruit is randomly selected it is removed from the .txt file so it is not used again.

However when the selection process or "quiz" is ran again, obviously all of the fruits in fruits.txt have been deleted.

I was wondering if there is such code that will allow for another .txt file, such as fruitsoriginal.txt to have it's content copied over to fruits.txt.

I doubt this will help at all but this is the code that removes a fruit from the list, a is a random fruit.

 f = open("fruits.txt","r")
 lines = f.readlines()
 f.close()
 f = open("fruits.txt","w")
 for line in lines:
    if line!=a+"\n":
     f.write(line) 
 f.close()

Upvotes: 0

Views: 120

Answers (1)

ndpu
ndpu

Reputation: 22571

Use copy from shutil

import shutil
shutil.copy('fruitsoriginal.txt', 'fruits.txt')

Upvotes: 3

Related Questions