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>please bear with my cellphones skills ;).</p>

<p>There are different encoding that you can use for making python understand or decode strings. i.e. str("some awkward string",'UTF-16') should be able to decode what you need it to. </p>

<p>To help you understand though, i would use an IDE ( i.e. pycharm) and run the program up to this line ( in debug mode).  then I would try to evalute each step of your line to understand what it return right now.  </p>

python unicode