Alternative of urllib.urlretrieve in Python 3.5How to implement the equivalent of urllib.urlretrieve in Python 3?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

Could a guilty Boris Johnson be used to cancel Brexit?

Can those paralyzed by the Hold Person spell be forcibly moved?

What is a simple, physical situation where complex numbers emerge naturally?

Sucuri detects malware on wordpress but I can't find the malicious code

Rotated Position of Integers

Why does my electric oven present the option of 40A and 50A breakers?

Is American Express widely accepted in France?

Are there practical reasons to NOT use a stepper motor with lead screw for the X and or Y axes?

Why were the Night's Watch required to be celibate?

How is it possible for Mordenkainen to be alive during the Curse of Strahd adventure?

Why are grass strips more dangerous than tarmac?

Is it legal in the UK for politicians to lie to the public for political gain?

Credit card offering 0.5 miles for every cent rounded up. Too good to be true?

Pros and cons of writing a book review?

Strange math syntax in old basic listing

Opposite of "Squeaky wheel gets the grease"

How to provide realism without making readers think grimdark

Is the capacitor drawn or wired wrongly?

How can a single Member of the House block a Congressional bill?

Accidentally cashed a check twice

How can Iron Man's suit withstand this?

What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?

Please help me identify this plane

Computing the differentials in the Adams spectral sequence



Alternative of urllib.urlretrieve in Python 3.5


How to implement the equivalent of urllib.urlretrieve in Python 3?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am currently doing a course on machine learning in UDACITY . In there they have written some code in python 2.7 but as i am currently using python 3.5 , i am getting some error . This is the code



import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


I tried urllib.request .



 import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


But still gives me error .



urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable


I am using PyCharm as my IDE .










share|improve this question






















  • if requests is available, I'd use that

    – Wayne Werner
    Jul 13 '16 at 17:57






  • 1





    Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

    – CodeHead
    Jul 13 '16 at 17:59

















1















I am currently doing a course on machine learning in UDACITY . In there they have written some code in python 2.7 but as i am currently using python 3.5 , i am getting some error . This is the code



import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


I tried urllib.request .



 import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


But still gives me error .



urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable


I am using PyCharm as my IDE .










share|improve this question






















  • if requests is available, I'd use that

    – Wayne Werner
    Jul 13 '16 at 17:57






  • 1





    Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

    – CodeHead
    Jul 13 '16 at 17:59













1












1








1








I am currently doing a course on machine learning in UDACITY . In there they have written some code in python 2.7 but as i am currently using python 3.5 , i am getting some error . This is the code



import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


I tried urllib.request .



 import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


But still gives me error .



urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable


I am using PyCharm as my IDE .










share|improve this question














I am currently doing a course on machine learning in UDACITY . In there they have written some code in python 2.7 but as i am currently using python 3.5 , i am getting some error . This is the code



import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.urlretrieve(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


I tried urllib.request .



 import urllib
url = "https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz"
urllib.request(url, filename="../enron_mail_20150507.tgz")
print ("download complete!")


But still gives me error .



urllib.request(url, filename="../enron_mail_20150507.tgz")
TypeError: 'module' object is not callable


I am using PyCharm as my IDE .







python python-3.x urllib2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 13 '16 at 17:48









CodeHeadCodeHead

57110




57110












  • if requests is available, I'd use that

    – Wayne Werner
    Jul 13 '16 at 17:57






  • 1





    Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

    – CodeHead
    Jul 13 '16 at 17:59

















  • if requests is available, I'd use that

    – Wayne Werner
    Jul 13 '16 at 17:57






  • 1





    Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

    – CodeHead
    Jul 13 '16 at 17:59
















if requests is available, I'd use that

– Wayne Werner
Jul 13 '16 at 17:57





if requests is available, I'd use that

– Wayne Werner
Jul 13 '16 at 17:57




1




1





Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

– CodeHead
Jul 13 '16 at 17:59





Someone just answerd it and worked for me . instead of urllib.urlretrive one would have to use urllib.request.urlretrieve

– CodeHead
Jul 13 '16 at 17:59












2 Answers
2






active

oldest

votes


















7














You'd use urllib.request.urlretrieve. Note that this function "may become deprecated at some point in the future", so you might be better off using the less likely to be deprecated interface:



# Adapted from the source:
# https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
with open(filename, 'wb') as out_file:
with contextlib.closing(urllib.request.urlopen(url)) as fp:
block_size = 1024 * 8
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)


For small enough files, you could just read and write the whole thing and drop the loop entirely.






share|improve this answer

























  • Thanks a lot , It worked fine .

    – CodeHead
    Jul 13 '16 at 18:04


















0














I know this question has long been answered but I'll contribute for any future viewer.



The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.



As a workaround to this problem here is how I adapted the code:



def getfile(url,filename,timeout=45):
with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
block_size = 1024 * 8
block = fp.read(block_size)
if block:
with open(filename,'wb') as out_file:
out_file.write(block)
while True:
block = fp.read(block_size)
if not block:
break
out_file.write(block)
else:
raise Exception ('nonexisting file or connection error')


