Tags: application, failing, following, links, manipulating, programming, python, reckognize, run, stat, symbolic, toknow
stat doesnt work!
On Programmer » Python
2,570 words with 3 Comments; publish: Wed, 26 Dec 2007 23:16:00 GMT; (20062.50, « »)
I am doing an application that needs to
know when it is manipulating symbolic links.
But it is failing to reckognize them.
If I run the following code:
<code>
import sys
import os
from stat import *
mode = os.stat(sys.argv[1])[ST_MODE]
if S_ISLNK(mode):
print 'File is a link'
else:
print 'Just a normal file'
</code
I will alway get the 'Just a normal file'
message even though I am using a symbolic
link as argument.
I've done the same example in Perl and in
bash and it works.
Could this be a bug in my Python version
(2.2.2) or I am doing something wrong ?
Thanks in advance,
Paulo Pinto
http://python.itags.org/q_python_84960.html
All Comments
Leave a comment...
- 3 Comments

- On Mon, Feb 09, 2004 at 05:10:31PM +0100, Paulo Pinto wrote:
> I am doing an application that needs to
> know when it is manipulating symbolic links.
try os.lstat.
>>> import os
>>> from stat import *
>>> S_ISLNK(os.stat("/proc/self").st_mode)
0
>>> S_ISLNK(os.lstat("/proc/self").st_mode)
1
Jeff
#1; Wed, 26 Dec 2007 23:17:00 GMT

- In article <c08bdn$fq6$1.python.itags.org.sunnews.cern.ch>,
Paulo Pinto <paulo.pinto.python.itags.org.cern.ch> wrote:
> I am doing an application that needs to
> know when it is manipulating symbolic links.
That's a special case, and therefore you have
to use a special version of stat(). stat()
will follow the link, so you get the original
file. lstat() will report the link itself.
Donn Cave, donn.python.itags.org.u.washington.edu
#2; Wed, 26 Dec 2007 23:18:00 GMT

- It works now.
Thanks for your answers.
Paulo Pinto wrote:
> I am doing an application that needs to
> know when it is manipulating symbolic links.
> But it is failing to reckognize them.
> If I run the following code:
> <code>
> import sys
> import os
> from stat import *
> mode = os.stat(sys.argv[1])[ST_MODE]
> if S_ISLNK(mode):
> print 'File is a link'
> else:
> print 'Just a normal file'
> </code>
>
> I will alway get the 'Just a normal file'
> message even though I am using a symbolic
> link as argument.
> I've done the same example in Perl and in
> bash and it works.
> Could this be a bug in my Python version
> (2.2.2) or I am doing something wrong ?
> Thanks in advance,
> Paulo Pinto
#3; Wed, 26 Dec 2007 23:19:00 GMT