2

I am trying to convert a url like "www.example.com/images/dog.png" into directories from the current directory.

So I get a folder named "www.example.com", inside that "images" and finally inside that the file saved as "dog.png"

I've tried using urllib.url2pathname(path) but it keeps appending P:\ to the start of it.

2
  • I use it like url2pathname("www.google.com") and it returns "P:\www.google.com" Commented Apr 18, 2015 at 12:57
  • Just tested urllib.url2pathname(path) and got the correct result on Python 2.7.13. result: www.example.com\images\dog.png Commented Feb 6, 2019 at 10:30

1 Answer 1

3

You can use os.makedirs() to create the directory tree, but that will fail if the final directory already exists. So you can test if it exists before attempting to create the directory tree, or use try: ... except OSError:. In Python 3 you can supply an exist_ok parameter to over-ride this behaviour, see the Python docs of os.makedirs for further info.

#!/usr/bin/env python

import os

cwd = os.getcwd()

url = "www.example.com/images/dog.png"

fullname = os.path.join(cwd, url)
path, basename = os.path.split(fullname)
if not os.path.exists(path):
    os.makedirs(path)

with open(fullname, 'w') as f:
    f.write('test\n')

If your system doesn't support directory names containing periods you can translate them to another character, eg _, like this:

fullname = fullname.replace('.', '_')

(just insert this after the fullname = os.path.join(cwd, url) line).

And as jwilner mentions in the comments, it's more efficient to use

path = os.path.dirname

than path, basename = os.path.split(fullname) if you don't need the base component of the file name (in this example "dog.png").

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

8 Comments

This doesn't work because the url is not in the same format as a path directory
@Shifty: It works for me (on Linux on an ext3 filesystem) with that URL. Are you using an OS or filesystem that doesn't permit periods in pathnames? If so, you should have mentioned that in your question. And that can be easily fixed with, eg fullname = fullname.replace('.', '_')
I would suggest that you use os.path.dirname instead of os.path.split -- you have no need for the basename component, and os.path.dirname is more explicit. Also, the os.path.exists call is unnecessary -- os.makedirs only makes directories which do not exist.
@jwilner OTOH, the os.makedirs issue is a little complicated. In Python 2 "Raises an error exception [OSError] if the leaf directory already exists or cannot be created". In Python 3 there's an exist_ok parameter: "If exist_ok is False (the default), an OSError is raised if the target directory already exists."
Path names are not consistent across windows and linux. So using the URL as the path name explicitly does not support windows path naming conventions of the "\" backslash
|

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.