-2

Hi engineers & developers, I am really worried and stuck in the python program to replace the windows based URL to a web URL. Unfortunately, I couldn't find any solution for this. I do tried for hours to solve this problem but I couldn't. if any one help me is a great service guys. Ok let me tell the problem below.

This is the URL

http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:\Windows\TEMP\243dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf

when I basically run the python below code

url = """http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:\Windows\TEMP\243dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf"""
print(url)

The output will be like this

http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:\Windows\TEMP£dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf

please check it has replaced some values from the original URL. In this parameter reportFile=

I want the URL to be replaced like this

http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:/Windows/TEMP/243dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf

I tried and I failed

I wanted a common routine for downloading purpose of any file using URL in the python snippet. Because I am new to python. Please help me. Thanks in advance

  • 2
    Duplicate of [How to prevent automatic escaping of special characters in Python](https://stackoverflow.com/questions/12605090/how-to-prevent-automatic-escaping-of-special-characters-in-python) – esqew Nov 18 '20 at 05:17

1 Answers1

0

The reason this is happening is python is automatically interpreting the backslash followed by numbers as an escape sequence. The solution is to make the string a raw string by adding the r prefix and replacing the \ with /.

url = r"""http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:\Windows\TEMP\243dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf""".replace('\\', '/')
print(url)

will output:

http://203.215.33.115/CompanyVerification/Pages/ViewPDF.aspx?reportFile=C:/Windows/TEMP/243dbd82-9e93-42bb-a5ec-8fa032e93fdf.pdf
Umbral Reaper
  • 172
  • 3
  • 9