[Tutor] Python Command Line Args (input) equivalent to Perl

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Oct 28 22:11:35 CEST 2004



On Thu, 28 Oct 2004, Edward WIJAYA wrote:

> I am new to Python, and I like to learn more about it. Since I am used
> to Perl before, I would like to know what is Python equivalent of Perl
> code below:
>
>
> $filename = $ARGV[0];
> open (FILE,"$filename") || die "Can't Open $filename: $!\n";
> while<FILE>{ #dealing with it per-lines
>    #process something here
> }


Hi Edward,


Several variables that Perl makes globally available have equivalents in
Python.  Python tries to keep fewer global variables at toplevel:  they
equivalents will often live in separate modules.

Perl's '@ARGV', for example, has an equivalent in Python's 'sys.argv'
list.  Perl's '%ENV' corresponds with Python's 'os.environ' dictionary.


For example, from the interactive interpreter, we can type:

###
>>> import os
>>> os.environ['HOME']
'/home/dyoo'
###

If you have more questions, please feel free to ask!



More information about the Tutor mailing list