Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103301 > unrolled thread
| Started by | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| First post | 2016-02-21 21:42 +0530 |
| Last post | 2016-02-22 21:36 +1100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
can try expect have if else. Ganesh Pal <ganesh1pal@gmail.com> - 2016-02-21 21:42 +0530
Re: can try expect have if else. Steven D'Aprano <steve@pearwood.info> - 2016-02-22 21:36 +1100
| From | Ganesh Pal <ganesh1pal@gmail.com> |
|---|---|
| Date | 2016-02-21 21:42 +0530 |
| Subject | can try expect have if else. |
| Message-ID | <mailman.11.1456071141.20994.python-list@python.org> |
Hi Team,
Iam on python 2.6 , need input on the below piece of code.
EXIT_STATUS_ERROR=1
def create_dataset():
"""
"""
logging.info("Dataset create.....Started !!!")
try:
if os.path.ismount("/nfs_mount"):
touch_file("inode_fixcrc.txt")
logging.info("Dataset create.....Done !!!")
else:
raise Exception("/nfs_mount is not mounted. Dataset create
failed !!!")
return False
except Exception as e:
logging.error(e)
sys.stderr.write("Dataset create failed...Exiting !!!")
sys.exit(EXIT_STATUS_ERROR)
return True
1. Can we have if else with in a try except block
2. How can the above code be improved
Regards,
Ganesh
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-02-22 21:36 +1100 |
| Message-ID | <56cae498$0$1615$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #103301 |
On Mon, 22 Feb 2016 03:12 am, Ganesh Pal wrote:
> Hi Team,
>
> Iam on python 2.6 , need input on the below piece of code.
>
>
> EXIT_STATUS_ERROR=1
>
> def create_dataset():
> """
> """
> logging.info("Dataset create.....Started !!!")
> try:
> if os.path.ismount("/nfs_mount"):
> touch_file("inode_fixcrc.txt")
> logging.info("Dataset create.....Done !!!")
> else:
> raise Exception("/nfs_mount is not mounted. Dataset create
> failed !!!")
> return False
You should not raised Exception directly.
The "return False" line is dead code, it will never be executed.
> except Exception as e:
> logging.error(e)
> sys.stderr.write("Dataset create failed...Exiting !!!")
> sys.exit(EXIT_STATUS_ERROR)
> return True
>
> 1. Can we have if else with in a try except block
Did you try it to see what would happen?
Yes, you can have an if...else block inside a try block.
> 2. How can the above code be improved
# Use a custom exception type.
class CreateDatasetError(OSError):
pass
def touch_file(path, filename):
"""Raises OSError or IOError if path is not a mounted mount point,
and path/filename cannot be created.
"""
if not os.path.ismount(path):
raise CreateDatasetError('%s is not mounted, create failed.' % path)
else:
fullname = os.join(path, filename)
f = open(fullname, 'w')
f.close()
EXIT_STATUS_ERROR=1
def create_dataset():
"""
"""
logging.info("Dataset create.....Started !!!")
try:
touch_file("/nfs_mount", "inode_fixcrc.txt")
except (IOError, OSError) as e:
logging.error(e)
sys.stderr.write("Dataset create failed...Exiting !!!")
sys.exit(EXIT_STATUS_ERROR)
logging.info("Dataset create.....Done !!!")
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web