Re: Granularity of OSError

2009-09-21 Thread kj
In MRAB writes: >kj wrote: >> In MRAB >> 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(...), although that's Unix only. Thanks!

Re: Granularity of OSError

2009-09-20 Thread ryles
On Sep 19, 9:22 pm, MRAB 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 for common usage errors,

Re: Granularity of OSError

2009-09-19 Thread Grant Edwards
On 2009-09-20, MRAB wrote: > kj wrote: >> In MRAB >> 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(...), although that's Unix only.

Re: Granularity of OSError

2009-09-19 Thread MRAB
kj wrote: In MRAB 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(...), although that's Unix only. The point is that it's sometimes a good idea to do a che

Re: Granularity of OSError

2009-09-19 Thread kj
In MRAB 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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Granularity of OSError

2009-09-19 Thread MRAB
Grant Edwards wrote: On 2009-09-19, Christian Heimes 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 equal.

Re: Granularity of OSError

2009-09-18 Thread Grant Edwards
On 2009-09-19, Christian Heimes 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 Sean DiZazzo
On Sep 18, 5:23 pm, Christian Heimes 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 ve

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 conditio

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 plac

Re: Granularity of OSError

2009-09-18 Thread kj
In <254eac4d-ce19-4af9-8c6a-5be8e7b0f...@u16g2000pru.googlegroups.com> Sean DiZazzo writes: >On Sep 18, 11:54=A0am, kj 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 t

Re: Granularity of OSError

2009-09-18 Thread Jeff McNeil
On Sep 18, 3:05 pm, Sean DiZazzo wrote: > On Sep 18, 11:54 am, kj 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") > > st

Re: Granularity of OSError

2009-09-18 Thread Sean DiZazzo
On Sep 18, 11:54 am, kj 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 this: > > if os