#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    5
    Rep Power
    0

    python in windows


    Hi all,
    I have python 2.3.4 for windows installed.
    I am working on a project where i'm using GHS Multi-2000 s/w,a simulator for embedded processors.
    The code written has many test cases which have to be executed one by one manually.I have thought of automating these tests using python.I just want to "bypass" keyboard input and instead ,my script should take the inputs from a file "inputs.txt".Can anyone tell if it is possible in windows environment using python 2.3.4?
    I know that it can be easily done in linux.
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    Yep.

    I'm not sure about piping commands in windows (which is how I'd do it under linux/bsd), but you can always do something like this:

    Code:
    #!/usr/bin/python
    import os
    
    try: # try to open your input file
        fhandle = open('inputs.txt', 'r')  
        data = fhandle.readlines()
        fhandle.close()	
    
    except IOError: # abort!
        print "Can't open file inputs.txt for reading!"
        sys.exit(0)
    
    for line in data:
        os.system("ghs.exe %s" % (line)) # runs system command..
    Cheers,
    Simon
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What that work on one platform will in most cases work on others, which is some of the reasons people choose Python over other languages. As long as you stay away from platform spacific modules and commands you'll be fine!

    To open a pipe from Python you need to use one of the os.popen() classes but they are very easy to use if you know how to use files in Python .

    Code:
    #!/usr/bin/env python
    
    import os	
    
    try:
        for line in file('input.txt'): os.popen('runow.exe').write(line)
    except IOError:
        raise SystemExit
    Note: this example, though untested, should be easy to addapt your needs.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    I'm not sure if this is what you mean, but you can redirect input to read from a file on the Windows command line just as you can in UNIX shells:

    Code:
    c:\> python myprog.py < input.txt
    so there may be no need to change the program at all.

    Dave - The Developers' Coach
    Last edited by DevCoach; June 7th, 2004 at 06:07 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Oh, and piping works the same as in UNIX, too:

    Code:
    c:\> prog1 someparams | prog2 moreparams
    feeds the output from prog1 into the input to prog2.

    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo