read file problem

E

Ernesto

I'm just want to read in the contents of a (text) file. The text file
is filled with semiColon delimited floating point strings...

0.456;1.265;99.742;...

For some reason, I can't get the contents back when I call file.read()
Here's my code.

filePath = "C:\\folder\\myFile.txt
fileHandle = open(filePath, 'r').read();
print fileHandle.read()

# This prints nothing when it should print the above values.... Thanks
for the help
 
F

Fuzzyman

Ernesto said:
I'm just want to read in the contents of a (text) file. The text file
is filled with semiColon delimited floating point strings...

0.456;1.265;99.742;...

For some reason, I can't get the contents back when I call file.read()
Here's my code.

filePath = "C:\\folder\\myFile.txt
fileHandle = open(filePath, 'r').read();
print fileHandle.read()

# This prints nothing when it should print the above values.... Thanks
for the help

Try adding a 'print fileHandle' in there...

That line should be :

fileHandle = open(filePath, 'r')

The '.read()' part returns the full file... so further reads have no
effect.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
R

Rene Pijlman

Ernesto:
fileHandle = open(filePath, 'r').read();
^^^^^^^^
Remove this:

open() returns the filehandle, open().read() returns the data read from
the filehandle.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

if you want the numbers you can combine it into one-liner

nums = file(r"C:\folder\myFile.txt").read().split(";")

the numbers are in string representation in the list
you can no do

nums = [float(num) for num in nums]

Regards, Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top