I need some advice/help on running my scripts

Discussion in 'Python' started by Sean, Jan 1, 2005.

  1. Sean

    Sean Guest

    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?
     
    Sean, Jan 1, 2005
    #1
    1. Advertising

  2. Sean wrote:
    > 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
     
    Steven Bethard, Jan 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yuyazhang
    Replies:
    14
    Views:
    734
    yuyazhang
    Apr 29, 2006
  2. Jan Danielsson
    Replies:
    8
    Views:
    651
    Mike Meyer
    Jul 22, 2005
  3. Jp Calderone
    Replies:
    0
    Views:
    474
    Jp Calderone
    Jul 21, 2005
  4. davidj411
    Replies:
    0
    Views:
    562
    davidj411
    Jun 27, 2008
  5. Replies:
    13
    Views:
    574
    Anno Siegel
    Sep 10, 2007
Loading...

Share This Page