|  | Checking if the file is a symlink fails |  | |
| | | saswat@gmail.com |  |
| Posted: Thu Aug 28, 2008 9:55 pm Post subject: Checking if the file is a symlink fails |  |
check if file is a symlink Here is a small piece of code:
import os from stat import *
filePath = "/home/xyz/symLinkTest" mode = os.stat(filePath)[ST_MODE]
print 'Find using os.path : ',os.path.islink(filePath)
print 'Find using mode :', S_ISLNK(mode) print 'Is this a regular file : ' S_ISREG(mode)
Output : ---------
Find using os.path : True Find using mode : False Is this a regular file : True
File symLinkTest is a symbolic link.
Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ? |
| |
| | | Christian Heimes |  |
| Posted: Thu Aug 28, 2008 9:55 pm Post subject: Re: Checking if the file is a symlink fails |  |
saswat@gmail.com wrote:
| Quote: | File symLinkTest is a symbolic link.
Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
|
Because you are using os.stat() instead of os.lstat().
LINK
Christian |
| |
| | | saswat@gmail.com |  |
| Posted: Thu Aug 28, 2008 11:41 pm Post subject: Re: Checking if the file is a symlink fails |  |
On Aug 28, 3:11 pm, Christian Heimes <li...@cheimes.de> wrote:
| Quote: | sas...@gmail.com wrote: File symLinkTest is a symbolic link.
Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
Because you are using os.stat() instead of os.lstat().
LINK
Christian
|
Do you mean the following is deprecated ? LINK
From the documentation -
S_ISLNK( mode) Return non-zero if the mode is from a symbolic link.
Thanks -Saswat |
| |
| | | Miles |  |
| Posted: Fri Aug 29, 2008 3:06 am Post subject: Re: Checking if the file is a symlink fails |  |
saswat wrote:
| Quote: | On Aug 28, 3:11 pm, Christian Heimes wrote: sas...@gmail.com wrote: File symLinkTest is a symbolic link.
Why S_ISLNK(mode) returns False and S_ISREG(mode) returns True ?
Because you are using os.stat() instead of os.lstat().
Do you mean the following is deprecated ? LINK
From the documentation -
S_ISLNK( mode) Return non-zero if the mode is from a symbolic link.
|
No, it means that os.stat follows the symbolic link and gives stat information for the file that the symbolic link points to, which is *not* a symbolic link. Like the documentation for lstat says: "Like stat(), but do not follow symbolic links." But why not just use os.path.islink?
-Miles |
| |
| | | Fredrik Lundh |  |
| Posted: Fri Aug 29, 2008 3:20 am Post subject: Re: Checking if the file is a symlink fails |  |
saswat@gmail.com wrote:
| Quote: | Do you mean the following is deprecated ? LINK
From the documentation -
S_ISLNK( mode) Return non-zero if the mode is from a symbolic link.
|
As that page states, that's a function used to interpret a mode flag returned by os.stat, os.fstat, or os.lstat. It obviously won't give you the result you're looking for if you use a stat function that *follows* symbolic links.
</F> |
| |
| | | saswat@gmail.com |  |
| Posted: Fri Aug 29, 2008 8:16 am Post subject: Re: Checking if the file is a symlink fails |  |
On Aug 28, 10:20 pm, Fredrik Lundh <fred...@pythonware.com> wrote:
| Quote: | sas...@gmail.com wrote: Do you mean the following is deprecated ? LINK
From the documentation -
S_ISLNK( mode) Return non-zero if the mode is from a symbolic link.
As that page states, that's a function used to interpret a mode flag returned by os.stat, os.fstat, or os.lstat. It obviously won't give you the result you're looking for if you use a stat function that *follows* symbolic links.
/F
|
Thank you Fredrick and Miles,
stat() is on the actual file and not on the symlink. That explains.
-Saswat |
| |
|
|