Simple file download.
Well, that works modestly well. It downloaded both sample files for this Google code jam problem.
Well, not working on github.com
Getting coding error
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/78B1F70D-7DE8-4840-87C8-F5C1D01E9EF5/Pythonista3/Documents/LPTHW/FileDownload.py", line 49, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xb7' in position 1367: ordinal not in range(128)
JonB last edited by JonB
You are better off using
urllib. It is easier to work with, and it handles decoding of the charset for you (otherwise, you need to check what encoding the website uses, and decode based in that). Also, you are better off using 'wb' to open your file, and write the bytes directly, rather than trying to mess with encodings at all.
Here is a 2 liner to save a file:
with open(destpath,'wb') as file: file.write(requests.get(url).content)
See https://gist.github.com/jsbain/fcb3f42932dde9b0ff6c122893d1b230 for how this is used in an app extension that can be run from safari, to save a file to pythonista.
By the way, to post code in the forums, please type three backticks before you paste, on a separate line, and theee backticks after:
``` your code here ```
Backticks on ios are a pain, you have to long press the single quote, and select the option on the left. I like to create a keyboard shortcut in settings, which replaces ,,, with ``` for this purpose.
Ok, let's give it a try.
Here is a simple working version of your suggestion.
I like the 'wb' switch.
import requests print() destpath = 'ex24file.txt' url = "https://raw.githubusercontent.com/grrrr/py/741ba0500bc49e8f6268f02d23e461649e8d457b/scripts/buffer.py" with open(destpath,'wb') as file: file.write(requests.get(url).content)
Tagging this discussion for future reference. link text
TutorialDoctor last edited by TutorialDoctor
Found this bit of code on github inside of a game made with Pythonista.
It downloads a file from their Google Drive account into the directory the script that runs this code is.
lukaskollmer last edited by
TutorialDoctor last edited by
So what is the difference between request and urllib.request?
Same library, just different collections?
abcabc last edited by
lachlantula last edited by
@TutorialDoctor Hahaha, looks like you found my game I made as a joke between me and my friend:P