Calling external text-files (newb)

A

Andreas Winkler

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
 
M

Michael Hartl

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
 
M

Michael Hartl

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
 
P

Peter Hansen

Andreas said:
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
 

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
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top