[{"data":1,"prerenderedAt":1720},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-download-files-from-a-url-example":3},{"id":4,"title":5,"body":6,"description":1713,"extension":1714,"meta":1715,"navigation":76,"path":1716,"seo":1717,"stem":1718,"__hash__":1719},"content\u002Fexamples\u002Fpython-download-files-from-a-url-example.md","Python Download Files from a URL Example",{"type":7,"value":8,"toc":1684},"minimark",[9,13,17,20,44,49,234,240,244,263,267,270,288,294,312,315,328,332,335,465,470,477,488,494,505,509,526,529,533,556,559,566,570,585,588,605,608,612,647,652,663,667,691,697,704,713,716,730,736,743,747,750,903,906,917,921,924,1237,1240,1254,1258,1261,1267,1270,1294,1297,1309,1313,1316,1319,1367,1370,1474,1477,1488,1495,1499,1502,1505,1529,1533,1536,1539,1550,1559,1563,1566,1577,1580,1583,1587,1594,1600,1611,1616,1620,1633,1637,1640,1644,1680],[10,11,5],"h1",{"id":12},"python-download-files-from-a-url-example",[14,15,16],"p",{},"This beginner-friendly example shows how to download a file from a URL in Python and save it on your computer.",[14,18,19],{},"You will learn how to:",[21,22,23,27,30,38,41],"ul",{},[24,25,26],"li",{},"download a file from a web address",[24,28,29],{},"save it locally",[24,31,32,33,37],{},"use ",[34,35,36],"code",{},"requests.get()"," to fetch the file",[24,39,40],{},"write the file safely with binary mode",[24,42,43],{},"handle common problems like bad URLs and missing packages",[45,46,48],"h2",{"id":47},"quick-example","Quick example",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(\"file.txt\", \"wb\") as file:\n    file.write(response.content)\n\nprint(\"Download complete\")\n","python","",[34,57,58,71,78,99,127,141,146,189,212,217],{"__ignoreMap":55},[59,60,63,67],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sVHd0","import",[59,68,70],{"class":69},"su5hD"," requests\n",[59,72,74],{"class":61,"line":73},2,[59,75,77],{"emptyLinePlaceholder":76},true,"\n",[59,79,81,84,88,92,96],{"class":61,"line":80},3,[59,82,83],{"class":69},"url ",[59,85,87],{"class":86},"smGrS","=",[59,89,91],{"class":90},"sjJ54"," \"",[59,93,95],{"class":94},"s_sjI","https:\u002F\u002Fexample.com\u002Ffile.txt",[59,97,98],{"class":90},"\"\n",[59,100,102,105,107,110,114,118,121,124],{"class":61,"line":101},4,[59,103,104],{"class":69},"response ",[59,106,87],{"class":86},[59,108,109],{"class":69}," requests",[59,111,113],{"class":112},"sP7_E",".",[59,115,117],{"class":116},"slqww","get",[59,119,120],{"class":112},"(",[59,122,123],{"class":116},"url",[59,125,126],{"class":112},")\n",[59,128,130,133,135,138],{"class":61,"line":129},5,[59,131,132],{"class":69},"response",[59,134,113],{"class":112},[59,136,137],{"class":116},"raise_for_status",[59,139,140],{"class":112},"()\n",[59,142,144],{"class":61,"line":143},6,[59,145,77],{"emptyLinePlaceholder":76},[59,147,149,152,156,158,161,164,166,169,171,174,176,179,182,186],{"class":61,"line":148},7,[59,150,151],{"class":65},"with",[59,153,155],{"class":154},"sptTA"," open",[59,157,120],{"class":112},[59,159,160],{"class":90},"\"",[59,162,163],{"class":94},"file.txt",[59,165,160],{"class":90},[59,167,168],{"class":112},",",[59,170,91],{"class":90},[59,172,173],{"class":94},"wb",[59,175,160],{"class":90},[59,177,178],{"class":112},")",[59,180,181],{"class":65}," as",[59,183,185],{"class":184},"sMMDD"," file",[59,187,188],{"class":112},":\n",[59,190,192,195,197,200,202,204,206,210],{"class":61,"line":191},8,[59,193,194],{"class":184},"    file",[59,196,113],{"class":112},[59,198,199],{"class":116},"write",[59,201,120],{"class":112},[59,203,132],{"class":116},[59,205,113],{"class":112},[59,207,209],{"class":208},"skxfh","content",[59,211,126],{"class":112},[59,213,215],{"class":61,"line":214},9,[59,216,77],{"emptyLinePlaceholder":76},[59,218,220,223,225,227,230,232],{"class":61,"line":219},10,[59,221,222],{"class":154},"print",[59,224,120],{"class":112},[59,226,160],{"class":90},[59,228,229],{"class":94},"Download complete",[59,231,160],{"class":90},[59,233,126],{"class":112},[14,235,236,237,239],{},"This is the fastest working example. It downloads the file and saves it as ",[34,238,163],{}," in the current folder.",[45,241,243],{"id":242},"what-this-example-does","What this example does",[21,245,246,249,252,258],{},[24,247,248],{},"Downloads a file from a web address",[24,250,251],{},"Saves the file to your computer",[24,253,254,255,257],{},"Uses ",[34,256,36],{}," to fetch data",[24,259,254,260,262],{},[34,261,173],{}," mode to write binary data safely",[45,264,266],{"id":265},"what-you-need-before-running-it","What you need before running it",[14,268,269],{},"Before you run the script, make sure you have:",[21,271,272,275,282,285],{},[24,273,274],{},"Python installed",[24,276,277,278,281],{},"The ",[34,279,280],{},"requests"," package installed",[24,283,284],{},"A valid file URL",[24,286,287],{},"Permission to write files in the current folder",[14,289,290,291,293],{},"If ",[34,292,280],{}," is not installed, run:",[50,295,299],{"className":296,"code":297,"language":298,"meta":55,"style":55},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","pip install requests\n","bash",[34,300,301],{"__ignoreMap":55},[59,302,303,307,310],{"class":61,"line":62},[59,304,306],{"class":305},"sbgvK","pip",[59,308,309],{"class":94}," install",[59,311,70],{"class":94},[14,313,314],{},"If you are not sure Python is installed correctly, check with:",[50,316,318],{"className":296,"code":317,"language":298,"meta":55,"style":55},"python --version\n",[34,319,320],{"__ignoreMap":55},[59,321,322,324],{"class":61,"line":62},[59,323,54],{"class":305},[59,325,327],{"class":326},"stzsN"," --version\n",[45,329,331],{"id":330},"main-example-download-and-save-a-file","Main example: download and save a file",[14,333,334],{},"Here is the full beginner-friendly version again:",[50,336,338],{"className":52,"code":337,"language":54,"meta":55,"style":55},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\n\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(\"file.txt\", \"wb\") as file:\n    file.write(response.content)\n\nprint(\"Download complete\")\n",[34,339,340,346,350,362,366,384,394,398,428,446,450],{"__ignoreMap":55},[59,341,342,344],{"class":61,"line":62},[59,343,66],{"class":65},[59,345,70],{"class":69},[59,347,348],{"class":61,"line":73},[59,349,77],{"emptyLinePlaceholder":76},[59,351,352,354,356,358,360],{"class":61,"line":80},[59,353,83],{"class":69},[59,355,87],{"class":86},[59,357,91],{"class":90},[59,359,95],{"class":94},[59,361,98],{"class":90},[59,363,364],{"class":61,"line":101},[59,365,77],{"emptyLinePlaceholder":76},[59,367,368,370,372,374,376,378,380,382],{"class":61,"line":129},[59,369,104],{"class":69},[59,371,87],{"class":86},[59,373,109],{"class":69},[59,375,113],{"class":112},[59,377,117],{"class":116},[59,379,120],{"class":112},[59,381,123],{"class":116},[59,383,126],{"class":112},[59,385,386,388,390,392],{"class":61,"line":143},[59,387,132],{"class":69},[59,389,113],{"class":112},[59,391,137],{"class":116},[59,393,140],{"class":112},[59,395,396],{"class":61,"line":148},[59,397,77],{"emptyLinePlaceholder":76},[59,399,400,402,404,406,408,410,412,414,416,418,420,422,424,426],{"class":61,"line":191},[59,401,151],{"class":65},[59,403,155],{"class":154},[59,405,120],{"class":112},[59,407,160],{"class":90},[59,409,163],{"class":94},[59,411,160],{"class":90},[59,413,168],{"class":112},[59,415,91],{"class":90},[59,417,173],{"class":94},[59,419,160],{"class":90},[59,421,178],{"class":112},[59,423,181],{"class":65},[59,425,185],{"class":184},[59,427,188],{"class":112},[59,429,430,432,434,436,438,440,442,444],{"class":61,"line":214},[59,431,194],{"class":184},[59,433,113],{"class":112},[59,435,199],{"class":116},[59,437,120],{"class":112},[59,439,132],{"class":116},[59,441,113],{"class":112},[59,443,209],{"class":208},[59,445,126],{"class":112},[59,447,448],{"class":61,"line":219},[59,449,77],{"emptyLinePlaceholder":76},[59,451,453,455,457,459,461,463],{"class":61,"line":452},11,[59,454,222],{"class":154},[59,456,120],{"class":112},[59,458,160],{"class":90},[59,460,229],{"class":94},[59,462,160],{"class":90},[59,464,126],{"class":112},[466,467,469],"h3",{"id":468},"how-it-works","How it works",[471,472,474,475],"h4",{"id":473},"_1-import-requests","1. Import ",[34,476,280],{},[50,478,480],{"className":52,"code":479,"language":54,"meta":55,"style":55},"import requests\n",[34,481,482],{"__ignoreMap":55},[59,483,484,486],{"class":61,"line":62},[59,485,66],{"class":65},[59,487,70],{"class":69},[14,489,490,491,493],{},"This imports the ",[34,492,280],{}," package so your script can make an HTTP request.",[14,495,496,497,113],{},"If this line gives an error, see ",[498,499,501,502],"a",{"href":500},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","how to fix ",[34,503,504],{},"ModuleNotFoundError: No module named x",[471,506,508],{"id":507},"_2-store-the-url","2. Store the URL",[50,510,512],{"className":52,"code":511,"language":54,"meta":55,"style":55},"url = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\n",[34,513,514],{"__ignoreMap":55},[59,515,516,518,520,522,524],{"class":61,"line":62},[59,517,83],{"class":69},[59,519,87],{"class":86},[59,521,91],{"class":90},[59,523,95],{"class":94},[59,525,98],{"class":90},[14,527,528],{},"Replace this with the direct URL of the file you want to download.",[471,530,532],{"id":531},"_3-send-a-get-request","3. Send a GET request",[50,534,536],{"className":52,"code":535,"language":54,"meta":55,"style":55},"response = requests.get(url)\n",[34,537,538],{"__ignoreMap":55},[59,539,540,542,544,546,548,550,552,554],{"class":61,"line":62},[59,541,104],{"class":69},[59,543,87],{"class":86},[59,545,109],{"class":69},[59,547,113],{"class":112},[59,549,117],{"class":116},[59,551,120],{"class":112},[59,553,123],{"class":116},[59,555,126],{"class":112},[14,557,558],{},"This asks the server for the file.",[14,560,561,562,113],{},"If you want to learn more about requests first, see ",[498,563,565],{"href":564},"\u002Fhow-to\u002Fhow-to-make-an-api-request-in-python","how to make an API request in Python",[471,567,569],{"id":568},"_4-check-for-http-errors","4. Check for HTTP errors",[50,571,573],{"className":52,"code":572,"language":54,"meta":55,"style":55},"response.raise_for_status()\n",[34,574,575],{"__ignoreMap":55},[59,576,577,579,581,583],{"class":61,"line":62},[59,578,132],{"class":69},[59,580,113],{"class":112},[59,582,137],{"class":116},[59,584,140],{"class":112},[14,586,587],{},"This stops the program if the server returns an error such as:",[21,589,590,595,600],{},[24,591,592],{},[34,593,594],{},"404 Not Found",[24,596,597],{},[34,598,599],{},"403 Forbidden",[24,601,602],{},[34,603,604],{},"500 Internal Server Error",[14,606,607],{},"This is better than saving a bad response without noticing.",[471,609,611],{"id":610},"_5-open-a-local-file-in-binary-write-mode","5. Open a local file in binary write mode",[50,613,615],{"className":52,"code":614,"language":54,"meta":55,"style":55},"with open(\"file.txt\", \"wb\") as file:\n",[34,616,617],{"__ignoreMap":55},[59,618,619,621,623,625,627,629,631,633,635,637,639,641,643,645],{"class":61,"line":62},[59,620,151],{"class":65},[59,622,155],{"class":154},[59,624,120],{"class":112},[59,626,160],{"class":90},[59,628,163],{"class":94},[59,630,160],{"class":90},[59,632,168],{"class":112},[59,634,91],{"class":90},[59,636,173],{"class":94},[59,638,160],{"class":90},[59,640,178],{"class":112},[59,642,181],{"class":65},[59,644,185],{"class":184},[59,646,188],{"class":112},[14,648,649,650,239],{},"This creates a local file named ",[34,651,163],{},[14,653,654,655,658,659,113],{},"If you want to understand ",[34,656,657],{},"open()"," better, read ",[498,660,662],{"href":661},"\u002Freference\u002Fpython-open-function-explained","Python open() function explained",[471,664,666],{"id":665},"_6-write-the-downloaded-content","6. Write the downloaded content",[50,668,670],{"className":52,"code":669,"language":54,"meta":55,"style":55},"file.write(response.content)\n",[34,671,672],{"__ignoreMap":55},[59,673,674,677,679,681,683,685,687,689],{"class":61,"line":62},[59,675,676],{"class":184},"file",[59,678,113],{"class":112},[59,680,199],{"class":116},[59,682,120],{"class":112},[59,684,132],{"class":116},[59,686,113],{"class":112},[59,688,209],{"class":208},[59,690,126],{"class":112},[14,692,693,696],{},[34,694,695],{},"response.content"," contains the downloaded bytes. The script writes those bytes into the file.",[45,698,700,701,703],{"id":699},"why-wb-mode-is-used","Why ",[34,702,173],{}," mode is used",[14,705,706,708,709,113],{},[34,707,173],{}," means ",[710,711,712],"strong",{},"write binary",[14,714,715],{},"This matters because downloaded files are often not plain text. Binary mode works safely for:",[21,717,718,721,724,727],{},[24,719,720],{},"images",[24,722,723],{},"PDFs",[24,725,726],{},"ZIP files",[24,728,729],{},"text files",[14,731,732,733,735],{},"Using ",[34,734,173],{}," helps avoid file corruption, especially for non-text files.",[14,737,738,739,113],{},"If you want a broader introduction to reading and writing files, see ",[498,740,742],{"href":741},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write","Python file handling basics: read and write",[45,744,746],{"id":745},"example-with-a-custom-filename","Example with a custom filename",[14,748,749],{},"You do not have to save the file with the same name as the URL. You can choose your own filename.",[50,751,753],{"className":52,"code":752,"language":54,"meta":55,"style":55},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\noutput_file = \"downloaded_file.txt\"\n\nresponse = requests.get(url)\nresponse.raise_for_status()\n\nwith open(output_file, \"wb\") as file:\n    file.write(response.content)\n\nprint(f\"Saved as {output_file}\")\n",[34,754,755,761,765,777,791,795,813,823,827,854,872,876],{"__ignoreMap":55},[59,756,757,759],{"class":61,"line":62},[59,758,66],{"class":65},[59,760,70],{"class":69},[59,762,763],{"class":61,"line":73},[59,764,77],{"emptyLinePlaceholder":76},[59,766,767,769,771,773,775],{"class":61,"line":80},[59,768,83],{"class":69},[59,770,87],{"class":86},[59,772,91],{"class":90},[59,774,95],{"class":94},[59,776,98],{"class":90},[59,778,779,782,784,786,789],{"class":61,"line":101},[59,780,781],{"class":69},"output_file ",[59,783,87],{"class":86},[59,785,91],{"class":90},[59,787,788],{"class":94},"downloaded_file.txt",[59,790,98],{"class":90},[59,792,793],{"class":61,"line":129},[59,794,77],{"emptyLinePlaceholder":76},[59,796,797,799,801,803,805,807,809,811],{"class":61,"line":143},[59,798,104],{"class":69},[59,800,87],{"class":86},[59,802,109],{"class":69},[59,804,113],{"class":112},[59,806,117],{"class":116},[59,808,120],{"class":112},[59,810,123],{"class":116},[59,812,126],{"class":112},[59,814,815,817,819,821],{"class":61,"line":148},[59,816,132],{"class":69},[59,818,113],{"class":112},[59,820,137],{"class":116},[59,822,140],{"class":112},[59,824,825],{"class":61,"line":191},[59,826,77],{"emptyLinePlaceholder":76},[59,828,829,831,833,835,838,840,842,844,846,848,850,852],{"class":61,"line":214},[59,830,151],{"class":65},[59,832,155],{"class":154},[59,834,120],{"class":112},[59,836,837],{"class":116},"output_file",[59,839,168],{"class":112},[59,841,91],{"class":90},[59,843,173],{"class":94},[59,845,160],{"class":90},[59,847,178],{"class":112},[59,849,181],{"class":65},[59,851,185],{"class":184},[59,853,188],{"class":112},[59,855,856,858,860,862,864,866,868,870],{"class":61,"line":219},[59,857,194],{"class":184},[59,859,113],{"class":112},[59,861,199],{"class":116},[59,863,120],{"class":112},[59,865,132],{"class":116},[59,867,113],{"class":112},[59,869,209],{"class":208},[59,871,126],{"class":112},[59,873,874],{"class":61,"line":452},[59,875,77],{"emptyLinePlaceholder":76},[59,877,879,881,883,887,890,894,896,899,901],{"class":61,"line":878},12,[59,880,222],{"class":154},[59,882,120],{"class":112},[59,884,886],{"class":885},"sbsja","f",[59,888,889],{"class":94},"\"Saved as ",[59,891,893],{"class":892},"srdBf","{",[59,895,837],{"class":116},[59,897,898],{"class":892},"}",[59,900,160],{"class":94},[59,902,126],{"class":112},[14,904,905],{},"This version:",[21,907,908,911,914],{},[24,909,910],{},"uses a custom output filename",[24,912,913],{},"saves the file in the current folder",[24,915,916],{},"keeps the path simple and easy to test",[45,918,920],{"id":919},"basic-error-handling","Basic error handling",[14,922,923],{},"A beginner script is better if it shows clear errors instead of crashing.",[50,925,927],{"className":52,"code":926,"language":54,"meta":55,"style":55},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\noutput_file = \"file.txt\"\n\ntry:\n    response = requests.get(url)\n    response.raise_for_status()\n\n    with open(output_file, \"wb\") as file:\n        file.write(response.content)\n\n    print(\"Download complete\")\n\nexcept requests.exceptions.MissingSchema:\n    print(\"Invalid URL. Did you forget https:\u002F\u002F ?\")\n\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problem. Check your internet or the website address.\")\n\nexcept requests.exceptions.HTTPError as error:\n    print(f\"HTTP error: {error}\")\n\nexcept PermissionError:\n    print(\"Permission denied. Try saving to a different folder.\")\n",[34,928,929,935,939,951,963,967,974,993,1004,1008,1035,1054,1058,1074,1079,1099,1115,1120,1138,1154,1159,1182,1205,1210,1221],{"__ignoreMap":55},[59,930,931,933],{"class":61,"line":62},[59,932,66],{"class":65},[59,934,70],{"class":69},[59,936,937],{"class":61,"line":73},[59,938,77],{"emptyLinePlaceholder":76},[59,940,941,943,945,947,949],{"class":61,"line":80},[59,942,83],{"class":69},[59,944,87],{"class":86},[59,946,91],{"class":90},[59,948,95],{"class":94},[59,950,98],{"class":90},[59,952,953,955,957,959,961],{"class":61,"line":101},[59,954,781],{"class":69},[59,956,87],{"class":86},[59,958,91],{"class":90},[59,960,163],{"class":94},[59,962,98],{"class":90},[59,964,965],{"class":61,"line":129},[59,966,77],{"emptyLinePlaceholder":76},[59,968,969,972],{"class":61,"line":143},[59,970,971],{"class":65},"try",[59,973,188],{"class":112},[59,975,976,979,981,983,985,987,989,991],{"class":61,"line":148},[59,977,978],{"class":69},"    response ",[59,980,87],{"class":86},[59,982,109],{"class":69},[59,984,113],{"class":112},[59,986,117],{"class":116},[59,988,120],{"class":112},[59,990,123],{"class":116},[59,992,126],{"class":112},[59,994,995,998,1000,1002],{"class":61,"line":191},[59,996,997],{"class":69},"    response",[59,999,113],{"class":112},[59,1001,137],{"class":116},[59,1003,140],{"class":112},[59,1005,1006],{"class":61,"line":214},[59,1007,77],{"emptyLinePlaceholder":76},[59,1009,1010,1013,1015,1017,1019,1021,1023,1025,1027,1029,1031,1033],{"class":61,"line":219},[59,1011,1012],{"class":65},"    with",[59,1014,155],{"class":154},[59,1016,120],{"class":112},[59,1018,837],{"class":116},[59,1020,168],{"class":112},[59,1022,91],{"class":90},[59,1024,173],{"class":94},[59,1026,160],{"class":90},[59,1028,178],{"class":112},[59,1030,181],{"class":65},[59,1032,185],{"class":184},[59,1034,188],{"class":112},[59,1036,1037,1040,1042,1044,1046,1048,1050,1052],{"class":61,"line":452},[59,1038,1039],{"class":184},"        file",[59,1041,113],{"class":112},[59,1043,199],{"class":116},[59,1045,120],{"class":112},[59,1047,132],{"class":116},[59,1049,113],{"class":112},[59,1051,209],{"class":208},[59,1053,126],{"class":112},[59,1055,1056],{"class":61,"line":878},[59,1057,77],{"emptyLinePlaceholder":76},[59,1059,1061,1064,1066,1068,1070,1072],{"class":61,"line":1060},13,[59,1062,1063],{"class":154},"    print",[59,1065,120],{"class":112},[59,1067,160],{"class":90},[59,1069,229],{"class":94},[59,1071,160],{"class":90},[59,1073,126],{"class":112},[59,1075,1077],{"class":61,"line":1076},14,[59,1078,77],{"emptyLinePlaceholder":76},[59,1080,1082,1085,1087,1089,1092,1094,1097],{"class":61,"line":1081},15,[59,1083,1084],{"class":65},"except",[59,1086,109],{"class":69},[59,1088,113],{"class":112},[59,1090,1091],{"class":208},"exceptions",[59,1093,113],{"class":112},[59,1095,1096],{"class":208},"MissingSchema",[59,1098,188],{"class":112},[59,1100,1102,1104,1106,1108,1111,1113],{"class":61,"line":1101},16,[59,1103,1063],{"class":154},[59,1105,120],{"class":112},[59,1107,160],{"class":90},[59,1109,1110],{"class":94},"Invalid URL. Did you forget https:\u002F\u002F ?",[59,1112,160],{"class":90},[59,1114,126],{"class":112},[59,1116,1118],{"class":61,"line":1117},17,[59,1119,77],{"emptyLinePlaceholder":76},[59,1121,1123,1125,1127,1129,1131,1133,1136],{"class":61,"line":1122},18,[59,1124,1084],{"class":65},[59,1126,109],{"class":69},[59,1128,113],{"class":112},[59,1130,1091],{"class":208},[59,1132,113],{"class":112},[59,1134,1135],{"class":208},"ConnectionError",[59,1137,188],{"class":112},[59,1139,1141,1143,1145,1147,1150,1152],{"class":61,"line":1140},19,[59,1142,1063],{"class":154},[59,1144,120],{"class":112},[59,1146,160],{"class":90},[59,1148,1149],{"class":94},"Connection problem. Check your internet or the website address.",[59,1151,160],{"class":90},[59,1153,126],{"class":112},[59,1155,1157],{"class":61,"line":1156},20,[59,1158,77],{"emptyLinePlaceholder":76},[59,1160,1162,1164,1166,1168,1170,1172,1175,1177,1180],{"class":61,"line":1161},21,[59,1163,1084],{"class":65},[59,1165,109],{"class":69},[59,1167,113],{"class":112},[59,1169,1091],{"class":208},[59,1171,113],{"class":112},[59,1173,1174],{"class":208},"HTTPError",[59,1176,181],{"class":65},[59,1178,1179],{"class":69}," error",[59,1181,188],{"class":112},[59,1183,1185,1187,1189,1191,1194,1196,1199,1201,1203],{"class":61,"line":1184},22,[59,1186,1063],{"class":154},[59,1188,120],{"class":112},[59,1190,886],{"class":885},[59,1192,1193],{"class":94},"\"HTTP error: ",[59,1195,893],{"class":892},[59,1197,1198],{"class":116},"error",[59,1200,898],{"class":892},[59,1202,160],{"class":94},[59,1204,126],{"class":112},[59,1206,1208],{"class":61,"line":1207},23,[59,1209,77],{"emptyLinePlaceholder":76},[59,1211,1213,1215,1219],{"class":61,"line":1212},24,[59,1214,1084],{"class":65},[59,1216,1218],{"class":1217},"sZMiF"," PermissionError",[59,1220,188],{"class":112},[59,1222,1224,1226,1228,1230,1233,1235],{"class":61,"line":1223},25,[59,1225,1063],{"class":154},[59,1227,120],{"class":112},[59,1229,160],{"class":90},[59,1231,1232],{"class":94},"Permission denied. Try saving to a different folder.",[59,1234,160],{"class":90},[59,1236,126],{"class":112},[14,1238,1239],{},"This helps handle common problems such as:",[21,1241,1242,1245,1248,1251],{},[24,1243,1244],{},"invalid URLs",[24,1246,1247],{},"connection failures",[24,1249,1250],{},"HTTP errors like 404",[24,1252,1253],{},"file permission problems",[45,1255,1257],{"id":1256},"common-beginner-problems","Common beginner problems",[14,1259,1260],{},"Here are the most common issues when downloading files in Python.",[466,1262,1264,1266],{"id":1263},"requests-is-not-installed",[34,1265,280],{}," is not installed",[14,1268,1269],{},"You may see an error like:",[50,1271,1273],{"className":52,"code":1272,"language":54,"meta":55,"style":55},"ModuleNotFoundError: No module named 'requests'\n",[34,1274,1275],{"__ignoreMap":55},[59,1276,1277,1280,1283,1286,1289,1291],{"class":61,"line":62},[59,1278,1279],{"class":1217},"ModuleNotFoundError",[59,1281,1282],{"class":112},":",[59,1284,1285],{"class":69}," No module named ",[59,1287,1288],{"class":90},"'",[59,1290,280],{"class":94},[59,1292,1293],{"class":90},"'\n",[14,1295,1296],{},"Install it with:",[50,1298,1299],{"className":296,"code":297,"language":298,"meta":55,"style":55},[34,1300,1301],{"__ignoreMap":55},[59,1302,1303,1305,1307],{"class":61,"line":62},[59,1304,306],{"class":305},[59,1306,309],{"class":94},[59,1308,70],{"class":94},[466,1310,1312],{"id":1311},"the-url-opens-a-web-page-instead-of-a-file","The URL opens a web page instead of a file",[14,1314,1315],{},"Sometimes the URL does not point directly to a file. It may point to a normal web page instead.",[14,1317,1318],{},"You can debug this with:",[50,1320,1322],{"className":52,"code":1321,"language":54,"meta":55,"style":55},"print(response.url)\nprint(response.headers.get(\"content-type\"))\n",[34,1323,1324,1338],{"__ignoreMap":55},[59,1325,1326,1328,1330,1332,1334,1336],{"class":61,"line":62},[59,1327,222],{"class":154},[59,1329,120],{"class":112},[59,1331,132],{"class":116},[59,1333,113],{"class":112},[59,1335,123],{"class":208},[59,1337,126],{"class":112},[59,1339,1340,1342,1344,1346,1348,1351,1353,1355,1357,1359,1362,1364],{"class":61,"line":73},[59,1341,222],{"class":154},[59,1343,120],{"class":112},[59,1345,132],{"class":116},[59,1347,113],{"class":112},[59,1349,1350],{"class":208},"headers",[59,1352,113],{"class":112},[59,1354,117],{"class":116},[59,1356,120],{"class":112},[59,1358,160],{"class":90},[59,1360,1361],{"class":94},"content-type",[59,1363,160],{"class":90},[59,1365,1366],{"class":112},"))\n",[14,1368,1369],{},"Example:",[50,1371,1373],{"className":52,"code":1372,"language":54,"meta":55,"style":55},"import requests\n\nurl = \"https:\u002F\u002Fexample.com\u002Ffile.txt\"\nresponse = requests.get(url)\n\nprint(response.status_code)\nprint(response.url)\nprint(response.headers.get(\"content-type\"))\n",[34,1374,1375,1381,1385,1397,1415,1419,1434,1448],{"__ignoreMap":55},[59,1376,1377,1379],{"class":61,"line":62},[59,1378,66],{"class":65},[59,1380,70],{"class":69},[59,1382,1383],{"class":61,"line":73},[59,1384,77],{"emptyLinePlaceholder":76},[59,1386,1387,1389,1391,1393,1395],{"class":61,"line":80},[59,1388,83],{"class":69},[59,1390,87],{"class":86},[59,1392,91],{"class":90},[59,1394,95],{"class":94},[59,1396,98],{"class":90},[59,1398,1399,1401,1403,1405,1407,1409,1411,1413],{"class":61,"line":101},[59,1400,104],{"class":69},[59,1402,87],{"class":86},[59,1404,109],{"class":69},[59,1406,113],{"class":112},[59,1408,117],{"class":116},[59,1410,120],{"class":112},[59,1412,123],{"class":116},[59,1414,126],{"class":112},[59,1416,1417],{"class":61,"line":129},[59,1418,77],{"emptyLinePlaceholder":76},[59,1420,1421,1423,1425,1427,1429,1432],{"class":61,"line":143},[59,1422,222],{"class":154},[59,1424,120],{"class":112},[59,1426,132],{"class":116},[59,1428,113],{"class":112},[59,1430,1431],{"class":208},"status_code",[59,1433,126],{"class":112},[59,1435,1436,1438,1440,1442,1444,1446],{"class":61,"line":148},[59,1437,222],{"class":154},[59,1439,120],{"class":112},[59,1441,132],{"class":116},[59,1443,113],{"class":112},[59,1445,123],{"class":208},[59,1447,126],{"class":112},[59,1449,1450,1452,1454,1456,1458,1460,1462,1464,1466,1468,1470,1472],{"class":61,"line":191},[59,1451,222],{"class":154},[59,1453,120],{"class":112},[59,1455,132],{"class":116},[59,1457,113],{"class":112},[59,1459,1350],{"class":208},[59,1461,113],{"class":112},[59,1463,117],{"class":116},[59,1465,120],{"class":112},[59,1467,160],{"class":90},[59,1469,1361],{"class":94},[59,1471,160],{"class":90},[59,1473,1366],{"class":112},[14,1475,1476],{},"This helps you see:",[21,1478,1479,1482,1485],{},[24,1480,1481],{},"the final URL after redirects",[24,1483,1484],{},"the status code",[24,1486,1487],{},"the content type returned by the server",[14,1489,1490,1491,1494],{},"If the content type is ",[34,1492,1493],{},"text\u002Fhtml",", you may be downloading a page instead of the file you expected.",[466,1496,1498],{"id":1497},"the-saved-file-has-the-wrong-name-or-extension","The saved file has the wrong name or extension",[14,1500,1501],{},"Make sure your output filename matches the type of file you are downloading.",[14,1503,1504],{},"For example:",[21,1506,1507,1517,1523],{},[24,1508,1509,1510,1513,1514],{},"image data should usually be saved as ",[34,1511,1512],{},".jpg"," or ",[34,1515,1516],{},".png",[24,1518,1519,1520],{},"PDF data should usually be saved as ",[34,1521,1522],{},".pdf",[24,1524,1525,1526],{},"ZIP files should usually be saved as ",[34,1527,1528],{},".zip",[466,1530,1532],{"id":1531},"the-script-has-no-permission-to-save-the-file","The script has no permission to save the file",[14,1534,1535],{},"If Python cannot write to the folder, you may get a permission error.",[14,1537,1538],{},"In that case:",[21,1540,1541,1544,1547],{},[24,1542,1543],{},"save to a different folder",[24,1545,1546],{},"use a simpler location",[24,1548,1549],{},"check folder permissions",[14,1551,1552,1553,113],{},"For help, see ",[498,1554,501,1556],{"href":1555},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix",[34,1557,1558],{},"PermissionError: [Errno 13] Permission denied",[45,1560,1562],{"id":1561},"when-to-use-this-approach","When to use this approach",[14,1564,1565],{},"This approach is a good choice when you want:",[21,1567,1568,1571,1574],{},[24,1569,1570],{},"a simple download script",[24,1572,1573],{},"basic automation",[24,1575,1576],{},"a clear starting point for file download tasks",[14,1578,1579],{},"It is especially useful for beginners because the code is short and easy to test.",[14,1581,1582],{},"For very large files, a streamed download is usually better because it avoids loading the whole file into memory at once.",[45,1584,1586],{"id":1585},"faq","FAQ",[466,1588,1590,1591,1593],{"id":1589},"do-i-need-requests-to-download-a-file-in-python","Do I need ",[34,1592,280],{}," to download a file in Python?",[14,1595,1596,1597,1599],{},"No. Python can also do this with built-in modules. But ",[34,1598,280],{}," is one of the easiest options for beginners because the code is short and readable.",[466,1601,1603,1604,1606,1607,1610],{"id":1602},"why-do-i-use-wb-instead-of-w","Why do I use ",[34,1605,173],{}," instead of ",[34,1608,1609],{},"w","?",[14,1612,1613,1615],{},[34,1614,173],{}," writes binary data. It is safer for files like images, PDFs, and ZIP files.",[466,1617,1619],{"id":1618},"what-if-the-download-url-returns-an-error","What if the download URL returns an error?",[14,1621,1622,1623,1626,1627,1629,1630,1632],{},"Use ",[34,1624,1625],{},"response.raise_for_status()"," and wrap the request in ",[34,1628,971],{},"\u002F",[34,1631,1084],{}," so you can show a helpful message.",[466,1634,1636],{"id":1635},"can-this-download-large-files","Can this download large files?",[14,1638,1639],{},"Yes, but for very large files, a streamed download is better than loading everything into memory at once.",[45,1641,1643],{"id":1642},"see-also","See also",[21,1645,1646,1651,1655,1659,1666,1674],{},[24,1647,1648],{},[498,1649,1650],{"href":564},"How to make an API request in Python",[24,1652,1653],{},[498,1654,662],{"href":661},[24,1656,1657],{},[498,1658,742],{"href":741},[24,1660,1661],{},[498,1662,1663,1664],{"href":500},"Fix ",[34,1665,504],{},[24,1667,1668],{},[498,1669,1663,1671],{"href":1670},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix",[34,1672,1673],{},"FileNotFoundError: [Errno 2] No such file or directory",[24,1675,1676],{},[498,1677,1663,1678],{"href":1555},[34,1679,1558],{},[1681,1682,1683],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":55,"searchDepth":73,"depth":73,"links":1685},[1686,1687,1688,1689,1692,1694,1695,1696,1703,1704,1712],{"id":47,"depth":73,"text":48},{"id":242,"depth":73,"text":243},{"id":265,"depth":73,"text":266},{"id":330,"depth":73,"text":331,"children":1690},[1691],{"id":468,"depth":80,"text":469},{"id":699,"depth":73,"text":1693},"Why wb mode is used",{"id":745,"depth":73,"text":746},{"id":919,"depth":73,"text":920},{"id":1256,"depth":73,"text":1257,"children":1697},[1698,1700,1701,1702],{"id":1263,"depth":80,"text":1699},"requests is not installed",{"id":1311,"depth":80,"text":1312},{"id":1497,"depth":80,"text":1498},{"id":1531,"depth":80,"text":1532},{"id":1561,"depth":73,"text":1562},{"id":1585,"depth":73,"text":1586,"children":1705},[1706,1708,1710,1711],{"id":1589,"depth":80,"text":1707},"Do I need requests to download a file in Python?",{"id":1602,"depth":80,"text":1709},"Why do I use wb instead of w?",{"id":1618,"depth":80,"text":1619},{"id":1635,"depth":80,"text":1636},{"id":1642,"depth":73,"text":1643},"Master python download files from a url example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-download-files-from-a-url-example",{"title":5,"description":1713},"examples\u002Fpython-download-files-from-a-url-example","GqQGrzFoQjAnO_ZpPnvYHTq4ql01j_3TrC2DzkiITxg",1777585475040]