[Tutor] A question about sys.argv

Dave Angel d at davea.name
Tue Nov 1 15:23:39 CET 2011


On 11/01/2011 10:05 AM, Hugo Arts wrote:
> On Tue, Nov 1, 2011 at 2:48 PM, Jefferson Ragot<jbr5393 at gmail.com>  wrote:
>> In a Vista command prompt if I typed this:
>>
>>          >>>  python  somescript.py  filename
>>
>> Will sys.argv[1] return a valid path or just the filename?
>> If it just returns the filename, is there a simple way to get the path?
>>
> sys.argv contains exactly what you typed in on the command line.
> Nothing more, nothing less. It doesn't care whether what you typed is
> actually a filename or a plain number or some string, it just passes
> the commands as they are.
>
> You can use the os.path.abspath() function on your argument, that will
> probably do what you want.
>
> HTH,
> Hugo

That's very helpful, but let me add a caveat:

The operating system shell may modify that command line before handing 
it to Python.  i don't believe that happens in Vista, however.  Watch 
out for quotes and such, they may be eaten by the c runtime.  And if you 
were on Linux, a filename with wildcards could get expanded into several 
argv[] items.

-- 

DaveA



More information about the Tutor mailing list