I hope this help.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38358521%2falternative-of-urllib-urlretrieve-in-python-3-5%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7














    You'd use urllib.request.urlretrieve. Note that this function "may become deprecated at some point in the future", so you might be better off using the less likely to be deprecated interface:



    # Adapted from the source:
    # https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
    with open(filename, 'wb') as out_file:
    with contextlib.closing(urllib.request.urlopen(url)) as fp:
    block_size = 1024 * 8
    while True:
    block = fp.read(block_size)
    if not block:
    break
    out_file.write(block)


    For small enough files, you could just read and write the whole thing and drop the loop entirely.






    share|improve this answer

























    • Thanks a lot , It worked fine .

      – CodeHead
      Jul 13 '16 at 18:04















    7














    You'd use urllib.request.urlretrieve. Note that this function "may become deprecated at some point in the future", so you might be better off using the less likely to be deprecated interface:



    # Adapted from the source:
    # https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
    with open(filename, 'wb') as out_file:
    with contextlib.closing(urllib.request.urlopen(url)) as fp:
    block_size = 1024 * 8
    while True:
    block = fp.read(block_size)
    if not block:
    break
    out_file.write(block)


    For small enough files, you could just read and write the whole thing and drop the loop entirely.






    share|improve this answer

























    • Thanks a lot , It worked fine .

      – CodeHead
      Jul 13 '16 at 18:04













    7












    7








    7







    You'd use urllib.request.urlretrieve. Note that this function "may become deprecated at some point in the future", so you might be better off using the less likely to be deprecated interface:



    # Adapted from the source:
    # https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
    with open(filename, 'wb') as out_file:
    with contextlib.closing(urllib.request.urlopen(url)) as fp:
    block_size = 1024 * 8
    while True:
    block = fp.read(block_size)
    if not block:
    break
    out_file.write(block)


    For small enough files, you could just read and write the whole thing and drop the loop entirely.






    share|improve this answer















    You'd use urllib.request.urlretrieve. Note that this function "may become deprecated at some point in the future", so you might be better off using the less likely to be deprecated interface:



    # Adapted from the source:
    # https://hg.python.org/cpython/file/3.5/Lib/urllib/request.py#l170
    with open(filename, 'wb') as out_file:
    with contextlib.closing(urllib.request.urlopen(url)) as fp:
    block_size = 1024 * 8
    while True:
    block = fp.read(block_size)
    if not block:
    break
    out_file.write(block)


    For small enough files, you could just read and write the whole thing and drop the loop entirely.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 13 '16 at 18:00

























    answered Jul 13 '16 at 17:54









    mgilsonmgilson

    217k41429542




    217k41429542












    • Thanks a lot , It worked fine .

      – CodeHead
      Jul 13 '16 at 18:04

















    • Thanks a lot , It worked fine .

      – CodeHead
      Jul 13 '16 at 18:04
















    Thanks a lot , It worked fine .

    – CodeHead
    Jul 13 '16 at 18:04





    Thanks a lot , It worked fine .

    – CodeHead
    Jul 13 '16 at 18:04













    0














    I know this question has long been answered but I'll contribute for any future viewer.



    The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.



    As a workaround to this problem here is how I adapted the code:



    def getfile(url,filename,timeout=45):
    with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
    block_size = 1024 * 8
    block = fp.read(block_size)
    if block:
    with open(filename,'wb') as out_file:
    out_file.write(block)
    while True:
    block = fp.read(block_size)
    if not block:
    break
    out_file.write(block)
    else:
    raise Exception ('nonexisting file or connection error')


    I hope this help.






    share|improve this answer



























      0














      I know this question has long been answered but I'll contribute for any future viewer.



      The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.



      As a workaround to this problem here is how I adapted the code:



      def getfile(url,filename,timeout=45):
      with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
      block_size = 1024 * 8
      block = fp.read(block_size)
      if block:
      with open(filename,'wb') as out_file:
      out_file.write(block)
      while True:
      block = fp.read(block_size)
      if not block:
      break
      out_file.write(block)
      else:
      raise Exception ('nonexisting file or connection error')


      I hope this help.






      share|improve this answer

























        0












        0








        0







        I know this question has long been answered but I'll contribute for any future viewer.



        The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.



        As a workaround to this problem here is how I adapted the code:



        def getfile(url,filename,timeout=45):
        with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
        block_size = 1024 * 8
        block = fp.read(block_size)
        if block:
        with open(filename,'wb') as out_file:
        out_file.write(block)
        while True:
        block = fp.read(block_size)
        if not block:
        break
        out_file.write(block)
        else:
        raise Exception ('nonexisting file or connection error')


        I hope this help.






        share|improve this answer













        I know this question has long been answered but I'll contribute for any future viewer.



        The proposed solution is good but the main issue if that it can generate empty files if you are using invalid urls.



        As a workaround to this problem here is how I adapted the code:



        def getfile(url,filename,timeout=45):
        with contextlib.closing(urlopen(url,timeout=timeout)) as fp:
        block_size = 1024 * 8
        block = fp.read(block_size)
        if block:
        with open(filename,'wb') as out_file:
        out_file.write(block)
        while True:
        block = fp.read(block_size)
        if not block:
        break
        out_file.write(block)
        else:
        raise Exception ('nonexisting file or connection error')


        I hope this help.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 11:52









        Al rlAl rl

        195




        195



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38358521%2falternative-of-urllib-urlretrieve-in-python-3-5%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해