0

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.

stovfl
  • 14,998
  • 7
  • 24
  • 51
Johann
  • 69
  • 1
  • 7
  • What you do now is: copy the file multiple times **with the same name**, then rename. What you should do: loop though wanted names, copy the file **with new names in the destination**. – h4z3 Oct 25 '19 at 07:47
  • Sorry, @h4z3 Could you write down example code, I still can't understand. – Johann Oct 25 '19 at 07:58
  • From what I understood in your code, you always have the same dst while copying. And only then you have `rename`. Which causes you to have `FileExistsError`. You should copy the file with the new name. – h4z3 Oct 25 '19 at 08:01
  • Unrelated to your ***Questions title***: Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Oct 25 '19 at 08:52
  • see if file already droped, or use tool like filemon, to detect system level file operation. also, need to pay attention that the file you are touching might still been changed – Jack Wu Oct 25 '19 at 09:28
  • @h4z3 thank you for your suggestion. By your suggestion, the program was modified and can be run well. – Johann Oct 25 '19 at 15:22

0 Answers0