Re: Granularity of OSError

2009-09-21 Thread kj
In mailman.122.1253409745.2807.python-l...@python.org MRAB pyt...@mrabarnett.plus.com writes: kj wrote: In mailman.107.1253369463.2807.python-l...@python.org MRAB pyt...@mrabarnett.plus.com writes: If, for example, you're going to copy a file, it's a good idea to check beforehand that

Re: Granularity of OSError

2009-09-20 Thread ryles
On Sep 19, 9:22 pm, MRAB pyt...@mrabarnett.plus.com wrote: The point is that it's sometimes a good idea to do a cheap check first before attempting an operation that's 'expensive' even when it fails. Strongly agree. Furthermore, with LBYL it's often easier to give a user clearer error messages

Re: Granularity of OSError

2009-09-19 Thread kj
In mailman.107.1253369463.2807.python-l...@python.org MRAB pyt...@mrabarnett.plus.com writes: If, for example, you're going to copy a file, it's a good idea to check beforehand that there's enough space available for the copy. How do you do that? TIA, kynn --

Re: Granularity of OSError

2009-09-19 Thread MRAB
kj wrote: In mailman.107.1253369463.2807.python-l...@python.org MRAB pyt...@mrabarnett.plus.com writes: If, for example, you're going to copy a file, it's a good idea to check beforehand that there's enough space available for the copy. How do you do that? There's os.statvfs(...),

Re: Granularity of OSError

2009-09-19 Thread Grant Edwards
On 2009-09-20, MRAB pyt...@mrabarnett.plus.com wrote: kj wrote: In mailman.107.1253369463.2807.python-l...@python.org MRAB pyt...@mrabarnett.plus.com writes: If, for example, you're going to copy a file, it's a good idea to check beforehand that there's enough space available for the

Re: Granularity of OSError

2009-09-18 Thread Sean DiZazzo
On Sep 18, 11:54 am, kj no.em...@please.post wrote: I've often come across the idea that good Python style deals with potential errors using an EAFP (easier to ask forgiveness than permission) strategy rather than a LBYL (look before you leap) strategy. For example, LBYL would look like

Re: Granularity of OSError

2009-09-18 Thread Jeff McNeil
On Sep 18, 3:05 pm, Sean DiZazzo half.ital...@gmail.com wrote: On Sep 18, 11:54 am, kj no.em...@please.post wrote: I've often come across the idea that good Python style deals with potential errors using an EAFP (easier to ask forgiveness than permission) strategy rather than a LBYL

Re: Granularity of OSError

2009-09-18 Thread kj
In 254eac4d-ce19-4af9-8c6a-5be8e7b0f...@u16g2000pru.googlegroups.com Sean DiZazzo half.ital...@gmail.com writes: On Sep 18, 11:54=A0am, kj no.em...@please.post wrote: I've often come across the idea that good Python style deals with potential errors using an EAFP (easier to ask forgiveness

Re: Granularity of OSError

2009-09-18 Thread Ryan Kelly
You can access the exception object which gives you greater detail. try: os.unlink(some_file) except OSError, e: print e.errno print e.strerror if e.errno == 2: pass else: raise I do this myself in a lot of places, almost exactly like

Re: Granularity of OSError

2009-09-18 Thread Christian Heimes
kj wrote: For example, LBYL would look like this: if os.path.isfile(some_file): os.unlink(some_file) In contrast, EAFP would look like this: try: os.unlink(some_file) except OSError: pass The two version aren't equal. The first one suffers from a race condition which

Re: Granularity of OSError

2009-09-18 Thread Sean DiZazzo
On Sep 18, 5:23 pm, Christian Heimes li...@cheimes.de wrote: kj wrote: For example, LBYL would look like this: if os.path.isfile(some_file):     os.unlink(some_file) In contrast, EAFP would look like this: try:     os.unlink(some_file) except OSError:     pass The two version

Re: Granularity of OSError

2009-09-18 Thread Grant Edwards
On 2009-09-19, Christian Heimes li...@cheimes.de wrote: kj wrote: For example, LBYL would look like this: if os.path.isfile(some_file): os.unlink(some_file) In contrast, EAFP would look like this: try: os.unlink(some_file) except OSError: pass The two version aren't