0

I am having trouble with saving a file using Python on windows.

Here's the URL variable that stores the URL:

my_url = "https://example.com/some-page"

I want to remove the "https:" part and all the "/" from this string. This is what I tried:

filename = my_url.replace('https://', '')
filename = filename.replace('http://', '')
filename = filename.replace('/', '|') + ".txt"

I want to remove these characters as windows doesn't allow : and / characters as a file name.

The error that I am getting is:

Traceback (most recent call last):
  File "123.py", line 28, in <module>
    f = open(filename, "w")
OSError: [Errno 22] Invalid argument: 'example.com|some-page.txt'

I want to do this with multiple URLs so even though the actual link uses https I tried to remove the http too.

1
  • 1
    But it also doesn't allow | in the file name so you'll need a different replacement character. Commented Jan 1, 2018 at 16:02

3 Answers 3

1

There is a function in urllib called urllib.parse.quote which removes special characters from urls and replaces them with their equivalent percent encoding.

urllib.parse.quote(string, safe='/', encoding=None, errors=None)

Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of URL. The optional safe parameter specifies additional ASCII characters that should not be quoted — its default value is '/'.

Sign up to request clarification or add additional context in comments.

Comments

1

The pipe character ("|") is not allowed in Windows filenames either. Source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

Comments

0

I managed to solve the problem :)

Here's what I did:

filename = my_url.replace('https://', '')
filename = filename.replace('http://', '')
filename = filename.replace('.', '_')
filename = filename.replace('-', '_')
filename = filename.replace('/', '_') + ".txt"

Thank you!

1 Comment

No need to replace - and .. Those are allowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.