save url as a file name in python
Question: Firstly, I'm pretty new in python, please leave a comment as well if you
consider to down vote

I have a url such as

    
    
    http://example.com/here/there/index.html
    

now I want to save a file and its content in a directory. I want the name of
the file to be :

    
    
    http://example.com/here/there/index.html
    

but I get error, I'm guessing that error is as the result of `/` in the url
name.

This is what I'm doing at the moment.

    
    
            with open('~/' + response.url, 'w') as f:
                f.write(response.body)
    

any ideas how I should do it instead?


Answer: You could use the reversible
[base64](https://docs.python.org/2/library/base64.html) encoding.

    
    
    >>> import base64
    >>> base64.b64encode('http://example.com/here/there/index.html')
    'aHR0cDovL2V4YW1wbGUuY29tL2hlcmUvdGhlcmUvaW5kZXguaHRtbA=='
    >>> base64.b64decode('aHR0cDovL2V4YW1wbGUuY29tL2hlcmUvdGhlcmUvaW5kZXguaHRtbA==')
    'http://example.com/here/there/index.html'
    

or perhaps [binascii](https://docs.python.org/2/library/binascii.html)

    
    
    >>> binascii.hexlify(b'http://example.com/here/there/index.html')
    '687474703a2f2f6578616d706c652e636f6d2f686572652f74686572652f696e6465782e68746d6c'
    >>> binascii.unhexlify('687474703a2f2f6578616d706c652e636f6d2f686572652f74686572652f696e6465782e68746d6c')
    'http://example.com/here/there/index.html'
    

