passing file from command line startup

B

Bradley Hintze

Hi all,

Is there a way that I can startup my script and pass it a file? For example:

~$ python myscript.py mytext.txt

and then access mytext.txt in myscript.py?

As a long shot, for myscript.py I tried

def __init__(fle):
print fle

expecting the full path to mytext.txt to be printed but that didn't work.

Obviously I've never done this. I hope the above makes sense. any help
will be greatly appreciated.

Thanks,
--
Bradley J. Hintze
Graduate Student
Duke University
School of Medicine
801-712-8799
 
P

Peter Otten

Bradley said:
Hi all,

Is there a way that I can startup my script and pass it a file? For
example:

~$ python myscript.py mytext.txt

and then access mytext.txt in myscript.py?

As a long shot, for myscript.py I tried

def __init__(fle):
print fle

expecting the full path to mytext.txt to be printed but that didn't work.

Obviously I've never done this. I hope the above makes sense. any help
will be greatly appreciated.

Thanks,

You are looking for sys.argv:

$ cat tmp.py
import sys
print sys.argv
$ python tmp.py one two 'many arguments'
['tmp.py', 'one', 'two', 'many arguments']

Around that simple mechanism fancier libraries have been built:

http://docs.python.org/library/argparse.html

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top