how to run a python file with another file as an argument!

Discussion in 'Python' started by se7en, Feb 5, 2004.

  1. se7en

    se7en Guest

    okay,

    i cant seem to run a file with another file as an argument.

    e.g I want to send the file "x.met" as an argument when running the file "y.py"

    thanx
    -python newbie-
    se7en, Feb 5, 2004
    #1
    1. Advertising

  2. "se7en" <> wrote in message
    news:

    > okay,
    >
    > i cant seem to run a file with another file as an argument.
    >
    > e.g I want to send the file "x.met" as an argument when running the file "y.py"
    >
    > thanx
    > -python newbie-



    Maybe you are looking for 'sys.argv' list?


    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
    Carmine Noviello, Feb 5, 2004
    #2
    1. Advertising

  3. se7en

    Dave Kuhlman Guest

    se7en wrote:

    > okay,
    >
    > i cant seem to run a file with another file as an argument.
    >
    > e.g I want to send the file "x.met" as an argument when running
    > the file "y.py"
    >


    You will want to read about file objects in Python. See:

    http://www.python.org/doc/current/lib/bltin-file-objects.html

    Pass the file *name* on the command line.

    Try something like the following:

    ===========================================================

    #!/usr/bin/env python

    import sys

    def main():
    # Get the arguments from the command line, except the first one.
    args = sys.argv[1:]
    if len(args) == 1:
    # Open the file for read only.
    infile = file(args[0], 'r')
    content = infile.read()
    infile.close()
    # Do something with the contents of the file.
    o
    o
    o
    else:
    print 'usage: y.py <inputfile>'
    sys.exit(-1)

    if __name__ == '__main__':
    main()

    ===========================================================

    Here is an alternative that reads one line at a time:

    ===========================================================

    #!/usr/bin/env python

    import sys

    def main():
    # Get the arguments from the command line, except the first one.
    args = sys.argv[1:]
    if len(args) == 1:
    # Open the file for read only.
    infile = file(args[0], 'r')
    # Iterate over the lines in the file.
    # Note that a file object obeys the iterator protocol.
    for line in infile:
    do_process(line)
    infile.close()
    else:
    print 'usage: y.py <inputfile>'
    sys.exit(-1)

    if __name__ == '__main__':
    main()

    ===========================================================

    Hope this helps.

    Dave

    --
    http://www.rexx.com/~dkuhlman
    Dave Kuhlman, Feb 5, 2004
    #3
    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. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    410
  2. se7en
    Replies:
    2
    Views:
    302
    Nuff Said
    Feb 6, 2004
  3. Ben Kial
    Replies:
    1
    Views:
    643
    Eric Enright
    Nov 15, 2004
  4. Seid Mohammed
    Replies:
    0
    Views:
    279
    Seid Mohammed
    Nov 21, 2008
  5. Reckoner
    Replies:
    11
    Views:
    696
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page