473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Check if a symlink is broken or circular

Hi there,
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.

def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
Nov 30 '07 #1
3 4116
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.

def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin
Nov 30 '07 #2
On 1 Dic, 00:10, "Martin v. Löwis" <mar...@v.loewis.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0

You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin
Mmmm... do you mean something like this?
Could it be ok?

import os, errno

def isvalidlink(path):
assert os.path.lexists(path)
try:
os.stat(path)
except os.error, err:
# broken link
# "No such file or directory"
if err.errno == errno.ENOENT:
return 1
# circular link
# "Too many levels of symlinks"
elif err.errno == errno.ELOOP:
return 2
# something else occurred,
# assume it as invalid anyway
else:
return 3
return 0

Dec 1 '07 #3
Giampaolo Rodola' wrote:
On 1 Dic, 00:10, "Martin v. L�wis" <mar...@v.loewis.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin

Mmmm... do you mean something like this?
Could it be ok?

I think he meant something like this (see below):
>
import os, errno

def isvalidlink(path):
assert os.path.lexists(path)
try:
os.stat(path)
except os.error, err:
# broken link
# "No such file or directory"
if err.errno == errno.ENOENT:
return 1
Right, except that it should be return 0 (or preferably return False)
unless you want inverted logic.
# circular link
# "Too many levels of symlinks"
elif err.errno == errno.ELOOP:
return 2
Change to return 1/True (inverted) or return 0/False (not inverted)
# something else occurred,
# assume it as invalid anyway
else:
return 3
Change "return 3" to "raise", so the exception gets propagated.
return 0
Again, if you do not want the logic to be inverted, use return 1/True
instead

--Nathan Davis
Dec 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patrick Lioi | last post by:
I've developed a simple content management system in PHP, and I'd like to be able to put the code for it in a single place, and have several sites on one server seamlessly share the code. I want...
1
by: marco | last post by:
Hi all, I know about os.symlink(src, dst), but is there a way to overwrite an existing symlink e.g. "ln -sf"? Short of checking and, if necessary, having to delete the existing link by hand......
0
by: A. Murat Eren | last post by:
Hi, I have a problem about zipfile. I'm trying to add files from local file system into a zip file via zipfile module of python.. I have a function in my class which recursively adds files ...
0
by: lynn | last post by:
Hello, I tried to install on my mac osx 10.3.3 my mysql 4.0.13 databases on an other partition than its application; I know it is possible because I managed to do it previously with jaguar....
10
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is...
2
by: wheresjim | last post by:
I have a project in which I only need read access to a Microsoft Access database (.mdb file) via JDBC and unfortunately, the file is frequently locked by another application when I need access...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
1
by: | last post by:
I'm developing a website (PHP/Apache), and I need to be able to use a 'symlink' inside a URL where the symlink is a folder not a file. I have been able to use symlinks that point directly to a...
2
by: Nikolaus Rath | last post by:
Hello, symlink(...) symlink(src, dst) Create a symbolic link pointing to src named dst. Is there any reason why this is so deliberately confusing? Why is the
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.