Calling external text-files (newb)

Discussion in 'Python' started by Andreas Winkler, Feb 28, 2005.

  1. Hi
    I tried to have python call an external 'Word'-file,
    and the read the whole text into a single string with
    the following code:
    Code:
    source = raw_input('file path')
    
    File = open('source', 'r')
    S = input.read()
    
    print S
    
    But when I try to run it, it raises the following
    error:

    File = open('sorce', 'r')
    IOError: [Errno 2] No such file or directory: 'sorce'

    Is it something I am mimssing, or does python require
    a special form of path?

    Thanks in advance
    Fred\\






    ___________________________________________________________
    Gesendet von Yahoo! Mail - Jetzt mit 250MB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de
    Andreas Winkler, Feb 28, 2005
    #1
    1. Advertising

  2. There are two problems: (1) by quoting 'source', you refer to a string
    literal, not the variable you defined via raw_input; (2) you have not
    defined the variable 'input', so there's no way to call the 'read'
    method on it.

    Try this instead:

    source_path = raw_input('file path: ')
    s = open(source_path).read()
    print s

    The default behavior of 'open' includes 'r' implicitly.

    You can also use the function 'file', which is a synonym for 'open':

    s = file(source).read()

    Be warned that using the same syntax when writing files is dangerous;
    in that case you should create a file object and explicitly close it.

    f = file('a_copy', 'w')
    f.write(s)
    f.close()

    HTH,

    Michael

    --
    Michael D. Hartl, Ph.D.
    CTO, Quark Sports LLC
    http://quarksports.com/
    Michael Hartl, Feb 28, 2005
    #2
    1. Advertising

  3. There are two problems: (1) by quoting 'source', you refer to a string
    literal, not the variable you defined via raw_input; (2) you have not
    defined the variable 'input', so there's no way to call the 'read'
    method on it.

    Try this instead:

    source_path = raw_input('file path: ')
    s = open(source_path).read()
    print s

    The default behavior of 'open' includes 'r' implicitly.

    You can also use the function 'file', which is a synonym for 'open':

    s = file(source).read()

    Be warned that using the same syntax when writing files is dangerous;
    in that case you should create a file object and explicitly close it.

    f = file('a_copy', 'w')
    f.write(s)
    f.close()

    HTH,

    Michael

    --
    Michael D. Hartl, Ph.D.
    CTO, Quark Sports LLC
    http://quarksports.com/
    Michael Hartl, Feb 28, 2005
    #3
  4. Andreas Winkler

    Peter Hansen Guest

    Andreas Winkler wrote:
    > I tried to have python call an external 'Word'-file,
    > and the read the whole text into a single string with
    > the following code:
    >
    Code:
    > source = raw_input('file path')
    > 
    > File = open('source', 'r')
    > S = input.read()
    > 
    > print S
    > 


    Anything inside quotation marks is just a string of
    characters, not a reference to a variable. You are
    asking it to open a file named "source", not to open
    the file whose name is in the variable called source.

    In addition, you are assigning the return value from
    open() to a variable named "File", but then you are
    not using that variable but are trying to read from
    a non-existent object called "input" instead. You'll
    see an error for that if you fix only the first
    problem and try rerunning the code, before you fix
    the second (which you should do, to learn more).

    Use this instead:

    f = open(source, 'r')
    s = f.read()
    print s

    -Peter
    Peter Hansen, Feb 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Tasso

    Re: Calling external HTML files

    William Tasso, Aug 31, 2003, in forum: HTML
    Replies:
    0
    Views:
    362
    William Tasso
    Aug 31, 2003
  2. Raskolnikow
    Replies:
    1
    Views:
    348
    Michiel Salters
    Jul 18, 2003
  3. Pete Wilson
    Replies:
    3
    Views:
    399
    David Harmon
    Apr 3, 2004
  4. Ernesto
    Replies:
    4
    Views:
    279
    Ernesto
    Feb 2, 2006
  5. Mellow Crow
    Replies:
    6
    Views:
    404
    Richard Cornford
    Nov 4, 2005
Loading...

Share This Page