I have a nautilus script to copy tunes that I like into a special folder which I sync to my phone and my car. It fails on paths with funny characters like á in them. I’m fixing it incrementally with stuff like:
temp = temp.replace('%20', ' ')
temp = temp.replace('%5B', '[')
temp = temp.replace('%5D', ']')
but I’m getting tired of these bandaid solutions, and I’m sure there is a better way to do this with str.encode or str.decode.
Does anyone recognise this strange encoding and how I can handle it properly? The problem is, for example, I have a folder such as
/media/music/kálmán balogh and the gipsy cimbalom band/aven shavale
on my disk, but when I get it using os.getenv('NAUTILUS_SCRIPT_CURRENT_URI'), i.e. the currently selected folder in nautilus, it appears in python as
/media/music/k%C3%A1lm%C3%A1n balogh and the gipsy cimbalom band/aven shavale
and then other actions such as renaming or copying the file don’t work because it doesn’t find the file on disk.
You are looking at url encoding. Use
urllib.unquote()to interpret these to UTF-8 encoded text, then decode to unicode:In Python 3, you need to use
urllib.parse.unquote(); the function was moved.