Python Forum

Full Version: copy paste file and re-name it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am building some automated tests including install/uninstall of an app. I need to build a small back-up file system for a file.

I have a file called User.gdh inside this folder:
- C:\ProgramData\MyProgram\Database , so basically the full path is this C:\ProgramData\MyProgram\Database\User.gdh

I want to create a copy of this file inside the same folder that will be called User.gdh.testcopy.

Now if User.gdh.testcopy exists in this folder, I want to copy it over User.gdh.
If User.gdh.testcopy does not exists in folder(this will happen only after a freshInstall of my tested program), I want to copy it from User.gdh and name with test copy inside the same folder.

I've tried this:
    def userGdhBackup():

        userGdhPath = C:\ProgramData\MyProgram\Database\User.gdh
        userGdhTestCopy = C:\ProgramData\MyProgram\Database\User.gdh.testcopy

        if os.path.isfile(userGdhTestCopy):
            shutil.copy(userGdhTestCopy,userGdhPath)
        else:
            shutil.move(userGdhPath,userGdhTestCopy)        
I guess this is not quite good and I am also getting "Permission denied error"

What would be the best solution here?
If you get "permission denied error", you probably need admin rights.