Open URL encoded filenames in Unix
Question: I'm a python n00b. I have downloaded URL encoded file and I want to work with
it on my unix system(Ubuntu 14).

When I try and run some operations on my file, the system says that the file
doesn't exist. How do I change my filename to a unix recognizable format?

Some of the files I have download have spaces in them so they would have to be
presented with a backslash and then a space. Below is a snippet of my code

    
    
    link = "http://www.stephaniequinn.com/Music/Scheherezade%20Theme.mp3"
    
    output = open(link.split('/')[-1],'wb')
    output.write(site.read())
    output.close()
    
    shutil.copy(link.split('/')[-1], tmp_dir)
    


Answer: The "link" you have actually is a
[URL](http://%3E%3E%3E%20import%20urllib.parse%20%3E%3E%3E%20url%20=%20%22http://www.stephaniequinn.com/Music/Scheherezade%20Theme.mp3%22%20%3E%3E%3E%20urllib.unqote\(url\)%20Traceback%20\(most%20recent%20call%20last\):%20%20%20File%20%22%3Cstdin%3E%22,%20line%201,%20in%20%3Cmodule%3E%20AttributeError:%20'module'%20object%20has%20no%20attribute%20'unqote'%20%3E%3E%3E%20urllib.parse.unquote\(url\)%20'http://www.stephaniequinn.com/Music/Scheherezade%20Theme.mp3'%20%3E%3E%3E).
URLs are special and are not allowed to contain certain characters, such as
spaces. These special characters can still be represented, but in an encoded
form. The translation from special characters to this encoded form happens via
a certain rule set, often known as "URL encoding". If interested, have a read
over here: <http://en.wikipedia.org/wiki/Percent-encoding>

The encoding operation can be inverted, which is called decoding. The tool set
with which you downloaded the files you mentioned most probably did the
decoding already, for you. In your link example, there is only one special
character in the URL, "%20", and this encodes a space. Your download tool set
probably decoded this, and saved the file to your file system with the actual
space character in the file name. That is, most likely you have a file in the
file system with the following
[basename](http://linux.die.net/man/1/basename):

    
    
    Scheherezade Theme.mp3
    

So, when you want to open that file from within Python, and all you have is
the `link`, you first need to get the decoded variant of it. Python can decode
URL-encoded strings with built-in tools. This is what you need:

    
    
    >>> import urllib.parse
    >>> url = "http://www.stephaniequinn.com/Music/Scheherezade%20Theme.mp3"
    >>> urllib.parse.unquote(url)
    'http://www.stephaniequinn.com/Music/Scheherezade Theme.mp3'
    >>>
    

This assumes that you are using Python 3, and that your `link` object is a
unicode object (type `str` in Python 3).

Starting off with the **decoded** URL, you can derive the filename. Your
`link.split('/')[-1]` method might work in many cases, but J.F. Sebastian's
answer provides a more reliable method.

