Calling external text-files (newb)

Peter Hansen peter at engcorp.com
Mon Feb 28 17:15:58 EST 2005


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
> [/CODE]

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



More information about the Python-list mailing list