with-statements have a syntactic quirk, which I think would be useful to fix.
This is true in Python 2.7 through 3.3, but it's likely not fixable until 3.4,
unless of course it's a bug <wink>.
Legal:
>>> with open('/etc/passwd') as p1, open('/etc/passwd') as p2: pass
Not legal:
>>> with (open('/etc/passwd') as p1, open('/etc/passwd') as p2): pass
Why is this useful? If you need to wrap this onto multiple lines, say to fit
it within line length limits. IWBNI you …
[View More]could write it like this:
with (open('/etc/passwd') as p1,
open('/etc/passwd') as p2):
pass
This seems analogous to using parens to wrap long if-statements, but maybe
there's some subtle corner of the grammar that makes this problematic (like
'with' treating the whole thing as a single context manager).
Of course, you can wrap with backslashes, but ick!
Cheers,
-Barry
[View Less]
A couple of times recently on different projects, I've had the need to clear out
the contents of an existing directory. There doesn't appear to be any function
in shutil to do this. I've used
def clear_directory(path):
for fn in os.listdir(path):
fn = os.path.join(path, fn)
if os.path.islink(fn) or os.path.isfile(fn):
os.remove(fn)
else:
shutil.rmtree(fn)
One could just do shutil.rmtree(path) followed by os.mkdir(path), but that fails
if …
[View More]path is a symlink to a directory (disallowed by rmtree).
Is there some other obvious way to do this that I've missed? If not, I'd like to
see something like this added to shutil. What say?
Regards,
Vinay Sajip
[View Less]