copy file, keep permissions and owner copy file, keep permissions and owner python python

copy file, keep permissions and owner


You perhaps could use os.stat to get the guid and uid like in this answer and then reset the uid and guid after coping using os.chown.


I did it this way:

import osimport statimport shutildef copyComplete(source, target):    # copy content, stat-info (mode too), timestamps...    shutil.copy2(source, target)    # copy owner and group    st = os.stat(source)    os.chown(target, st.st_uid, st.st_gid)


You can use the subprocess module:

from subprocess import Popenp = Popen(['cp','-p','--preserve',src,dest])p.wait()