"Cloning" file attributes and permissions

Kevin Kelley wyldwolf at gmail.com
Fri Apr 13 09:15:56 EDT 2007


This is what I (just now) put together as an example:

from os import stat,mknod,chown

def match_perms(org_fname,new_fname):
    # Get information on old file
    st = stat(org_fname)
    st_mode = st.st_mode
    st_uid = st.st_uid
    st_gid = st.st_gid

    # Create the new file
    mknod(new_fname,st_mode)

    # Matching permissions
    chown(new_fname,st_uid,st_gid)

if __name__ == "__main__":
    match_perms('old_filename','new_filename')

On 4/13/07, Kevin Kelley <wyldwolf at gmail.com> wrote:
>
> If you know what the permissions are going to be then you can use umask to
> set the default file creation permissions to match. Then any files created
> in that directory will have the correct permissions.
>
> I think the "pythonic" way to solve this problem would be to code up your
> own module which handles all the dirty parts in the background. It would
> create the new file, stat the original, and chmod/chown the new file as
> needed to match the original. All you would have to do after creating the
> module is pass the new function the original and new file information.
>
> --
> Kevin Kelley
>
> On 4/12/07, Paulo da Silva <psdasilvaX at esotericax.ptx> wrote:
> >
> > attn.steven.kuo at gmail.com escreveu:
> > > On Apr 12, 5:19 pm, attn.steven.... at gmail.com wrote:
> > >> On Apr 12, 4:09 pm, Paulo da Silva <psdasil... at esotericaX.ptX >
> > wrote:
> > >>
> > ...
> > >
> > > After poking around a bit I also discovered the
> > > shutil module.  It looks like you can use
> > > shutil.copy2.  More Pythonic, yes?
> > >
> >
> >
> > I have seen that in the index but I thought it was a different thing
> > because it was referenced as "high-level operations".
> > Anyway that helps but I still need to copy the whole file or to use stat
> > and chown for the user/group ids.
> >
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070413/19684ffa/attachment.html>


More information about the Python-list mailing list