<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Gotcha — backslashes are escape characters</title>
	<atom:link href="http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-are-escape-characters/feed/" rel="self" type="application/rss+xml" />
	<link>http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters/</link>
	<description>Adventures across space and time with the Python programming language</description>
	<lastBuildDate>Sat, 04 May 2013 20:54:54 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: melbourne</title>
		<link>http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters/#comment-797</link>
		<dc:creator><![CDATA[melbourne]]></dc:creator>
		<pubDate>Mon, 11 Jul 2011 10:51:18 +0000</pubDate>
		<guid isPermaLink="false">http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters#comment-797</guid>
		<description><![CDATA[
&lt;blockquote&gt;&lt;big&gt;&lt;pre&gt;&gt;&gt;&gt;s=&#039;\\&#039;
&gt;&gt;&gt;s
&#039;\\&#039;
&gt;&gt;&gt;len(s)
1
&gt;&gt;&gt;print s
&#039;\&#039;
&gt;&gt;&gt;ss=&#039;s=&quot;\\&quot;&#039;
&gt;&gt;&gt;exec(ss)
Traceback (most recent call last):
  File &quot;&quot;, line 1, in 
  File &quot;&quot;, line 1
    s=&quot;\&quot;
        ^
&lt;/pre&gt;&lt;/big&gt;&lt;/blockquote&gt;
The escape character has been correctly escaped, and is now in string ss. However, when we exec(ss), we need to escape the escape character again, to get an escaped escaped escape character:
&lt;blockquote&gt;&lt;code&gt;&gt;&gt;&gt;exec(&#039;s=&quot;\\\\&quot;&#039;)
&gt;&gt;&gt;len(s)
1
&gt;&gt;&gt;print s
\
&lt;/code&gt;&lt;/blockquote&gt;
Generally speaking, a generic text string that you have in your program will probably already be in its correct representation: the exception is raw input characters, which you would normally parse and decode before feeding into exec() anyway. So the safe use of exec() is:
&lt;blockquote&gt;&lt;code&gt;&gt;&gt;&gt;exec(mystring.encode(&quot;string_escape&quot;)&lt;/code&gt;&lt;/blockquote&gt;
or, from my example:
&lt;blockquote&gt;&lt;code&gt;&gt;&gt;&gt;exec(&#039;s=&quot;\\&quot;&#039;.encode(&quot;string_escape&quot;))&lt;/code&gt;&lt;/blockquote&gt;

This allows for the implicit .decode(&quot;string_escape&quot;) that exec() does before executing the string.]]></description>
		<content:encoded><![CDATA[<blockquote><p><big>
<pre>&gt;&gt;&gt;s='\\'
&gt;&gt;&gt;s
'\\'
&gt;&gt;&gt;len(s)
1
&gt;&gt;&gt;print s
'\'
&gt;&gt;&gt;ss='s="\\"'
&gt;&gt;&gt;exec(ss)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1
    s="\"
        ^
</pre>
<p></big></p></blockquote>
<p>The escape character has been correctly escaped, and is now in string ss. However, when we exec(ss), we need to escape the escape character again, to get an escaped escaped escape character:</p>
<blockquote><p><code>&gt;&gt;&gt;exec('s="\\\\"')<br />
&gt;&gt;&gt;len(s)<br />
1<br />
&gt;&gt;&gt;print s<br />
\<br />
</code></p></blockquote>
<p>Generally speaking, a generic text string that you have in your program will probably already be in its correct representation: the exception is raw input characters, which you would normally parse and decode before feeding into exec() anyway. So the safe use of exec() is:</p>
<blockquote><p><code>&gt;&gt;&gt;exec(mystring.encode("string_escape")</code></p></blockquote>
<p>or, from my example:</p>
<blockquote><p><code>&gt;&gt;&gt;exec('s="\\"'.encode("string_escape"))</code></p></blockquote>
<p>This allows for the implicit .decode(&#8220;string_escape&#8221;) that exec() does before executing the string.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Ferg</title>
		<link>http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters/#comment-16</link>
		<dc:creator><![CDATA[Steve Ferg]]></dc:creator>
		<pubDate>Fri, 24 Jul 2009 13:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters#comment-16</guid>
		<description><![CDATA[For a discussion of backslashes and raw string in Windows filenames, see

http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/]]></description>
		<content:encoded><![CDATA[<p>For a discussion of backslashes and raw string in Windows filenames, see</p>
<p><a href="http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/" rel="nofollow">http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters/#comment-14</link>
		<dc:creator><![CDATA[Anonymous]]></dc:creator>
		<pubDate>Fri, 24 Jul 2009 13:24:30 +0000</pubDate>
		<guid isPermaLink="false">http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters#comment-14</guid>
		<description><![CDATA[Try using &#039;raw&#039; strings, e.g., r&#039;C:\somefile.txt&#039; or r&#039;([A-Z]+)\.\1&#039;&lt;br /&gt;&lt;br /&gt;Raw strings treat \&#039;s as \&#039;s, making them well adapted for windows filenames and regular expressions.]]></description>
		<content:encoded><![CDATA[<p>Try using &#39;raw&#39; strings, e.g., r&#39;C:\somefile.txt&#39; or r&#39;([A-Z]+)\.\1&#39;</p>
<p>Raw strings treat \&#39;s as \&#39;s, making them well adapted for windows filenames and regular expressions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: b4hand</title>
		<link>http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters/#comment-3</link>
		<dc:creator><![CDATA[b4hand]]></dc:creator>
		<pubDate>Tue, 23 Dec 2008 17:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%e2%80%94-backslashes-are-escape-characters#comment-3</guid>
		<description><![CDATA[I&#039;m not really sure that I would pose this as a Windows versus Unix background.  I know that several years ago on Windows a recommended practice was to always use forward slashes inside path names in source code.  In fact this is required practice in standard C for #include directives, although many if not all compilers for Windows do allow backslash characters.  So, I would really call this a C language background bias even though obviously C was developed for Unix.  I&#039;m not sure if all of the Unix shells and utilities of the time got backslash escapes from C or vice versa.&lt;br/&gt;&lt;br/&gt;For what it is worth, using forward slashes in path names apparently works in Python just as well.&lt;br/&gt;&lt;br/&gt;In either case, this is still an issue for &lt;em&gt;new&lt;/em&gt; programmers with no prior programming experience, which I believe has always been a focus of concern for Python.]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m not really sure that I would pose this as a Windows versus Unix background.  I know that several years ago on Windows a recommended practice was to always use forward slashes inside path names in source code.  In fact this is required practice in standard C for #include directives, although many if not all compilers for Windows do allow backslash characters.  So, I would really call this a C language background bias even though obviously C was developed for Unix.  I&#8217;m not sure if all of the Unix shells and utilities of the time got backslash escapes from C or vice versa.</p>
<p>For what it is worth, using forward slashes in path names apparently works in Python just as well.</p>
<p>In either case, this is still an issue for <em>new</em> programmers with no prior programming experience, which I believe has always been a focus of concern for Python.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
