I need some advice/help on running my scripts

S

Sean

For the last couple of months I have been reading and working throught
the examples in Magnus Lie Hetland's Book "Practical Python" This for
all practical purposes is the first computer programming language I
have spent any time at learning, so much of what I have covered in the
book was for the first time.

My problem is that many of the example scripts are run on Linux
machines and I am using Win XP Pro. Here is a specific example of what
is confusing me. If I want to open a file from the dos prompt in some
script do I just write the name of the file I want to open (assuming it
is in the same directory) after the script name?
such as

c:\some_script.py some_text_file.txt

Does piping work the same way in dos as it does on a linux machine?
And last but not least, is there a way to do this all from IDLE?
 
S

Steven Bethard

Sean said:
My problem is that many of the example scripts are run on Linux
machines and I am using Win XP Pro. Here is a specific example of what
is confusing me. If I want to open a file from the dos prompt in some
script do I just write the name of the file I want to open (assuming it
is in the same directory) after the script name?
such as

c:\some_script.py some_text_file.txt

It's unclear to me what you want to do here. If your some_script.py
looks like:

import sys
f = file(sys.argv[1])

then yes, you can call some_script.py as above, and the file will be
readable from the 'f' file object.

Does piping work the same way in dos as it does on a linux machine?

Mostly:

[D:\Steve]$ type test.py
import sys
for i, line in enumerate(sys.stdin):
sys.stdout.write("%i:%s" % (i, line))

[D:\Steve]$ type input.txt
A
B
C
D

[D:\Steve]$ python test.py < input.txt
0:A
1:B
2:C
3:D

[D:\Steve]$ python test.py > output.txt
Z
Y
X
^Z
^Z

[D:\Steve]$ type output.txt
0:Z
1:Y
2:X

[D:\Steve]$ python test.py < input.txt > output.txt

[D:\Steve]$ type output.txt
0:A
1:B
2:C
3:D

[D:\Steve]$ type input.txt | python test.py
0:A
1:B
2:C
3:D


Note however, that you may run into problems if you don't explicitly
call python:

[D:\Steve]$ test.py < input.txt
Traceback (most recent call last):
File "D:\Steve\test.py", line 2, in ?
for i, line in enumerate(sys.stdin):
IOError: [Errno 9] Bad file descriptor
And last but not least, is there a way to do this all from IDLE?

What exactly do you want to do? You can certainly type something like:

f = file('input.txt')

in IDLE to get access to the 'input.txt' file...

Steve
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top