[Tutor] Executing Programs

Alexandre Ratti alex@gabuzomeu.net
Wed, 15 May 2002 10:31:41 +0200


Hi Zachary,


At 01:28 15/05/2002 -0400, you wrote:
>From: "Zachary Cousins" <snoopyboy21@earthlink.net>
>Date: Tue, 14 May 2002 18:32:28 -0500
>Subject: [Tutor] Executing Programs

>how do you execute a saved *.txt file from my hardrive?

Do you mean "how do I open a .txt file in an editor from Python"? Try this 
(on Windows):

thePath = r"c:\temp\toto.txt"
# Import a tool module from the library
import os
# Run a system command from Python
# %s is replace by the file path
os.system("notepad %s" % thePath)

For more information, see:

- Python Tutorial:
http://www.python.org/doc/current/tut/tut.html

- Tools available in the standard library:
http://www.python.org/doc/current/modindex.html


>what do I type?
>I tried 'open' but all it says is type 'file' and if I do it doesn't
>work and gives me an error message.

Or do you mean "how do I read the content of a .txt file from Python"? In 
this case, try this:

thePath = r"c:\temp\toto.txt"
# Get a file object in read mode
fileObj = open(thePath, 'r')
# Read lines from the text file
lineList = fileObj.readlines()
# Print them out one by one
for aLine in lineList:
    print aLine

If I misunderstood your question, please give us more details.


Cheers.

Alexandre