473,461 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
2 2010
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.