Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > passing file from command line startup

Reply
Thread Tools

passing file from command line startup

 
 
Bradley Hintze
Guest
Posts: n/a
 
      08-11-2010
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-11-2010
Bradley Hintze wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
use one line Perl command to add a line at the begin of a file Ting Wang Perl Misc 3 12-13-2005 06:07 PM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
RUN/execute a Command-Line command from an ASP page. Lucas Cowald ASP .Net 4 10-23-2003 11:09 AM



Advertisments