taking qoutes in arguments
bignose+hates-spam at benfinney.id.au
Tue May 16 01:05:09 CEST 2006
"Lee Caine" <kano.priv at gmail.com> writes:
> Im new to python and am in need of a little help....
Possibly also new to command-line shells?
> Im attempting to write a program that finds and replaces text in all
> files in a given directory.
> example of running the program with arguments
> >python far.py /home/lee/Documents/ find replace
> I have managed to get it all working apart from one thing, if the
> find or replace arguments
> contain quotes e.g( content="somat" ), it doesnt work,
When asking questions about behaviour, please describe the behaviour
and how it differs from your expectations. "It doesn't work" is
utterly useless as a description.
> so I print the string and it shows as content=somat, without the
This is because your command-line shell (which one are you using?)
needs to do its own work on the command you type before Python is even
For most command shells, the arguments for a command are separated by
spaces, and double-quotes can be used to tell the shell that spaces
are part of an argument.
$ ls foo bar # lists two files, "foo" and "bar"
$ ls "foo bar" # lists one file, "foo bar"
The shell parses the command line and determines what the arguments
are, stripping the quotes after figuring out where the arguments begin
and end. The command receives each argument in an array element,
without the quotes.
Because of this, to get actual quote characters, you need to tell the
shell which quote characters are *not* to be interpreted this way,
usually with an escape character like the backslash.
> So i tryed this...
> >python far.py /home/lee/Documents content=\"somat\" content=\"somat else\"
Yep, just like that.
> and this works, but I would like a way of not having to do this when
> i run the program
Your shell may also understand single-quoted arguments, which remove a
lot of the special meanings for characters (like double quote
$ ls '"foo bar"' # lists one file, '"foo bar"'
Read the manual for your command shell, to understand better what
characters are special and how to get those characters literally in an
Hope that helps.
\ "My doctor told me to stop having intimate dinners for four. |
`\ Unless there are three other people." -- Orson Welles |
More information about the Python-list