1 |
On 11/10/2014 03:09 AM, Alexander Berntsen wrote: |
2 |
> On 10/11/14 00:24, Zac Medico wrote: |
3 |
>> ir |
4 |
>> + while current_dir != os.sep: |
5 |
>> + try: |
6 |
>> + st = os.stat(current_dir) |
7 |
>> + except OSError as e: |
8 |
>> + if errno == errno.ENOENT: |
9 |
>> + current_dir = \ |
10 |
>> + os.path.dirname(current_dir) |
11 |
>> + continue |
12 |
>> + break |
13 |
> Can you think of a less terrible way of doing this in Python? |
14 |
|
15 |
Well, you'll have to clarify what's so "terrible" about it. Note that I didn't |
16 |
use os.path.isdir or similar because those functions hide all kinds of relevant |
17 |
exceptions, such as EACCES. |
18 |
|
19 |
If it's the continue and break that upset you, we can do it like this: |
20 |
|
21 |
found_dir = False |
22 |
while current_dir != os.sep and not found_dir: |
23 |
try: |
24 |
os.stat(current_dir) |
25 |
found_dir = True |
26 |
except OSError: |
27 |
if errno == errno.ENOENT: |
28 |
current_dir = \ |
29 |
os.path.dirname(current_dir) |
30 |
else: |
31 |
found_dir = True |
32 |
|
33 |
-- |
34 |
Thanks, |
35 |
Zac |