passing arguments with ampersands

O.R.Senthil Kumaran orsenthil at users.sourceforge.net
Sun Aug 19 23:40:41 EDT 2007


Sorry for ignoring the thread.
> the problem is that when i associate a file extension to my application and
> i open it, the filename is not quoted before passed as an arguement. i dont

If it is not quoted by default, you can quote it explicit. If this be the case where the filename is obtained from the OS. 

newname = '"' + filename + '"'

If you are passing the filename, then just pass it as "filename".

> args = None
> > if len(sys.argv) > 1:
> >     args = sys.argv[1:]
> >
> print args

Yes, '&' character is being interpreted by the SHELL command interpreter and python is not receiving it. Thats why it is failing. You need to escape the special character or quote the entire arg.

> how can i make the params get quoted before send as arguments?

How are you getting the params? 
1) Like I said, if you are passing it, escape the special characters or quote the entire thing.
2) If you getting it from a python function like os.listdir(), I assume it should be readily usable.

[ors at goofy ~]$ cat 1.py
import sys
arg = None
if len(sys.argv) > 1:
    args = sys.argv[1:]

print args
[ors at goofy ~]$ python 1.py something anotherthing \& yetanother
['something', 'anotherthing', '&', 'yetanother']
[ors at goofy ~]$ python 1.py "something anotherthing & yetanother"
['something anotherthing & yetanother']
[ors at goofy ~]$





If filenames are obtained as os.listdir(), then quote each other
> 
> 
> 
> >
> > 2007/8/18, O.R.Senthil Kumaran <orsenthil at users.sourceforge.net>:
> 
> > Quote:D:\ftp\Music\Mixes & Compilations\Above & Beyond - Essential
> > > Mix\001_Essential_Mix_2004-06-06_-_Above_and_Beyond.txt
> >
> > > when i put 'print sys.argv[1:]' at the very beginning of the script, all
> > i
> > > get is this:
> > >
> > > Quote:['D:\\ftp\\Music\\Mixes']
> > >
> > Can you share the portion of the code?
> > Are you quoting the argument that you are passing?
> > for e.g:
> >
> > #cat 1.py
> > import sys
> > print sys.argv[1]
> >
> > #python 1.py "I am able to print the spaces and & characters"
> > I am able to print the spaces and & characters
> >
> > --
> > O.R.Senthil Kumaran
> > http://uthcode.sarovar.org
> >

-- 
O.R.Senthil Kumaran
http://uthcode.sarovar.org



More information about the Python-list mailing list