3

I want to download text files using python, how can I do so?

I used requests module's urlopen(url).read() but it gives me the bytes representation of file.

1
  • try, urlopen(url).read().decode() ? Commented Sep 4, 2022 at 11:07

3 Answers 3

2

When downloading text files with python I like to use the wget module

import wget

remote_url = 'https://www.google.com/test.txt'

local_file = 'local_copy.txt'

wget.download(remote_url, local_file)

If that doesn't work try using urllib

from urllib import request

remote_url = 'https://www.google.com/test.txt'

file = 'copy.txt'

request.urlretrieve(remote_url, file)

When you are using the request module you are reading the file directly from the internet and it is causing you to see the text in byte format. Try to write the text to a file then view it manually by opening it on your desktop

import requests

remote_url = 'test.com/test.txt'

local_file = 'local_file.txt'

data = requests.get(remote_url)

with open(local_file, 'wb')as file:
file.write(data.content)

Sign up to request clarification or add additional context in comments.

Comments

2

For me, I had to do the following (Python 3):

from urllib.request import urlopen

data = urlopen("[your url goes here]").read().decode('utf-8')

# Do what you need to do with the data.

Comments

2

You can use multiple options:

  1. For the simpler solution you can use this

     file_url = 'https://someurl.com/text_file.txt'
     for line in urllib.request.urlopen(file_url):
         print(line.decode('utf-8')) 
    
  2. For an API solution

     file_url = 'https://someurl.com/text_file.txt'
     response = requests.get(file_url)
     if (response.status_code):
         data = response.text
     for line in enumerate(data.split('\n')):
         print(line)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.