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

  • Home
  • SEARCH
  • 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 3272266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:48:30+00:00 2026-05-17T18:48:30+00:00

I am attempting to determine the size of a downloaded file in python before

  • 0

I am attempting to determine the size of a downloaded file in python before parsing and manipulating it with BeautifulSoup. (I intend to update to ElementTree soon, but having briefly played with it, it does not solve the problem I am posing here, as far as I can see).

import urllib2, BeautifulSoup
query = 'http://myexample.file.com/file.xml'
f = urllib2.urlopen(query)
print len(f.read())
soup = BeautifulSoup.BeautifulStoneSoup(f.read())

This code falters because when I read() the file the first time in len(), it naturally reaches an EOF and so the file object is then empty by the time I want to access it with BeautifulSoup.

My inital thought was simply to copy the object with a fcopy = f line, but this led me to learn I’m merely referencing the underlying object and gain nothing.

I then thought that fcopy = copy.copy(f) would create a true copy of the object, but apparently not as reading f still results in fcopy being an empty file object.

I even read about passing objects as parameters to functions in order to get round this, and tried the following code

import urllib2, BeautifulSoup
def get_bytes(file):
    return len(file.read())

query = 'http://myexample.file.com/file.xml'
f = urllib2.urlopen(query)
print(get_bytes(f))
soup = BeautifulSoup.BeautifulStoneSoup(f.read())

But I had the same problem. How can I determine the file size of this object without effectively destroying the file?

  • 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-17T18:48:30+00:00Added an answer on May 17, 2026 at 6:48 pm

    Copy the content of the file into a variable and work with it:

    import urllib2, BeautifulSoup
    
    query = 'http://myexample.file.com/file.xml'
    f = urllib2.urlopen(query)
    content = f.read()
    print len(content)
    soup = BeautifulSoup.BeautifulStoneSoup(content)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to determine prior art for the following idea: 1) user types
I am attempting to determine the amount of free space a CD has using
I am attempting to use a UIWebView to display some variable-size content, and I'd
I am attempting to determine if I can compute the sum of two 32
I am attempting to use Reflection in C# to determine at runtime the type
I am attempting to pull some information from my tnsnames file using regex. I
I'm attempting to determine the end of an English sentence (only approximately), by looking
I'm attempting to use sizeWithFont as follows: [commentTextLabel.text sizeWithFont:commentTextLabel.font constrainedToSize:CGSizeMake(commentTextLabel.frame.size.width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; When I
I am attempting to read a file with some random names in the format
I've got some legacy dataset code which I'm updating. I'm attempting to determine if

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.