[Tutor] Python open of c:\ path Problem
Martin Walsh
mwalsh at mwalsh.org
Sun Aug 24 04:26:22 CEST 2008
Alan Gauld wrote:
> "Wayne Watson" <sierra_mtnview at sbcglobal.net> wrote
>
>> BTW, how does one continue a long statement
>> that has, say, a long path to a file?
>
> You can create a long string by adding the shorter string
> elements :
>
> f = open(
> "a:/very/long/path/name/that/needs/a/whole/line/to./itself.py",
> "w")
>
> becomes
>
> f = open("a:/very/long/path/name/" +
> "that/needs/a/whole/" +
> "line/to./itself.py","w")
>
>
> or by using a line continuation character.
>
> f = open("a:/very/long/path/name/" \
> "that/needs/a/whole/" \
> "line/to./itself.py", "w")
>
or just
f = open("a:/very/long/path/name/"
"that/needs/a/whole/"
"line/to./itself.py", "w")
because of the enclosing parentheses. This works for expressions in
brackets or braces as well -- any enclosing form.
PEP8[1] suggests this as the preferred style (adding that "sometimes
using a backslash looks better"), which is a little surprising to me,
but not in a bad way. Then again, perhaps I've misunderstood -- which
would *not* be surprising.
# for those who aren't familiar
[1] http://www.python.org/dev/peps/pep-0008/
HTH,
Marty
More information about the Tutor
mailing list