User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 429,868 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,427 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums

Starting Python

Join Date: Jul 2005
Location: France
Posts: 1,019
Reputation: bumsfeld is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 45
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Veteran Poster

Solution Re: Starting Python

  #50  
Feb 25th, 2006
Code sample shows how to run a program and specified file(s) from commandline.
# using commandline to run a program and files

def process_datafile(filename):
    """load specified data file and process data"""
    # code to read file data
    # code to process data and show or return result
    print "Test only, file =", filename   # testing

if __name__ == '__main__':
    import sys

    # run the progam from the commandline and specify the data file(s) to be loaded
    # something like   myProgram data1.dat data2.dat
    if len(sys.argv) > 1:
        # sys.argv is a list eg. ['C:/Python24/Atest/myProgram.py', 'data1.dat', 'data2.dat']
        # sys.argv[0] is program filename, slice it off with sys.argv[1:]
        # using for loop, more than one datafile can be processed
        for filename in sys.argv[1:]:
            process_datafile(filename)
    else:
        print "usage myProgram datafile1 [datafile2 ...]"
Reply With Quote  
All times are GMT -4. The time now is 7:41 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC