Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a very basic Python script I wrote mostly for learning purposes.

It opens a terminal in the current folder. However, I can't get it to work in folders with accented characters in the URI (e.g.: /home/pablo/Vídeos or /home/pablo/Área de Trabalho), because it looks like Nautilus URIs are encoded to those %{number} values. Is there a way to convert these URIs to normalized URIs without having to translate every possible accented value by hand?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

It's not very clear from you question, but it looks like your are searching for urllib. Note that urllib expects 8 bit strings, so you will need to do some fancy decode/encode. Welcome to python2 somewhat weird unicode support, pythn3 is way better.

# -*- coding: utf-8 -*-
import urllib

url=u'/home/javier/Área'

url2 = url.encode('utf-8') # urllib expects 8-bit string
url3 = urllib.quote_plus(url2)
print url3  # >> %2Fhome%2Fjavier%2F%C3%81rea
url4 = urllib.unquote(url3) # It will return a 8-bit string
print url4  # >> /home/javier/Área
print url4.decode('utf-8')

You can use urllib.qoute instead of *urllib.quote_plus*, it will not quote spaces, + (pluses) and / (slashes). Both functions accept a string as a second parameter, they will keep any character in that string as is (I mean it will not quote it) in the output string. Note that the default value for safe, the name of the second parameter, is '/' for quote and '' for quote_plus. If you don't include a slash in your second parameter quote will change it.

urllib.quote_plus('a/a','/')  # 'a/a'
share|improve this answer
    
Thank you, Javier! However, I have a few more questions. As I'll get the URL dinamically, I (I suppose) can't use the u'string' sintax, and unicode("string") throws an error (can't decode character at position SOMETHING). Besides that, I noticed that the spaces become plus signs during this process, and that doesn't happen to Nautilus's URIs. Also, urllib, during this quoteing and unquoteing, turns the slashes into %2Fs, while Nautilus doesn't. –  Pablo Apr 4 '12 at 5:12
    
You can use quote instead of qoute_plus, it will keep spaces and /. If you need to keep some extra characters, there is a second paramether to both methods (quote, quote_plus), it accepts a string, it will avoid the substitution of all the characters that appear in the string. –  Javier Rivera Apr 4 '12 at 6:44
    
Note that quote needs a 8 bit string, if your URL is encoded as utf-8 (likely in linux-world) you can just pass it as is to unquote. –  Javier Rivera Apr 4 '12 at 6:52
    
Sorry for taking too long to answer. So, it works! You're awesome! Thank you very much! :D –  Pablo Apr 28 '12 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.