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? 6 1591
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.
On Jan 21, 9:08 pm, Mel <mwil...@the-wire.comwrote:
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.
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
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
En Tue, 22 Jan 2008 02:03:10 -0200, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribi:
"J. Peng" <jp***@block.duxieweb.comwrites:
> 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
Gabriel Genellina 写道:
En Tue, 22 Jan 2008 02:03:10 -0200, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribió:
>"J. Peng" <jp***@block.duxieweb.comwrites:
>> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sebastian Krause |
last post by:
Hello,
I tried to read in some large ascii files (200MB-2GB) in Python using
scipy.io.read_array, but it did not work as I expected. The whole idea
was to find a fast Python routine to read in...
|
by: Andrea Bauer |
last post by:
Hallo,
wie kann ich so eine Datei unter .Net schreiben C++ oder C#.
Bitte mit Funktionsaufrufen. Vielen Dank.
Gre Andrea
<Product>
<ProgramNumber>2</ProgramNumber>
|
by: who be dat? |
last post by:
I feel stupid for asking this but I can't figure this out. I've got some
text files I want my website to read. The text files are located in a
subdirectory of my application. Physically, the...
|
by: Zephyre |
last post by:
I have some UTF-8 text files written in Chinese to be read. Now the
only method that I know to read text from it is to use fopen()
function. Thus, I must read the contents byte by byte, change the...
|
by: nicolasg |
last post by:
Hi,
I'm trying to open a file (any file) in binary mode and save it inside
a new text file.
After that I want to read the source from the text file and save it
back to the disk with its...
|
by: Tracks |
last post by:
I have old legacy code from vb5 where data was written to a file with a
variant declaration (this was actually a coding error?)...
in vb5 the code was:
Dim thisdata as integer
Dim thatdata...
|
by: =?Utf-8?B?VGhvbWFzWg==?= |
last post by:
Hi,
Is it possible to read a file in reverse and only get the last 100 bytes in
the file without reading the whole file from the begining? I have to get info
from files that are in the last 100...
|
by: John Nagle |
last post by:
I'm looking for something that can read .MDB files, the format
Microsoft Access uses, from Python. I need to do this on
Linux, without using Microsoft tools. I just need to read
the files once,...
|
by: Kevin Ar18 |
last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230
I have a zip file that is several GB in size, and one of the files inside of it...
|
by: maneshborase |
last post by:
Hi friends,
I am facing one serious problem in my application.
I am trying to open dicom image file (.dcm) has size around 400 MB.
But I am getting and unhandy exceptions,
Some time,
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |