ASCII codec can't encode character u'\u2013'
<p>I have a little Python code in Q_GIS which opens objects. The problem I have is that in the directory there is a character (underscore like character) that can not be encoded. The error is:</p>

<blockquote>
  <p>Traceback (most recent call last):   File "", line 1, in 
  UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in
  position 10: ordinal not in range(128)</p>
</blockquote>

<p>My little code is:</p>

<pre><code>from os import startfile; 
proj = QgsProject.instance(); 
UriFile = str(proj.fileName()); 
img = '[% "pad" %]'; 
Path = str(os.path.dirname(UriFile)); 
startfile(Path+img)
</code></pre>

<p>Because of my little programming skills, I ask you to help me add some code in this little code to overcome the problem.</p>

<p>Thanks for the answers,</p>

<p>I have found the answer in replacing str with unicode in the python code, see the code below.</p>

<p>from os import startfile; proj = QgsProject.instance();UriFile = unicode(proj.fileName()); img = '[% "pad" %]'; Path = unicode(os.path.dirname(UriFile)); startfile(Path+img)</p>

python unicode