trouble using \ to escape %

L

Lucas Machado

I'm writing a python script that modifies the smb.conf file, and i need
to write the characters '%U' in the file. I tried making a string like
so:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
list[0], list[1])

but i keep getting: "TypeError: not enough arguments for format
string". I've also tried making the string:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
list[0], list[1])

but i get: "ValueError: incomplete format". These errors lead me to
believe that for some reason it is not escaping the '%' character.
There has to be a way to write '%' to a file. Thanks in advance..

Cheers
-Lucas Machado
 
R

Rob Williscroft

Lucas Machado wrote in @l41g2000cwc.googlegroups.com in comp.lang.python:
str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
list[0], list[1])

In a format string use '%%' for a single % char':

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/%%U" % (list[0], list[1],list[0],
list[1])


Rob.
 
M

Maarten Sneep

"Lucas Machado said:
I'm writing a python script that modifies the smb.conf file, and i need
to write the characters '%U' in the file.

print "%s = %%U" % "a" yields a = %U for me.

Maarten
 
D

Dan Bishop

Lucas said:
I'm writing a python script that modifies the smb.conf file, and i need
to write the characters '%U' in the file. I tried making a string like
so:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
list[0], list[1])

but i keep getting: "TypeError: not enough arguments for format
string". I've also tried making the string:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
list[0], list[1])

but i get: "ValueError: incomplete format". These errors lead me to
believe that for some reason it is not escaping the '%' character.
There has to be a way to write '%' to a file. Thanks in advance..
 
J

John Machin

I'm writing a python script that modifies the smb.conf file, and i need
to write the characters '%U' in the file. I tried making a string like
so:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%U" % (list[0], list[1],
list[0], list[1])

but i keep getting: "TypeError: not enough arguments for format
string". I've also tried making the string:

str1 = "[%s%s]\n\tpath = /mnt/samba/%s%s/\%" % (list[0], list[1],
list[0], list[1])

but i get: "ValueError: incomplete format". These errors lead me to
believe that for some reason it is not escaping the '%' character.
There has to be a way to write '%' to a file. Thanks in advance..

Looks like you've already got your answer, but here's some META-help:
what to do when you have a problem and your internet connection is
broken:

1. GUESS. Extrapolation from examples like DLE DLE in networking, $$
in m4 and \\ in *almost* everything might suggest trying %%

2. READ THE DOCS. Don't know what platform you're running on, but one
of what a recent blogger called "hopeless Windows people" [like me]
would do is this:

Start->Programs->Python 2.4->Python manuals, click on the Index tab,
type %, hit Enter ... scrolling down leads to a table of conversion
types, the last of which is the somewhat tautological """% No argument
is converted, results in a "%" character in the result."""

HTH,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,180
Members
47,813
Latest member
RustyGary3

Latest Threads

Top