read file problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ernesto

    #1

    read file problem

    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\\my File.txt
    fileHandle = open(filePath, 'r').read();
    print fileHandle.read ()

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

  • Fuzzyman

    #2
    Re: read file problem


    Ernesto wrote:[color=blue]
    > 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\\my File.txt
    > fileHandle = open(filePath, 'r').read();
    > print fileHandle.read ()
    >
    > # This prints nothing when it should print the above values.... Thanks
    > for the help[/color]

    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

    Comment

    • Rene Pijlman

      #3
      Re: read file problem

      Ernesto:[color=blue]
      >fileHandle = open(filePath, 'r').read();[/color]
      ^^^^^^^^
      Remove this:

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

      --
      René Pijlman

      Comment

      • Schüle Daniel

        #4
        Re: read file problem


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

        nums = file(r"C:\folde r\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

        Comment

        • Ernesto

          #5
          Re: read file problem

          Thanks to all.

          Comment

          Working...