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 a text file using list() Pedroski55 2 115 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,524 May-28-2025, 07:56 AM
Last Post: Winfried
  Online Python Editor a_k93 4 1,532 Mar-27-2025, 07:21 AM
Last Post: coolwholesales
  Trying to open depracated joblib file mckennamason 0 2,156 Sep-19-2024, 03:30 PM
Last Post: mckennamason
  Open/save file on Android frohr 0 2,187 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 17,397 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 2,703 Aug-14-2023, 05:04 PM
Last Post: snippsat
  I cannot able open a file in python ? ted 5 15,867 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 3,674 Dec-20-2022, 02:19 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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