-1

With reference to this link I can retrieve the path and file name.

Further all I want to name the file as-

D:\Links\filename

for eg- http://www.example.com/category/location/filename - will be the original

D:\Links\category\location\filename.html - is what I want to do due to which it will run as -

fttp://D:/Links/category/location/filename.html

Help / Guidance in any form is welcome.

Thanks a lot :)

Community
  • 1
  • 1
Pallavi Joshi
  • 245
  • 2
  • 17
  • Sorry, this is very unclear. Where exactly are you having problems? What code do you have so far? – Daniel Roseman Jan 11 '16 at 11:29
  • @DanielRoseman- the answer below works well. All I wanted to set the local drive path while parsing the url. Sorry if I was not clear enough although I got the correct solution. – Pallavi Joshi Jan 11 '16 at 12:28

1 Answers1

1

You could use something like the following:

import urlparse
import os

url = "http://www.example.com/category/location/filename"

url_path = urlparse.urlparse(url).path
local_filename = os.path.join(r'd:', os.sep, 'links', os.path.normpath(url_path)[1:]) + ".html"
print local_filename

This would give you a local filename as follows:

d:\links\category\location\filename.html

The Python urlparse function can be used to extract the path from your URL, and then the os.path.normpath function can be used to fix all of the separators. Finally, os.path.join can be used to join all of your parts back together again safely.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97