By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

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

P: n/a
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-
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"se7en" <bi**********@yahoo.com> wrote in message
news:ea**************************@posting.google.c om
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
Jul 18 '05 #2

P: n/a
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/li...e-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
dk******@rexx.com
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.