Hello .
I need code to download from the internet a list of files .
Their names + addresses are in a csv file , and no problems with permission / access .
The following code doesn't work , can it be fixed , re-written ?
I installled Python 2.7 and also 3.7 on my Windows pc just in case ...
Thank you .
import csv
import requests
with open('list.csv') as csvfile:
csvrows = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvrows:
filename = row[1]
url = row[0]
print(url)
result = requests.get(url, stream=True)
if result.status_code == 200:
image = result.raw.read()
open(filename,"wb").write(image)
Quote:The following code doesn't work
Please elaborate, how does it not work?
Thank you ichabod801 for formatting my text ,
i don't know python , and i have no idea how to use python tags .
As to your question Larz60+ , well , the code does not save the files
to my hard disk (they are Jpeg images if it matters) .
That's what i meant when i wrote that it doesn't work .
I don't code in python , but i thought that someone here will know
python , and will understand from the code what is wrong there .
Thank you .
Read comments and code modifications
import csv
import requests
with open('list.csv') as csvfile:
csvrows = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvrows:
filename = row[1]
url = row[0]
print(url)
result = requests.get(url, stream=True)
# In the statement below, you do not display any error message if status is not ok
if result.status_code == 200:
image = result.raw.read()
# better to write following as:
# open(filename,"wb").write(image)
with open(filename, 'wb') as fp:
fp.write(image)
else:
print(f"requests.get failed with status code: {result.status_code}")
Thank you Larz60+ for correcting the code .
Unfortunately , it still does not save the images .
More details :
I have a directory called "data" in the D:\ folder ,
and in this directory i keep the files "list.csv" & "data.py" .
I tried running "data.py" and also "python data.py" ,
and the result is the same error message , that a modoule is missing .
I uploaded a screen-capture here :
https://ev11.000webhostapp.com/misc/python01.jpg
I installled Python 2.7.x and also 3.7.x on my Windows 7 pc .
Hope i'm clear in what i am asking ,
and thank you again .
Have you installed the requests library (i.e. pip install requests
)?
Thank you ndc85430 .
I tried that , and got an error message that it can't be done because pip is too old .
That's weird , since just this week i installed the latest versions of python 2 & 3 .
It took me some time to find the command to update pip , but i didn't give up .
I tried updating pip , and that didn't work because i wasn't an administrator .
I re-logged and tried again , and it didn't update because it wanted me to FIRST uninstall pip .
So , i had to look for that command ...
All is well that ends well , i overcame this problems , and now the code works .
I wanted to link to the screen captures , but Larz60+ fotbids me ,
and there is absolutely no way i will hand type about 20 lines of error messages !
As someone who doesn't use python , i think this is TOO MUCH trobles if you want to invite more people to use python .
Just last question : is that a typo in the last line ? where it says "frint(f..."
Thank you all again for your help
Quote:I wanted to link to the screen captures , but Larz60+ fotbids me ,
These are forum rules, not mine.
You don't need to type, use cut and paste instead
if on linux:
bash shell, highlight text, right click, copy
if on windows:
right click, select Mark, highlight text, right click select copy
windows instructions from memory, so might be slightly different.
Just before i click the "set solved" button ,
can you please explain if there is a typo in the last line in the code ?
Where it says "print(f..." .
should it be "printf(... ? Or just "print(..." ?
And about the cut-paste , you have a mistake .
After you highlight a text , right clicking cancels the highlight ,
it does not give a menu to choose "copy" .