PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How Can I Read Files directly from its url

 August 29, 2022     csv, python, read.csv     No comments   

Issue

url = 'https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv'

f = open(url, 'r')
print(f.read())

Invalid argument: 'https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv'

Is there any way so that i can read file from url, instead of downloading it ??


Solution

Yes, the standard urllib.request.urlopen() does exactly that:

from urllib import request

with request.urlopen('https://cdn.wsform.com/wp-content/uploads/2021/05/currency.csv') as f:
    print(f.read().decode())  # the decode turns the bytes into a string for printing

Just note that the file is opened as a file of bytes, since the function can't know if it's a text file or not.



Answered By - Grismar
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing