[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

read files

J. Peng

1/22/2008 3:01:00 AM

first I know this is the correct method to read and print a file:

fd = open("/etc/sysctl.conf")
done=0
while not done:
line = fd.readline()
if line == '':
done = 1
else:
print line,

fd.close()


I dont like that flag of "done",then I tried to re-write it as:

fd = open("/etc/sysctl.conf")
while line = fd.readline():
print line,
fd.close()


this can't work.why?
7 Answers

Mel

1/22/2008 3:08:00 AM

0

J. Peng wrote:
> first I know this is the correct method to read and print a file:
>
> fd = open("/etc/sysctl.conf")
> done=0
> while not done:
> line = fd.readline()
> if line == '':
> done = 1
> else:
> print line,
>
> fd.close()
>
>
> I dont like that flag of "done",then I tried to re-write it as:
>
> fd = open("/etc/sysctl.conf")
> while line = fd.readline():
> print line,
> fd.close()
>
>
> this can't work.why?

Formally, because line = fd.readline() is a statement, not an
expression, and it can't be put into an if statement like that.

The most idiomatic way is probably


fd = open ("/etc/sysctl.conf")
for line in fd:
print line



Mel.

Benjamin

1/22/2008 3:11:00 AM

0

On Jan 21, 9:08 pm, Mel <mwil...@the-wire.com> wrote:
> J. Peng wrote:
> > first I know this is the correct method to read and print a file:
>
> > fd = open("/etc/sysctl.conf")
> > done=0
> > while not done:
> > line = fd.readline()
> > if line == '':
> > done = 1
> > else:
> > print line,
>
> > fd.close()
>
> > I dont like that flag of "done",then I tried to re-write it as:
>
> > fd = open("/etc/sysctl.conf")
> > while line = fd.readline():
> > print line,
> > fd.close()
>
> > this can't work.why?
>
> Formally, because line = fd.readline() is a statement, not an
> expression, and it can't be put into an if statement like that.
>
> The most idiomatic way is probably
>
> fd = open ("/etc/sysctl.conf")
> for line in fd:
> print line
more idiomatic in Python 2.5+:

from __future__ import with_statement
with open("/my/file.conf") as fd:
for line in fd:
print line
>
> Mel.

Steven D'Aprano

1/22/2008 3:51:00 AM

0

On Tue, 22 Jan 2008 11:00:53 +0800, J. Peng wrote:

> first I know this is the correct method to read and print a file:
>
> fd = open("/etc/sysctl.conf")
> done=0
> while not done:
> line = fd.readline()
> if line == '':
> done = 1
> else:
> print line,
>
> fd.close()


The evolution of a Python program.

# Solution 2:

fd = open("/etc/sysctl.conf")
done = False
while not done:
line = fd.readline()
if line:
print line,
else:
done = True
fd.close()



# Solution 3:

fd = open("/etc/sysctl.conf")
while True:
line = fd.readline()
if line:
print line,
else:
break
fd.close()


# Solution 4:

fd = open("/etc/sysctl.conf")
lines = fd.readlines()
for line in lines:
print line,
fd.close()


# Solution 5:

fd = open("/etc/sysctl.conf", "r")
for line in fd.readlines():
print line,
fd.close()


# Solution 6:

for line in open("/etc/sysctl.conf").readlines():
print line,
# garbage collector will close the file (eventually)


# Solution 7:

fd = open("/etc/sysctl.conf", "r")
line = fd.readline()
while line:
print line,
line = fd.readline()
fd.close()


# Solution 8:

fd = open("/etc/sysctl.conf", "r")
for line in fd:
print line,
fd.close()


# Solution 9:

for line in open("/etc/sysctl.conf"):
print line,


# Solution 10:
# (the paranoid developer)

try:
fd = open("/etc/sysctl.conf", "r")
except IOError, e:
log_error(e) # defined elsewhere
print "Can't open file, please try another."
else:
try:
for line in fd:
print line,
except Exception, e:
log_error(e)
print "Reading file was interrupted by an unexpected error."
try:
fd.close()
except IOError, e:
# Can't close a file??? That's BAD news.
log_error(e)
raise e




--
Steven

Paul Rubin

1/22/2008 4:03:00 AM

0

"J. Peng" <jpeng@block.duxieweb.com> writes:
> print line,

Do you really want all the lines crunched together like that? If not,
leave off the comma.

> I dont like that flag of "done",then I tried to re-write it as:
>
> fd = open("/etc/sysctl.conf")
> while line = fd.readline():
> print line,
> fd.close()

Python doesn't have assignment expressions like C. Try this:

fd = open("/etc/sysctl.conf")
for line in fd:
print line,
fd.close()

this uses the fact that looping through an file descriptor iterates
through it line by line.

J. Peng

1/22/2008 4:24:00 AM

0

Thank you. That gave so much solutions.
And thanks all.


Steven D'Aprano å??é?:
> On Tue, 22 Jan 2008 11:00:53 +0800, J. Peng wrote:
>
>> first I know this is the correct method to read and print a file:
>>
>> fd = open("/etc/sysctl.conf")
>> done=0
>> while not done:
>> line = fd.readline()
>> if line == '':
>> done = 1
>> else:
>> print line,
>>
>> fd.close()
>
>
> The evolution of a Python program.
>
> # Solution 2:
>
> fd = open("/etc/sysctl.conf")
> done = False
> while not done:
> line = fd.readline()
> if line:
> print line,
> else:
> done = True
> fd.close()
>
>
>
> # Solution 3:
>
> fd = open("/etc/sysctl.conf")
> while True:
> line = fd.readline()
> if line:
> print line,
> else:
> break
> fd.close()
>
>
> # Solution 4:
>
> fd = open("/etc/sysctl.conf")
> lines = fd.readlines()
> for line in lines:
> print line,
> fd.close()
>
>
> # Solution 5:
>
> fd = open("/etc/sysctl.conf", "r")
> for line in fd.readlines():
> print line,
> fd.close()
>
>
> # Solution 6:
>
> for line in open("/etc/sysctl.conf").readlines():
> print line,
> # garbage collector will close the file (eventually)
>
>
> # Solution 7:
>
> fd = open("/etc/sysctl.conf", "r")
> line = fd.readline()
> while line:
> print line,
> line = fd.readline()
> fd.close()
>
>
> # Solution 8:
>
> fd = open("/etc/sysctl.conf", "r")
> for line in fd:
> print line,
> fd.close()
>
>
> # Solution 9:
>
> for line in open("/etc/sysctl.conf"):
> print line,
>
>
> # Solution 10:
> # (the paranoid developer)
>
> try:
> fd = open("/etc/sysctl.conf", "r")
> except IOError, e:
> log_error(e) # defined elsewhere
> print "Can't open file, please try another."
> else:
> try:
> for line in fd:
> print line,
> except Exception, e:
> log_error(e)
> print "Reading file was interrupted by an unexpected error."
> try:
> fd.close()
> except IOError, e:
> # Can't close a file??? That's BAD news.
> log_error(e)
> raise e
>
>
>
>

Gabriel Genellina

1/22/2008 5:25:00 AM

0

En Tue, 22 Jan 2008 02:03:10 -0200, Paul Rubin
<"http://phr.cx"@NOSPAM.i... escribió:

> "J. Peng" <jpeng@block.duxieweb.com> writes:
>> print line,
>
> Do you really want all the lines crunched together like that? If not,
> leave off the comma.

Remember that Python *keeps* the end-of-line charater at the end of each
line; if you leave out the comma, you'll print them doubly spaced.

--
Gabriel Genellina

J. Peng

1/22/2008 5:38:00 AM

0

Gabriel Genellina å??é?:
> En Tue, 22 Jan 2008 02:03:10 -0200, Paul Rubin
> <"http://phr.cx"@NOSPAM.i... escribió:
>
>> "J. Peng" <jpeng@block.duxieweb.com> writes:
>>> print line,
>> Do you really want all the lines crunched together like that? If not,
>> leave off the comma.
>
> Remember that Python *keeps* the end-of-line charater at the end of each
> line; if you leave out the comma, you'll print them doubly spaced.
>

Most languages (AFAIK) keep the newline character at the end of lines
when reading from a file.But python's difference is its 'print' built-in
function add a newline automatically for you. So without that ',' we
will get double newlines.