A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Python: Saving a file from the URL by chunks

For example, when you want to save an image from a URL.

https://github.com/images/modules/header/logov3-hover.png
f = urllib.urlopen(request.POST['url'])
    p = os.path.join(settings.UPLOAD_DIRECTORY, "saved_file.jpg")
    filesize = 0
    with open(p,'wb') as output:
        while True:
            buf = f.read(65536)
            filesize += len(buf)
            if not buf:
                break
            output.write(buf)
This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>