I have made a small tkinter application with python 3.7.3 .py files (Windows 10).
Now I want to copy the same mdb
temporary file (temp) with different name to another folder many times. My program can copy and rename temporary file first time, but in the second time that file can't be copied.
It shows:
FileExistsError: [WinError 183] Cannot create a file when that file already exists:
I try to add
if os.path.isfile(dsc):
before
shutil.copy(src, dst)
in order to overwrite the file, but it still didn't work. Show the same error.
from tkinter import *
from tkinter.ttk import *
class View(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.winfo_toplevel().title("Save File")
self.grid()
self.init_view()
def init_view(self):
self.inputText = Label(self)
self.inputText["text"] = "Input Serial ID"
self.inputText.grid(row=0, column=0, sticky=N + E)
self.inputField = Entry(self)
self.inputField["width"] = 20
self.inputField.grid(row=0, column=1, sticky=N + W)
self.save = Button(self)
self.save["text"] = "Save"
self.save.grid(row=1, column=1, sticky=N + W)
self.displayText = Label(self)
self.displayText["text"] = "Status:Idle"
self.displayText.grid(row=3, column=0, columnspan=7, sticky=N)
if __name__ == '__main__':
root = Tk()
app = View(master=root)
root.mainloop()
import os
import shutil
from os import path
from Save_Server_View import View
from tkinter import Tk
# Controller
class Controller:
def __init__(self):
self.app = View(master=Tk())
self.app.save["command"] = self.save_method
self.app.mainloop()
# Copy & Rename File
def save_method(self):
if os.path.exists("Abs Path(HIDE FOR PRIVATE)"):
src = path.realpath("Abs Path(HIDE FOR PRIVATE)")
dir = os.path.dirname(os.path.realpath('__file__'))
dst = os.path.join(dir, "..\\test data\\")
message = 'source={} => destination={}'.format(src, dst)
self.app.displayText["text"] = message
shutil.copy(src, dst)
for dst_filename in os.listdir(dst):
new_dst_filename = self.app.inputField.get() + '.mdb'
os.rename(os.path.join(dst, dst_filename), os.path.join(dst, new_dst_filename))
else:
message = "File didn't exist"
self.app.displayText["text"] = message
# Execute
if __name__ == '__main__':
app = Controller()
I except copy the same temporary file with different names to another folder many times, but it just copy one time.