Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 366059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:35:48+00:00 2026-05-12T13:35:48+00:00

In Python, when given the URL for a text file, what is the simplest

  • 0

In Python, when given the URL for a text file, what is the simplest way to access the contents off the text file and print the contents of the file out locally line-by-line without saving a local copy of the text file?

TargetURL=http://www.myhost.com/SomeFile.txt
#read the file
#print first line
#print second line
#etc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:35:48+00:00Added an answer on May 12, 2026 at 1:35 pm

    Edit 09/2016: In Python 3 and up use urllib.request instead of urllib2

    Actually the simplest way is:

    import urllib2  # the lib that handles the url stuff
    
    data = urllib2.urlopen(target_url) # it's a file like object and works just like a file
    for line in data: # files are iterable
        print line
    

    You don’t even need "readlines", as Will suggested. You could even shorten it to: *

    import urllib2
    
    for line in urllib2.urlopen(target_url):
        print line
    

    But remember in Python, readability matters.

    However, this is the simplest way but not the safe way because most of the time with network programming, you don’t know if the amount of data to expect will be respected. So you’d generally better read a fixed and reasonable amount of data, something you know to be enough for the data you expect but will prevent your script from been flooded:

    import urllib2
    
    data = urllib2.urlopen("http://www.google.com").read(20000) # read only 20 000 chars
    data = data.split("\n") # then split it into lines
    
    for line in data:
        print line
    

    * Second example in Python 3:

    import urllib.request  # the lib that handles the url stuff
    
    for line in urllib.request.urlopen(target_url):
        print(line.decode('utf-8')) #utf-8 or iso8859-1 or whatever the page encoding scheme is
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Extend AuthenticationDetailsSourceImpl: import javax.servlet.http.HttpServletRequest; import org.acegisecurity.ui.AuthenticationDetailsSourceImpl; public class UserAgentAuthenticationDetailsSourceImpl extends… May 12, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer onkeydown (although it wouldn't work for pasted data, where onchange… May 12, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer It's been a while since I used ATL but, IIRC,… May 12, 2026 at 10:43 pm

Related Questions

I am trying to implement a djapian based full text search for searching user
For academic and performance sake, given this crawl recursive web-crawling function (which crawls only
I have this dictionary in my apps model file: TYPE_DICT = ( (1, Shopping
Im checking the examples google gives on how to start using python; especifically the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.