"Cloning" file attributes and permissions

P

Paulo da Silva

Hi!

I need to process a file to produce another file that *must* have
*exactly* the same attributes and permissions of the former. What is the
best way to do this? The file must not exist with contents (it may exist
empty) unless it has the same attributes and permissions.
I know how to do this using, let me call it, "C type code" (stat, chmod,
chown, etc). I would like to hear some opinions on if and how it would
be possible in a more elegant/python way.

Thanks.
Paulo
 
A

attn.steven.kuo

Hi!

I need to process a file to produce another file that *must* have
*exactly* the same attributes and permissions of the former. What is the
best way to do this? The file must not exist with contents (it may exist
empty) unless it has the same attributes and permissions.
I know how to do this using, let me call it, "C type code" (stat, chmod,
chown, etc). I would like to hear some opinions on if and how it would
be possible in a more elegant/python way.


Are you using a system that supports the creation
of a hard link?

If so, try os.link.
 
P

Paulo da Silva

(e-mail address removed) escreveu:
Are you using a system that supports the creation
of a hard link?

If so, try os.link.

May be I missed something exposing my q.
I need to process an input file "foo" to produce a different contents
file "bar". "bar" must have the same attributes/permissions of "foo".
I forgot to say that the OS is Linux.
 
A

attn.steven.kuo

(e-mail address removed) escreveu:






May be I missed something exposing my q.
I need to process an input file "foo" to produce a different contents
file "bar". "bar" must have the same attributes/permissions of "foo".
I forgot to say that the OS is Linux.



Sorry, I did misunderstand your question at first.
Well, you do have stat, chown, chmod in Python; but
if you're on Linux, you might just want to use
the "cp" command with the "-p" switch:

import subprocess
retcode = subprocess.call([ "/bin/cp", "-p", oldfile, newfile ])
On my system, this preserves the access permissions and ownership.

And if you modify the file after copying, you can alter
the access and modification times with posix.utime
to match that of the original.
 
A

attn.steven.kuo

(snipped)



import subprocess
retcode = subprocess.call([ "/bin/cp", "-p", oldfile, newfile ])
On my system, this preserves the access permissions and ownership.

And if you modify the file after copying, you can alter
the access and modification times with posix.utime
to match that of the original.


After poking around a bit I also discovered the
shutil module. It looks like you can use
shutil.copy2. More Pythonic, yes?
 
P

Paulo da Silva

(e-mail address removed) escreveu:
....

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top