0

i had posted a previous question a bit same like this one and it got solved but i now am facing a new problem.i want to duplicate a file with the data inside it when the user clicks a button. In my previous question i figured out how to copy a file and paste it but now what happened is that the second i run my program it duplicates my file, and it doesn't even paste the data into the new file, it just creates the new file with the name i specified, but i want it to happen like when the user clicks a button then the file should be duplicated and the data should be inside it. Here is the code i used:

from tkinter import  *


root=Tk()


def duplicatefunction():
    f=open("prototype.txt","r")
    g=open("copy.txt","w")


button1=Button(text="duplicate file", command=duplicatefunction())
button1.pack()


root.mainloop()

so what did i do wrong and how to fix it to copy the data in the files???

Pynchia
  • 10,996
  • 5
  • 34
  • 43

1 Answers1

1

You should not include the () in the command argument in your Button. So write

button1 = Button(text="duplicate file", command=duplicatefunction)

The duplicate function that you have doesn't do anything but opening 2 files. One for reading and one for writing, but you're not actually reading or writing anything.

If you want to create a copy, you can use the copy2() function from the shutil library: https://docs.python.org/3/library/shutil.html

In that case the duplicate function could look like:

def duplicatefunction():
    shutil.copy2('prototype.txt', 'copy.txt')
robbo
  • 525
  • 3
  • 11
  • THANKYOU SO MUCH IT WORKED, I WAS STARING AT MY COMPUTER SINCE 5 HRS THINKING WHT TO DO BUT NOW AT LAST IT WORKED!!!!!!! –  Dec 22 '20 at 05:26
  • hey but when i copy it, there's no data inside the file –  Dec 22 '20 at 05:30
  • odd but pls tell wht to do?? –  Dec 22 '20 at 05:30
  • That is odd... is there data in the original file that you try to copy? – robbo Dec 22 '20 at 05:32
  • yes there was data inside original file –  Dec 22 '20 at 05:37
  • what are you using for the duplicatefunction now? Can you paste it here? – robbo Dec 22 '20 at 05:39
  • ok this is the code i m currently using: –  Dec 22 '20 at 05:50
  • from tkinter import * root=Tk() #specifying the function going to be assigned to the button def duplicatefunction(): f=open("prototype.txt","r") g=open("copy.txt","w+") a=f.read() g.write(a) button1=Button(text="duplicate file", command=duplicatefunction) button1.pack() root.mainloop() –  Dec 22 '20 at 05:51
  • it is not clear but the code is right –  Dec 22 '20 at 05:51
  • Well not really... you're opening files but not closing them, so you might want to use context managers in this case. Also not sure why you would re-invent a copy function that exists and is part of Python's standard library? And hence very well tested by a large community. – robbo Dec 22 '20 at 05:52
  • 1
    anyways don't worry @robbo now the file is working and i got really excited and created tons of duplicates, i'll go delete them now –  Dec 22 '20 at 05:53