473,378 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Calling external text-files (newb)

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:
Expand|Select|Wrap|Line Numbers
  1. source = raw_input('file path')
  2.  
  3. File = open('source', 'r')
  4. S = input.read()
  5.  
  6. print S
  7.  
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
Jul 18 '05 #1
3 2207
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/

Jul 18 '05 #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/

Jul 18 '05 #3
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:
Expand|Select|Wrap|Line Numbers
  1.  source = raw_input('file path')
  2.  File = open('source', 'r')
  3.  S = input.read()
  4.  print S
  5.  


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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlo Chung | last post by:
Hi, I have a question about how to retrieve the information of the calling instance, such as instance name, in an external C procedure. Thank you, Carlo Chung
11
by: Jake j | last post by:
Yes, this is a simple and straightforward thing to do as long as the .js file doesn't use a function. When I try a script with a function in a js file I can't get it to work (though it works fine...
5
by: claus.hirth | last post by:
If I create the function HELLO in schema S01 as follows, @ CREATE FUNCTION S01.HELLO() RETURNS VARCHAR(32) EXTERNAL NAME 'UDFSRVXYZ!sayHelloWorld' LANGUAGE JAVA PARAMETER STYLE DB2GENERAL NO...
17
by: Bill Grigg | last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1 (.NET). I only mention this because I have never felt comfortable with the process, but nonetheless it did work....
11
by: RichN | last post by:
I am developing a c program in Visual Studio .NET 2003. I also have an Intel(R) Fortran compiler for MVS .NET My fortran sourcecode already existed. I started a new fortran project and chose to...
2
by: korund | last post by:
<html> <head> <title>Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> function init() { alert('Message 1');...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
5
by: Newbie Coder | last post by:
Hello all I have 3 functions in a javacrip file (MyScript.js), which is added to an ASP.NET 2.0 project 1) no right-click 2) no select text (copy...) 3) History.Back()' How do I call...
3
by: John Smith | last post by:
Hi All, I have a script which reads a data file, reads the characters one by one and if a certain character is meet it does something else, at the moment it echos the fact that it meet a certain...
3
by: Shawn T | last post by:
I have an application with a page that has a web user control When I call that page that has this user control, locally (http:// localhost/ApplicationX/default.aspx) and also externally ie...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.