Python Forum
Using online python for course, How do I open a File that's a link?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using online python for course, How do I open a File that's a link?
#1
Hi there!

I am doing a (beginner) python course online that allows us to use python in a jupyter notebook. I am trying to open a file that looks like a link and is a .csv.

links={'GDP':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv',\
       'unemployment':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_unemployment.csv'}
#GDP = links["GDP"]
#print(GDP)
with open('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv', 'r') as GDPFile:
    GDPData = GDPFile.read()
    print(GDPData)
print(GDPFile.closed)
print(GDPData)
And I get this error:
Error:
FileNotFoundError Traceback (most recent call last) <ipython-input-14-53937e55fa2e> in <module> 4 #GDP = links["GDP"] 5 #print(GDP) ----> 6 with open('https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv', 'r') as GDPFile: 7 GDPData = GDPFile.read() 8 print(GDPData) FileNotFoundError: [Errno 2] No such file or directory: 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv'
So I downloaded the file to my local drive and tried it this way:

links={'GDP':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv',\
       'unemployment':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_unemployment.csv'}
#GDP = links["GDP"]
#print(GDP)
with open('C:\Users\Amanda\Documents\Coursera IBM Data Science Certification\Python for Data Science and AI\clean_gdp.csv', 'r') as GDPFile:
    GDPData = GDPFile.read()
    print(GDPData)
print(GDPFile.closed)
print(GDPData)
However I get this error:
Error:
File "<ipython-input-11-59f1f16be3ed>", line 6 with open('C:\Users\Amanda\Documents\Coursera IBM Data Science Certification\Python for Data Science and AI\clean_gdp.csv', 'r') as GDPFile: ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Am I missing something? Does the file need to be saved in the notebook or under the same project? I'm using python on a data platform, IBM cloud.
Reply
#2
The second error is because you use backslash in path and some combinations like \u are escape sequences.
When on windows, use raw strings for path, or use forward slash.

for reading from web

import requests
import csv
import io

import pandas

base_url = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/'
links={'GDP':'clean_gdp.csv', 'unemployment':'clean_unemployment.csv'}
url = f"{base_url}{links['GDP']}"

# using requests and csv
response = requests.get(url)
csv_rdr = csv.reader(io.StringIO(response.text)) # also you can use DictReader instead
print(list(csv_rdr)[:5])  # you can iterate over csv_rdr instead

# alternative - using pandas
data = pandas.read_csv(url)
print(data.head())
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 317 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,089 Dec-14-2023, 08:03 AM
Last Post: shanoger
  How can i combine these two functions so i only open the file once? cubangt 4 853 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 3,307 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,375 Dec-20-2022, 02:19 AM
Last Post: Skaperen
  Run a Function?: Trouble with Online Python Course webmanoffesto 3 1,004 Aug-18-2022, 10:14 PM
Last Post: deanhystad
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,400 May-06-2022, 01:44 PM
Last Post: Larz60+
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,373 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  I can't open a link with Selenium in Python jao 0 1,396 Jan-30-2022, 04:21 AM
Last Post: jao
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,026 Jan-07-2022, 04:55 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020