Steve Holden <[EMAIL PROTECTED]> writes:
> >>>[line.rstrip() for line in open('C:\\switches.txt')]
> > How would I manually close a file that's been opened this way? Or is
> > it not possible in this case? Is it necessary?
>
> In CPython it's not strictly necessary to close the file, but other
> im
Steve Holden wrote:
> It's not possible to perform an explicit close if, as in this case, you
> don't have an explicit reference to the file object.
>
> In CPython it's not strictly necessary to close the file, but other
> implementations don't guarantee that a file will be closed after the
>
John Salerno wrote:
> Paul Rubin wrote:
>
>>John Salerno <[EMAIL PROTECTED]> writes:
>>
>>>Interesting. So I would say:
>>>
>>>[line.rstrip() for line in open('C:\\switches.txt')]
>
>
>
> How would I manually close a file that's been opened this way? Or is it
> not possible in this case? Is it
Paul Rubin wrote:
> John Salerno <[EMAIL PROTECTED]> writes:
>> Interesting. So I would say:
>>
>> [line.rstrip() for line in open('C:\\switches.txt')]
How would I manually close a file that's been opened this way? Or is it
not possible in this case? Is it necessary?
--
http://mail.python.org/m
Steven D'Aprano wrote:
> On Fri, 03 Mar 2006 16:57:10 +, John Salerno wrote:
>
>> Steven D'Aprano wrote:
>>
>>> The important term there is BINARY, not large. Many problems *reading*
>>> (not opening) binary files will go away if you use 'rb', regardless of
>>> whether they are small, medium o
On Fri, 03 Mar 2006 16:57:10 +, John Salerno wrote:
> Steven D'Aprano wrote:
>
>> The important term there is BINARY, not large. Many problems *reading*
>> (not opening) binary files will go away if you use 'rb', regardless of
>> whether they are small, medium or large.
>
> Is 'b' the proper
Steven D'Aprano wrote:
> The important term there is BINARY, not large. Many problems *reading*
> (not opening) binary files will go away if you use 'rb', regardless of
> whether they are small, medium or large.
Is 'b' the proper parameter to use when you want to read/write a binary
file? I was
On Fri, 03 Mar 2006 01:03:38 -0800, P Boy wrote:
> I had some issues while ago trying to open a large binary file.
The important term there is BINARY, not large. Many problems *reading*
(not opening) binary files will go away if you use 'rb', regardless of
whether they are small, medium or large.
I had some issues while ago trying to open a large binary file.
Anyway, from file() man page:
If mode is omitted, it defaults to 'r'. When opening a binary file, you
should append 'b' to the mode value for improved portability. (It's
useful even on systems which don't treat binary and text files
P Boy wrote:
> BTW, if the file is huge, one may want to consider using
> open('c:\\switches.txt', 'rb') instead.
Why?
--
http://mail.python.org/mailman/listinfo/python-list
One liners are cool. Personally however, I would not promote one liners
in Python. Python code is meant to be read. Cryptic coding is in perl's
world.
Code below is intuitive and almost a three year old would understand.
for line in open('C:\\switches.txt'):
print line.rstrip()
BTW, if t
John Salerno wrote:
> You can probably tell what I'm doing. Read a list of lines from a file,
> and then I want to slice off the '\n' character from each line.
If you are not concerned about memory consumption there is also
open(filename).read().splitlines()
Peter
--
http://mail.python.org/mai
Ben Cartwright wrote:
> No, since the line variable is unused. This:
>
> i = 0
> for line in switches:
> line = switches[i][:-1]
> i += 1
>
> Would be better written as:
>
> for i in range(len(switches)):
> switches[i] = switches[i][:-1]
This is better, IMHO:
for i, sw
John Salerno <[EMAIL PROTECTED]> writes:
> Interesting. So I would say:
>
> [line.rstrip() for line in open('C:\\switches.txt')]
Yes, you could do that. Note that it builds up an in-memory list of
all those lines, instead of processing the file one line at a time.
If the file is very large, that
John Salerno wrote:
> Paul Rubin wrote:
>
>> The preferred way to remove the newline is more like:
>>for line in open('C:\\switches.txt'):
>> print line.rstrip()
>
> Interesting. So I would say:
>
> [line.rstrip() for line in open('C:\\switches.txt')]
That seems to work. And on a relat
Paul Rubin wrote:
> The preferred way to remove the newline is more like:
>for line in open('C:\\switches.txt'):
> print line.rstrip()
Interesting. So I would say:
[line.rstrip() for line in open('C:\\switches.txt')]
--
http://mail.python.org/mailman/listinfo/python-list
John Salerno <[EMAIL PROTECTED]> writes:
> > print [line[:-1] for line in open('C:\\switches.txt')]
>
> Hmm, I just realized in my original code that I didn't escape the
> backslash. Why did it still work properly?
The character the backslash isn't special: \s doesn't get into
a code like \n, s
Ben Cartwright wrote:
> print [line[:-1] for line in open('C:\\switches.txt')]
Hmm, I just realized in my original code that I didn't escape the
backslash. Why did it still work properly?
By the way, this whole 'one line' thing has blown me away. I wasn't
thinking about list comprehensions w
Ben Cartwright wrote:
> Actually, it creates a new string instance with the \n character
> removed, then discards it. The original switches[0] string hasn't
> changed.
> Yes. You are repeated assigning a new string instance to "line", which
> is then never referenced again.
Ah, thank you!
>
John Salerno wrote:
> You can probably tell what I'm doing. Read a list of lines from a file,
> and then I want to slice off the '\n' character from each line. But
> after this code runs, the \n is still there. I thought it might have
> something to do with the fact that strings are immutable, but
Here's the code I wrote:
file = open('C:\switches.txt', 'r')
switches = file.readlines()
i = 0
for line in switches:
line = switches[i][:-1]
i += 1
print switches
You can probably tell what I'm doing. Read a list of lines from a file,
and then I want to slice off the '
21 matches
Mail list logo