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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:05:46+00:00 2026-05-26T21:05:46+00:00

I needed to download a file within a python program, someone told me to

  • 0

I needed to download a file within a python program, someone told me to do this.

source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)

It working very well, but I would like to understand the code.

When you have something like

patatoe = 1

Isn’t a variable?

and when you have a something like:

blabla()

isn’t to define a function?

Please, I would LOVE to understand correctly the code.

  • 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-26T21:05:47+00:00Added an answer on May 26, 2026 at 9:05 pm

    You define a function using the def keyword:

    def f():
        ...
    

    Without it, you are simply calling the function. open(...) returns a file object. which you then use to write the data out. It’s practically the same as this:

    f = open(...)
    f.write(source)
    

    It isn’t quite the same, though, since the variable f holds onto the file object until it goes out of scope, whereas calling open(...).write(source) creates a temporary reference to the file object that disappears immediately after write() returns. The consequence of this is that the single-line form will immediately flush and close the file, while the two-line form wil keep the file open — and possibly some or all of the output buffered — until f goes out of scope.

    You can observe this behaviour in the interactive shell:

    >>> f = open('xxx', 'w')
    >>> f.write('hello')
    >>> open('yyy', 'w').write('world')
    

    Now, without exiting the interactive shell, open another terminal window and check the contents of xxx and yyy. They’ll both exist, but only yyy will have anything in it. Also, if you go back to Python and invoke f = None or del f, you’ll find that xxx has now been written to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hard time reading open source file download pages like this http://rubyforge.org/frs/?group_id=167
I'm attempting to download a zip file using this code: o = urllib2.build_opener( urllib2.HTTPCookieProcessor()
I'm writing a program that among other things needs to download a file given
I have an HTTP server which host some large file and have python clients
So I'm making a program for android that tries to download something from www.wupload.com.
We needed to monitor all processes Registry calls/File Sytem calls/Process creations in the system
I needed to convert a Tab Seperated Text file into a tabular format as
I need to daily run a script that will download a file from a
is it possible to both return a file for download and update a view
When running my JSF Hello World in server I get a File download message

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.