sorry but i do not have any prior knowledge of python (was asked to do this though)
so can explain what you have written?
and also can provide the whole program for me, if possible? :x
thank you so much
Main Topics
Browse All Topicshi... can anybody give me some sample codes for reading a text file line by line?
I need to make a program to read the content of the file and to extract info in certain lines
thank u
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
it first checks if the file exists at all (os.path.exists).
afterwards it opens the file readonly.
now we iterate over all lines (input)
finally it prints the line with removed trailing whitespaces.
actually, that the whole program.
put the following two lines into the head and you are ready to go:
#!/usr/bin/env python
import os
You should always close the open file after the processing. In other words, there should be input.close() at the end of the accepted solution. When using try/except, you should also close the file. From Python 2.5, you can also use the new "with" construct. See the standard documentation of 3.9 File Objects http://docs.python.org/lib
Citation:
close( )
Close the file. A closed file cannot be read or written any more. Any operation which requires that the file be open will raise a ValueError after the file has been closed. Calling close() more than once is allowed.
As of Python 2.5, you can avoid having to call this method explicitly if you use the with statement. For example, the following code will automatically close f when the with block is exited:
from __future__ import with_statement
with open("hello.txt") as f:
for line in f:
print line
In older versions of Python, you would have needed to do this to get the same effect:
f = open("hello.txt")
try:
for line in f:
print line
finally:
f.close()
pepr,
I am very, very new at python (and I am struggling with it, "they" said it was like Pascal, but I have yet to see any similarity).
I was curious about the future statement. At first it seemed odd, like declare something that will exist in the future" but obviously this new "with" statement already exists. Why the "future" declaration? Is it just not "classified" yet. Will this declaration have to be changed in the future, once it is, or what?
Thanks for the discussion!
I found this, it explains it fairly well:
http://docs.python.org/lib
The __future__ is a module that--when used explicitly--allows you to use the features that are not the part of the "mainstream python kernel" (sorry for my English). Think about it as about preview or preliminary versions of the code.
See the end of the "7.5 The with statement" (http://docs.python.org/re
"Note: In Python 2.5, the with statement is only allowed when the with_statement feature
has been enabled. It will always be enabled in Python 2.6. This __future__ import statement can
be used to enable the feature:
from __future__ import with_statement"
The __future__ is a Python module written in Python. You can look inside your .../python/Lib/__future__.
nomoslogos: I knew Pascal very well in the past (and I liked it; I am not using it now and I forgot details). However, Python is not similar to Pascal (in my opinion). Python requires a kind of "mind shift". But if you are flexible, do learn Python. Warning, the above Python "with" is completely unrelated to Pascal "with".
I would choose Python if I were forced to choose a single language. However, a single language is not good to solve all problems of mankind. My favourite combination is Python and C++. But there also is IronPython for .NET, Jython for Java. It is likely that Python will fit also with your brain (as Guido van Rossum likes as answer to "why this feature, why this way...").
Business Accounts
Answer for Membership
by: pYraniaPosted on 2004-07-21 at 00:34:42ID: 11600234
if os.path.exists("file"):
input = open("file", 'r')
for line in input:
print line.rstrip()