Download and Save a file in python 2.7?

后端 未结 2 1407
说谎
说谎 2021-01-28 03:34

Coming from here:

Basic http file downloading and saving to disk in python?

Is there any possibility to save the file in any folder? I tried this but i get error

2条回答
  •  花落未央
    2021-01-28 04:14

    You're most likely getting that error because /myfolder doesn't exist. Try creating it first

    import os
    import os.path
    import urllib
    
    destination = "/path/to/folder"
    if os.path.exists(destination) is False:
        os.mkdirs(destination)
    # You can also use the convenience method urlretrieve if you're using urllib anyway
    urllib.urlretrieve("http://randomsite.com/file.gz", os.path.join(destination, "file.gz"))
    

提交回复
热议问题