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 7870801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:24+00:00 2026-06-03T01:43:24+00:00

I have the following code: f=open(‘data.txt’, ‘w’) conn = urllib2.urlopen(‘http://example.com’) page_html = conn.read() data=BeautifulSoup(page_html)

  • 0

I have the following code:

f=open('data.txt', 'w')
conn = urllib2.urlopen('http://example.com')
page_html = conn.read()
data=BeautifulSoup(page_html)
count=0
out=""
for val in data.findAll('td'):
    count=count+1
    if(count%2==0 and val.contents):
        out=out+val.contents[0].strip(' \n\t')+"\n"
    if(count>=18):
        f.write(out+"\n")
        break
f.closed

Now when I execute the code, the output of the previous run goes in the file data.txt

For eg, now I have example.com in the url, I run the code then I change the url to stackoverflow.com. Now when I run it again and check data.txt I have the output of example.com in the data.txt file. The next time I run with a different url I am getting output of stackoverflow.com in the file. Can somebody help me out with this? I checked the outputs at every stage in the code. If I directly give the output instead of writing in file, it works perfectly.

  • 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-06-03T01:43:26+00:00Added an answer on June 3, 2026 at 1:43 am

    You probably want to use f.close() instead of f.closed. The latter doesn’t close the file. It returns a boolean that tells you whether or not the file has been closed.

    I’d also suggest that you use a with statement instead of closing the file manually.

    with open('data.txt', 'w') as f:
        # etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@http://maps.google.com/maps?q=%@,
I have the following code: (define (get-data) (define port (open-input-file ../data/problem11.txt)) (define field 0)
I have the following code which I call google #!/bin/bash q=$1 open http://www.google.com/search?q=$q It
I have the following code to open and read URLs: html_data = urllib2.urlopen(req).read() and
Supposed I have the following code: <html:link onclick=jQuery('#add').data('name','${name}').dialog('open'); href=#> And with this, if ${name}
I have the following code: try { conn.Open(); OracleCommand cmd = conn.CreateCommand(); cmd.CommandType =
I have the following code: # Initialisations filename='/home/Admin/Desktop/Ncapa/i_capa.txt' f = open(filename, 'r') ty =
So I have the following code: // Learn more about F# at http://fsharp.net open
I have the following code: f = File.open(/log/mytext.txt) f.each_line do |i| i = i.delete(\n)
I have the following code to open a input and output file: if ((source_file_ptr

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.