Search code examples
copyappdata

Python copy a file from appdata


how do I copy a file that is present in c:/users/robert/appdata/OpenOffice/work.txt from here to the folder where i execute the python script ? and without adding the folder name (robert) in python script?

from shutil import copyfile

copyfile(src, dst)


Solution

  • import getpass,shutil,os
    src="C:\Users\%s\AppData\OpenOffice\work.txt" %(getpass.getuser())
    
    dest=os.getcwd()
    def copyFile(src, dest):
        try:
            shutil.copy(src, dest)
            print("Done")
        # eg. src and dest are the same file
        except shutil.Error as e:
            print('Error: %s' % e)
        # eg. source or destination doesn't exist
        except IOError as e:
            print('Error: %s' % e.strerror)
    
    copyFile(src,dest)