[{"data":1,"prerenderedAt":1953},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python":3},{"id":4,"title":5,"body":6,"description":1946,"extension":1947,"meta":1948,"navigation":1001,"path":1949,"seo":1950,"stem":1951,"__hash__":1952},"content\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python.md","How to Write to a File in Python",{"type":7,"value":8,"toc":1902},"minimark",[9,13,22,25,41,46,135,141,148,152,166,170,173,227,232,255,258,262,268,276,287,291,294,329,332,350,353,378,381,393,399,403,406,412,468,479,482,488,544,554,561,567,624,632,635,638,642,648,748,751,757,760,823,826,835,839,844,847,899,909,913,920,976,979,1053,1057,1063,1066,1080,1083,1140,1146,1154,1158,1161,1165,1168,1176,1178,1234,1239,1242,1249,1253,1255,1262,1264,1336,1339,1345,1347,1422,1429,1431,1438,1440,1509,1513,1515,1523,1525,1582,1585,1596,1600,1602,1607,1610,1621,1625,1628,1650,1653,1754,1758,1784,1791,1795,1802,1811,1820,1828,1835,1843,1847,1856,1860,1866,1870,1898],[10,11,5],"h1",{"id":12},"how-to-write-to-a-file-in-python",[14,15,16,17,21],"p",{},"If you want to save text from your Python program into a file, use the built-in ",[18,19,20],"code",{},"open()"," function.",[14,23,24],{},"This page shows the simplest way to:",[26,27,28,32,35,38],"ul",{},[29,30,31],"li",{},"write text to a file",[29,33,34],{},"create a file if it does not exist",[29,36,37],{},"choose the right file mode",[29,39,40],{},"avoid overwriting a file by mistake",[42,43,45],"h2",{"id":44},"quick-answer","Quick answer",[47,48,53],"pre",{"className":49,"code":50,"language":51,"meta":52,"style":52},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","with open(\"notes.txt\", \"w\") as file:\n    file.write(\"Hello, world!\\n\")\n","python","",[18,54,55,106],{"__ignoreMap":52},[56,57,60,64,68,72,76,80,82,85,88,91,93,96,99,103],"span",{"class":58,"line":59},"line",1,[56,61,63],{"class":62},"sVHd0","with",[56,65,67],{"class":66},"sptTA"," open",[56,69,71],{"class":70},"sP7_E","(",[56,73,75],{"class":74},"sjJ54","\"",[56,77,79],{"class":78},"s_sjI","notes.txt",[56,81,75],{"class":74},[56,83,84],{"class":70},",",[56,86,87],{"class":74}," \"",[56,89,90],{"class":78},"w",[56,92,75],{"class":74},[56,94,95],{"class":70},")",[56,97,98],{"class":62}," as",[56,100,102],{"class":101},"sMMDD"," file",[56,104,105],{"class":70},":\n",[56,107,109,112,115,119,121,123,126,130,132],{"class":58,"line":108},2,[56,110,111],{"class":101},"    file",[56,113,114],{"class":70},".",[56,116,118],{"class":117},"slqww","write",[56,120,71],{"class":70},[56,122,75],{"class":74},[56,124,125],{"class":78},"Hello, world!",[56,127,129],{"class":128},"s_hVV","\\n",[56,131,75],{"class":74},[56,133,134],{"class":70},")\n",[14,136,137,138,140],{},"This creates ",[18,139,79],{}," if it does not exist.",[14,142,143,144,147],{},"The ",[18,145,146],{},"\"w\""," mode overwrites the file if it already exists.",[42,149,151],{"id":150},"what-this-page-helps-you-do","What this page helps you do",[26,153,154,157,160,163],{},[29,155,156],{},"Write text to a file in Python",[29,158,159],{},"Create a new file if it does not exist",[29,161,162],{},"Understand the difference between write modes",[29,164,165],{},"Avoid overwriting a file by mistake",[42,167,169],{"id":168},"basic-way-to-write-to-a-file","Basic way to write to a file",[14,171,172],{},"The most common pattern is:",[47,174,175],{"className":49,"code":50,"language":51,"meta":52,"style":52},[18,176,177,207],{"__ignoreMap":52},[56,178,179,181,183,185,187,189,191,193,195,197,199,201,203,205],{"class":58,"line":59},[56,180,63],{"class":62},[56,182,67],{"class":66},[56,184,71],{"class":70},[56,186,75],{"class":74},[56,188,79],{"class":78},[56,190,75],{"class":74},[56,192,84],{"class":70},[56,194,87],{"class":74},[56,196,90],{"class":78},[56,198,75],{"class":74},[56,200,95],{"class":70},[56,202,98],{"class":62},[56,204,102],{"class":101},[56,206,105],{"class":70},[56,208,209,211,213,215,217,219,221,223,225],{"class":58,"line":108},[56,210,111],{"class":101},[56,212,114],{"class":70},[56,214,118],{"class":117},[56,216,71],{"class":70},[56,218,75],{"class":74},[56,220,125],{"class":78},[56,222,129],{"class":128},[56,224,75],{"class":74},[56,226,134],{"class":70},[228,229,231],"h3",{"id":230},"how-it-works","How it works",[26,233,234,239,244,250],{},[29,235,236,238],{},[18,237,20],{}," opens the file",[29,240,241,243],{},[18,242,146],{}," means write mode",[29,245,246,249],{},[18,247,248],{},"write()"," writes text into the file",[29,251,252,254],{},[18,253,63],{}," closes the file automatically when you are done",[14,256,257],{},"This is the safest beginner-friendly way to write to a file.",[228,259,261],{"id":260},"expected-result","Expected result",[14,263,264,265,267],{},"After running the code, the file ",[18,266,79],{}," will contain:",[47,269,274],{"className":270,"code":272,"language":273,"meta":52},[271],"language-text","Hello, world!\n","text",[18,275,272],{"__ignoreMap":52},[14,277,278,279,281,282,114],{},"If you want to understand ",[18,280,20],{}," in more detail, see ",[283,284,286],"a",{"href":285},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[42,288,290],{"id":289},"what-the-key-parts-mean","What the key parts mean",[14,292,293],{},"Look at this line again:",[47,295,297],{"className":49,"code":296,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"w\") as file:\n",[18,298,299],{"__ignoreMap":52},[56,300,301,303,305,307,309,311,313,315,317,319,321,323,325,327],{"class":58,"line":59},[56,302,63],{"class":62},[56,304,67],{"class":66},[56,306,71],{"class":70},[56,308,75],{"class":74},[56,310,79],{"class":78},[56,312,75],{"class":74},[56,314,84],{"class":70},[56,316,87],{"class":74},[56,318,90],{"class":78},[56,320,75],{"class":74},[56,322,95],{"class":70},[56,324,98],{"class":62},[56,326,102],{"class":101},[56,328,105],{"class":70},[14,330,331],{},"Here is what each part means:",[26,333,334,340,344],{},[29,335,336,339],{},[18,337,338],{},"\"notes.txt\""," is the file name",[29,341,342,243],{},[18,343,146],{},[29,345,346,349],{},[18,347,348],{},"file"," is the variable that refers to the opened file",[14,351,352],{},"Then this line writes the text:",[47,354,356],{"className":49,"code":355,"language":51,"meta":52,"style":52},"file.write(\"Hello, world!\\n\")\n",[18,357,358],{"__ignoreMap":52},[56,359,360,362,364,366,368,370,372,374,376],{"class":58,"line":59},[56,361,348],{"class":101},[56,363,114],{"class":70},[56,365,118],{"class":117},[56,367,71],{"class":70},[56,369,75],{"class":74},[56,371,125],{"class":78},[56,373,129],{"class":128},[56,375,75],{"class":74},[56,377,134],{"class":70},[14,379,380],{},"Key points:",[26,382,383,388],{},[29,384,385,387],{},[18,386,248],{}," writes a string to the file",[29,389,390,392],{},[18,391,129],{}," adds a new line",[14,394,395,396,398],{},"Without ",[18,397,129],{},", the next text you write will continue on the same line.",[42,400,402],{"id":401},"common-file-modes-for-writing","Common file modes for writing",[14,404,405],{},"When writing to files, these modes are the most useful:",[228,407,409,411],{"id":408},"w-mode",[18,410,146],{}," mode",[47,413,415],{"className":49,"code":414,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"First line\\n\")\n",[18,416,417,447],{"__ignoreMap":52},[56,418,419,421,423,425,427,429,431,433,435,437,439,441,443,445],{"class":58,"line":59},[56,420,63],{"class":62},[56,422,67],{"class":66},[56,424,71],{"class":70},[56,426,75],{"class":74},[56,428,79],{"class":78},[56,430,75],{"class":74},[56,432,84],{"class":70},[56,434,87],{"class":74},[56,436,90],{"class":78},[56,438,75],{"class":74},[56,440,95],{"class":70},[56,442,98],{"class":62},[56,444,102],{"class":101},[56,446,105],{"class":70},[56,448,449,451,453,455,457,459,462,464,466],{"class":58,"line":108},[56,450,111],{"class":101},[56,452,114],{"class":70},[56,454,118],{"class":117},[56,456,71],{"class":70},[56,458,75],{"class":74},[56,460,461],{"class":78},"First line",[56,463,129],{"class":128},[56,465,75],{"class":74},[56,467,134],{"class":70},[26,469,470,473,476],{},[29,471,472],{},"Writes from the start of the file",[29,474,475],{},"Replaces old content if the file already exists",[29,477,478],{},"Creates the file if it does not exist",[14,480,481],{},"Use this when you want to replace everything in the file.",[228,483,485,411],{"id":484},"a-mode",[18,486,487],{},"\"a\"",[47,489,491],{"className":49,"code":490,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"a\") as file:\n    file.write(\"Another line\\n\")\n",[18,492,493,523],{"__ignoreMap":52},[56,494,495,497,499,501,503,505,507,509,511,513,515,517,519,521],{"class":58,"line":59},[56,496,63],{"class":62},[56,498,67],{"class":66},[56,500,71],{"class":70},[56,502,75],{"class":74},[56,504,79],{"class":78},[56,506,75],{"class":74},[56,508,84],{"class":70},[56,510,87],{"class":74},[56,512,283],{"class":78},[56,514,75],{"class":74},[56,516,95],{"class":70},[56,518,98],{"class":62},[56,520,102],{"class":101},[56,522,105],{"class":70},[56,524,525,527,529,531,533,535,538,540,542],{"class":58,"line":108},[56,526,111],{"class":101},[56,528,114],{"class":70},[56,530,118],{"class":117},[56,532,71],{"class":70},[56,534,75],{"class":74},[56,536,537],{"class":78},"Another line",[56,539,129],{"class":128},[56,541,75],{"class":74},[56,543,134],{"class":70},[26,545,546,549,552],{},[29,547,548],{},"Adds new content to the end of the file",[29,550,551],{},"Keeps existing content",[29,553,478],{},[14,555,556,557,114],{},"Use this when you want to keep what is already there. For more on this, see ",[283,558,560],{"href":559},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","how to append to a file in Python",[228,562,564,411],{"id":563},"x-mode",[18,565,566],{},"\"x\"",[47,568,570],{"className":49,"code":569,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"x\") as file:\n    file.write(\"New file created\\n\")\n",[18,571,572,603],{"__ignoreMap":52},[56,573,574,576,578,580,582,584,586,588,590,593,595,597,599,601],{"class":58,"line":59},[56,575,63],{"class":62},[56,577,67],{"class":66},[56,579,71],{"class":70},[56,581,75],{"class":74},[56,583,79],{"class":78},[56,585,75],{"class":74},[56,587,84],{"class":70},[56,589,87],{"class":74},[56,591,592],{"class":78},"x",[56,594,75],{"class":74},[56,596,95],{"class":70},[56,598,98],{"class":62},[56,600,102],{"class":101},[56,602,105],{"class":70},[56,604,605,607,609,611,613,615,618,620,622],{"class":58,"line":108},[56,606,111],{"class":101},[56,608,114],{"class":70},[56,610,118],{"class":117},[56,612,71],{"class":70},[56,614,75],{"class":74},[56,616,617],{"class":78},"New file created",[56,619,129],{"class":128},[56,621,75],{"class":74},[56,623,134],{"class":70},[26,625,626,629],{},[29,627,628],{},"Creates a new file",[29,630,631],{},"Fails if the file already exists",[14,633,634],{},"Use this when you only want to create a brand-new file.",[14,636,637],{},"Choose the mode based on whether you want to replace old content or keep it.",[42,639,641],{"id":640},"writing-multiple-lines","Writing multiple lines",[14,643,644,645,647],{},"You can call ",[18,646,248],{}," more than once:",[47,649,651],{"className":49,"code":650,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"Line 1\\n\")\n    file.write(\"Line 2\\n\")\n    file.write(\"Line 3\\n\")\n",[18,652,653,683,704,726],{"__ignoreMap":52},[56,654,655,657,659,661,663,665,667,669,671,673,675,677,679,681],{"class":58,"line":59},[56,656,63],{"class":62},[56,658,67],{"class":66},[56,660,71],{"class":70},[56,662,75],{"class":74},[56,664,79],{"class":78},[56,666,75],{"class":74},[56,668,84],{"class":70},[56,670,87],{"class":74},[56,672,90],{"class":78},[56,674,75],{"class":74},[56,676,95],{"class":70},[56,678,98],{"class":62},[56,680,102],{"class":101},[56,682,105],{"class":70},[56,684,685,687,689,691,693,695,698,700,702],{"class":58,"line":108},[56,686,111],{"class":101},[56,688,114],{"class":70},[56,690,118],{"class":117},[56,692,71],{"class":70},[56,694,75],{"class":74},[56,696,697],{"class":78},"Line 1",[56,699,129],{"class":128},[56,701,75],{"class":74},[56,703,134],{"class":70},[56,705,707,709,711,713,715,717,720,722,724],{"class":58,"line":706},3,[56,708,111],{"class":101},[56,710,114],{"class":70},[56,712,118],{"class":117},[56,714,71],{"class":70},[56,716,75],{"class":74},[56,718,719],{"class":78},"Line 2",[56,721,129],{"class":128},[56,723,75],{"class":74},[56,725,134],{"class":70},[56,727,729,731,733,735,737,739,742,744,746],{"class":58,"line":728},4,[56,730,111],{"class":101},[56,732,114],{"class":70},[56,734,118],{"class":117},[56,736,71],{"class":70},[56,738,75],{"class":74},[56,740,741],{"class":78},"Line 3",[56,743,129],{"class":128},[56,745,75],{"class":74},[56,747,134],{"class":70},[14,749,750],{},"The file will contain:",[47,752,755],{"className":753,"code":754,"language":273,"meta":52},[271],"Line 1\nLine 2\nLine 3\n",[18,756,754],{"__ignoreMap":52},[14,758,759],{},"You can also write one longer string:",[47,761,763],{"className":49,"code":762,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"Line 1\\nLine 2\\nLine 3\\n\")\n",[18,764,765,795],{"__ignoreMap":52},[56,766,767,769,771,773,775,777,779,781,783,785,787,789,791,793],{"class":58,"line":59},[56,768,63],{"class":62},[56,770,67],{"class":66},[56,772,71],{"class":70},[56,774,75],{"class":74},[56,776,79],{"class":78},[56,778,75],{"class":74},[56,780,84],{"class":70},[56,782,87],{"class":74},[56,784,90],{"class":78},[56,786,75],{"class":74},[56,788,95],{"class":70},[56,790,98],{"class":62},[56,792,102],{"class":101},[56,794,105],{"class":70},[56,796,797,799,801,803,805,807,809,811,813,815,817,819,821],{"class":58,"line":108},[56,798,111],{"class":101},[56,800,114],{"class":70},[56,802,118],{"class":117},[56,804,71],{"class":70},[56,806,75],{"class":74},[56,808,697],{"class":78},[56,810,129],{"class":128},[56,812,719],{"class":78},[56,814,129],{"class":128},[56,816,741],{"class":78},[56,818,129],{"class":128},[56,820,75],{"class":74},[56,822,134],{"class":70},[14,824,825],{},"Both approaches work.",[14,827,828,829,831,832,834],{},"Important: ",[18,830,248],{}," does not add line breaks for you. If you want each line on a new line, include ",[18,833,129],{}," yourself.",[42,836,838],{"id":837},"writing-numbers-and-other-non-string-values","Writing numbers and other non-string values",[14,840,143,841,843],{},[18,842,248],{}," method only accepts strings.",[14,845,846],{},"This will cause an error:",[47,848,850],{"className":49,"code":849,"language":51,"meta":52,"style":52},"with open(\"score.txt\", \"w\") as file:\n    file.write(100)\n",[18,851,852,883],{"__ignoreMap":52},[56,853,854,856,858,860,862,865,867,869,871,873,875,877,879,881],{"class":58,"line":59},[56,855,63],{"class":62},[56,857,67],{"class":66},[56,859,71],{"class":70},[56,861,75],{"class":74},[56,863,864],{"class":78},"score.txt",[56,866,75],{"class":74},[56,868,84],{"class":70},[56,870,87],{"class":74},[56,872,90],{"class":78},[56,874,75],{"class":74},[56,876,95],{"class":70},[56,878,98],{"class":62},[56,880,102],{"class":101},[56,882,105],{"class":70},[56,884,885,887,889,891,893,897],{"class":58,"line":108},[56,886,111],{"class":101},[56,888,114],{"class":70},[56,890,118],{"class":117},[56,892,71],{"class":70},[56,894,896],{"class":895},"srdBf","100",[56,898,134],{"class":70},[14,900,901,902,905,906,908],{},"You will get a ",[18,903,904],{},"TypeError"," because ",[18,907,896],{}," is an integer, not a string.",[228,910,912],{"id":911},"correct-way","Correct way",[14,914,915,916,919],{},"Convert the value with ",[18,917,918],{},"str()"," first:",[47,921,923],{"className":49,"code":922,"language":51,"meta":52,"style":52},"with open(\"score.txt\", \"w\") as file:\n    file.write(str(100))\n",[18,924,925,955],{"__ignoreMap":52},[56,926,927,929,931,933,935,937,939,941,943,945,947,949,951,953],{"class":58,"line":59},[56,928,63],{"class":62},[56,930,67],{"class":66},[56,932,71],{"class":70},[56,934,75],{"class":74},[56,936,864],{"class":78},[56,938,75],{"class":74},[56,940,84],{"class":70},[56,942,87],{"class":74},[56,944,90],{"class":78},[56,946,75],{"class":74},[56,948,95],{"class":70},[56,950,98],{"class":62},[56,952,102],{"class":101},[56,954,105],{"class":70},[56,956,957,959,961,963,965,969,971,973],{"class":58,"line":108},[56,958,111],{"class":101},[56,960,114],{"class":70},[56,962,118],{"class":117},[56,964,71],{"class":70},[56,966,968],{"class":967},"sZMiF","str",[56,970,71],{"class":70},[56,972,896],{"class":895},[56,974,975],{"class":70},"))\n",[14,977,978],{},"You can do the same for floats and other values:",[47,980,982],{"className":49,"code":981,"language":51,"meta":52,"style":52},"temperature = 23.5\n\nwith open(\"temp.txt\", \"w\") as file:\n    file.write(str(temperature))\n",[18,983,984,997,1003,1034],{"__ignoreMap":52},[56,985,986,990,994],{"class":58,"line":59},[56,987,989],{"class":988},"su5hD","temperature ",[56,991,993],{"class":992},"smGrS","=",[56,995,996],{"class":895}," 23.5\n",[56,998,999],{"class":58,"line":108},[56,1000,1002],{"emptyLinePlaceholder":1001},true,"\n",[56,1004,1005,1007,1009,1011,1013,1016,1018,1020,1022,1024,1026,1028,1030,1032],{"class":58,"line":706},[56,1006,63],{"class":62},[56,1008,67],{"class":66},[56,1010,71],{"class":70},[56,1012,75],{"class":74},[56,1014,1015],{"class":78},"temp.txt",[56,1017,75],{"class":74},[56,1019,84],{"class":70},[56,1021,87],{"class":74},[56,1023,90],{"class":78},[56,1025,75],{"class":74},[56,1027,95],{"class":70},[56,1029,98],{"class":62},[56,1031,102],{"class":101},[56,1033,105],{"class":70},[56,1035,1036,1038,1040,1042,1044,1046,1048,1051],{"class":58,"line":728},[56,1037,111],{"class":101},[56,1039,114],{"class":70},[56,1041,118],{"class":117},[56,1043,71],{"class":70},[56,1045,968],{"class":967},[56,1047,71],{"class":70},[56,1049,1050],{"class":117},"temperature",[56,1052,975],{"class":70},[42,1054,1056],{"id":1055},"when-to-use-append-instead","When to use append instead",[14,1058,1059,1060,1062],{},"Use ",[18,1061,487],{}," mode if you want to keep existing content.",[14,1064,1065],{},"This is useful for:",[26,1067,1068,1071,1074,1077],{},[29,1069,1070],{},"logs",[29,1072,1073],{},"notes",[29,1075,1076],{},"saving repeated results",[29,1078,1079],{},"adding new entries over time",[14,1081,1082],{},"Example:",[47,1084,1086],{"className":49,"code":1085,"language":51,"meta":52,"style":52},"with open(\"log.txt\", \"a\") as file:\n    file.write(\"Program started\\n\")\n",[18,1087,1088,1119],{"__ignoreMap":52},[56,1089,1090,1092,1094,1096,1098,1101,1103,1105,1107,1109,1111,1113,1115,1117],{"class":58,"line":59},[56,1091,63],{"class":62},[56,1093,67],{"class":66},[56,1095,71],{"class":70},[56,1097,75],{"class":74},[56,1099,1100],{"class":78},"log.txt",[56,1102,75],{"class":74},[56,1104,84],{"class":70},[56,1106,87],{"class":74},[56,1108,283],{"class":78},[56,1110,75],{"class":74},[56,1112,95],{"class":70},[56,1114,98],{"class":62},[56,1116,102],{"class":101},[56,1118,105],{"class":70},[56,1120,1121,1123,1125,1127,1129,1131,1134,1136,1138],{"class":58,"line":108},[56,1122,111],{"class":101},[56,1124,114],{"class":70},[56,1126,118],{"class":117},[56,1128,71],{"class":70},[56,1130,75],{"class":74},[56,1132,1133],{"class":78},"Program started",[56,1135,129],{"class":128},[56,1137,75],{"class":74},[56,1139,134],{"class":70},[14,1141,1142,1143,1145],{},"If ",[18,1144,1100],{}," already has text in it, the new line is added at the end.",[14,1147,1148,1149,1151,1152,114],{},"Do not use ",[18,1150,146],{}," if you need to preserve what is already in the file. If that is your goal, read ",[283,1153,560],{"href":559},[42,1155,1157],{"id":1156},"common-problems-beginners-hit","Common problems beginners hit",[14,1159,1160],{},"Here are some common mistakes when writing to files.",[228,1162,1164],{"id":1163},"_1-file-content-disappears","1. File content disappears",[14,1166,1167],{},"Cause:",[26,1169,1170],{},[29,1171,1172,1173,1175],{},"You used ",[18,1174,146],{}," mode, which overwrites the file",[14,1177,1082],{},[47,1179,1181],{"className":49,"code":1180,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"w\") as file:\n    file.write(\"New text\\n\")\n",[18,1182,1183,1213],{"__ignoreMap":52},[56,1184,1185,1187,1189,1191,1193,1195,1197,1199,1201,1203,1205,1207,1209,1211],{"class":58,"line":59},[56,1186,63],{"class":62},[56,1188,67],{"class":66},[56,1190,71],{"class":70},[56,1192,75],{"class":74},[56,1194,79],{"class":78},[56,1196,75],{"class":74},[56,1198,84],{"class":70},[56,1200,87],{"class":74},[56,1202,90],{"class":78},[56,1204,75],{"class":74},[56,1206,95],{"class":70},[56,1208,98],{"class":62},[56,1210,102],{"class":101},[56,1212,105],{"class":70},[56,1214,1215,1217,1219,1221,1223,1225,1228,1230,1232],{"class":58,"line":108},[56,1216,111],{"class":101},[56,1218,114],{"class":70},[56,1220,118],{"class":117},[56,1222,71],{"class":70},[56,1224,75],{"class":74},[56,1226,1227],{"class":78},"New text",[56,1229,129],{"class":128},[56,1231,75],{"class":74},[56,1233,134],{"class":70},[14,1235,1142,1236,1238],{},[18,1237,79],{}," already had content, it is replaced.",[14,1240,1241],{},"Fix:",[26,1243,1244],{},[29,1245,1059,1246,1248],{},[18,1247,487],{}," mode if you want to keep old content",[228,1250,1252],{"id":1251},"_2-text-does-not-appear-on-a-new-line","2. Text does not appear on a new line",[14,1254,1167],{},[26,1256,1257],{},[29,1258,1259,1260],{},"You forgot to add ",[18,1261,129],{},[14,1263,1082],{},[47,1265,1267],{"className":49,"code":1266,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"a\") as file:\n    file.write(\"First line\")\n    file.write(\"Second line\")\n",[18,1268,1269,1299,1317],{"__ignoreMap":52},[56,1270,1271,1273,1275,1277,1279,1281,1283,1285,1287,1289,1291,1293,1295,1297],{"class":58,"line":59},[56,1272,63],{"class":62},[56,1274,67],{"class":66},[56,1276,71],{"class":70},[56,1278,75],{"class":74},[56,1280,79],{"class":78},[56,1282,75],{"class":74},[56,1284,84],{"class":70},[56,1286,87],{"class":74},[56,1288,283],{"class":78},[56,1290,75],{"class":74},[56,1292,95],{"class":70},[56,1294,98],{"class":62},[56,1296,102],{"class":101},[56,1298,105],{"class":70},[56,1300,1301,1303,1305,1307,1309,1311,1313,1315],{"class":58,"line":108},[56,1302,111],{"class":101},[56,1304,114],{"class":70},[56,1306,118],{"class":117},[56,1308,71],{"class":70},[56,1310,75],{"class":74},[56,1312,461],{"class":78},[56,1314,75],{"class":74},[56,1316,134],{"class":70},[56,1318,1319,1321,1323,1325,1327,1329,1332,1334],{"class":58,"line":706},[56,1320,111],{"class":101},[56,1322,114],{"class":70},[56,1324,118],{"class":117},[56,1326,71],{"class":70},[56,1328,75],{"class":74},[56,1330,1331],{"class":78},"Second line",[56,1333,75],{"class":74},[56,1335,134],{"class":70},[14,1337,1338],{},"Result:",[47,1340,1343],{"className":1341,"code":1342,"language":273,"meta":52},[271],"First lineSecond line\n",[18,1344,1342],{"__ignoreMap":52},[14,1346,1241],{},[47,1348,1350],{"className":49,"code":1349,"language":51,"meta":52,"style":52},"with open(\"notes.txt\", \"a\") as file:\n    file.write(\"First line\\n\")\n    file.write(\"Second line\\n\")\n",[18,1351,1352,1382,1402],{"__ignoreMap":52},[56,1353,1354,1356,1358,1360,1362,1364,1366,1368,1370,1372,1374,1376,1378,1380],{"class":58,"line":59},[56,1355,63],{"class":62},[56,1357,67],{"class":66},[56,1359,71],{"class":70},[56,1361,75],{"class":74},[56,1363,79],{"class":78},[56,1365,75],{"class":74},[56,1367,84],{"class":70},[56,1369,87],{"class":74},[56,1371,283],{"class":78},[56,1373,75],{"class":74},[56,1375,95],{"class":70},[56,1377,98],{"class":62},[56,1379,102],{"class":101},[56,1381,105],{"class":70},[56,1383,1384,1386,1388,1390,1392,1394,1396,1398,1400],{"class":58,"line":108},[56,1385,111],{"class":101},[56,1387,114],{"class":70},[56,1389,118],{"class":117},[56,1391,71],{"class":70},[56,1393,75],{"class":74},[56,1395,461],{"class":78},[56,1397,129],{"class":128},[56,1399,75],{"class":74},[56,1401,134],{"class":70},[56,1403,1404,1406,1408,1410,1412,1414,1416,1418,1420],{"class":58,"line":706},[56,1405,111],{"class":101},[56,1407,114],{"class":70},[56,1409,118],{"class":117},[56,1411,71],{"class":70},[56,1413,75],{"class":74},[56,1415,1331],{"class":78},[56,1417,129],{"class":128},[56,1419,75],{"class":74},[56,1421,134],{"class":70},[228,1423,1425,1426,1428],{"id":1424},"_3-typeerror-when-writing-a-number","3. ",[18,1427,904],{}," when writing a number",[14,1430,1167],{},[26,1432,1433],{},[29,1434,1435,1437],{},[18,1436,248],{}," expects a string",[14,1439,1241],{},[47,1441,1443],{"className":49,"code":1442,"language":51,"meta":52,"style":52},"age = 25\n\nwith open(\"age.txt\", \"w\") as file:\n    file.write(str(age))\n",[18,1444,1445,1455,1459,1490],{"__ignoreMap":52},[56,1446,1447,1450,1452],{"class":58,"line":59},[56,1448,1449],{"class":988},"age ",[56,1451,993],{"class":992},[56,1453,1454],{"class":895}," 25\n",[56,1456,1457],{"class":58,"line":108},[56,1458,1002],{"emptyLinePlaceholder":1001},[56,1460,1461,1463,1465,1467,1469,1472,1474,1476,1478,1480,1482,1484,1486,1488],{"class":58,"line":706},[56,1462,63],{"class":62},[56,1464,67],{"class":66},[56,1466,71],{"class":70},[56,1468,75],{"class":74},[56,1470,1471],{"class":78},"age.txt",[56,1473,75],{"class":74},[56,1475,84],{"class":70},[56,1477,87],{"class":74},[56,1479,90],{"class":78},[56,1481,75],{"class":74},[56,1483,95],{"class":70},[56,1485,98],{"class":62},[56,1487,102],{"class":101},[56,1489,105],{"class":70},[56,1491,1492,1494,1496,1498,1500,1502,1504,1507],{"class":58,"line":728},[56,1493,111],{"class":101},[56,1495,114],{"class":70},[56,1497,118],{"class":117},[56,1499,71],{"class":70},[56,1501,968],{"class":967},[56,1503,71],{"class":70},[56,1505,1506],{"class":117},"age",[56,1508,975],{"class":70},[228,1510,1512],{"id":1511},"_4-file-path-errors","4. File path errors",[14,1514,1167],{},[26,1516,1517,1520],{},[29,1518,1519],{},"The file location is wrong",[29,1521,1522],{},"You are using a folder name that does not exist",[14,1524,1082],{},[47,1526,1528],{"className":49,"code":1527,"language":51,"meta":52,"style":52},"with open(\"missing_folder\u002Fnotes.txt\", \"w\") as file:\n    file.write(\"Hello\\n\")\n",[18,1529,1530,1561],{"__ignoreMap":52},[56,1531,1532,1534,1536,1538,1540,1543,1545,1547,1549,1551,1553,1555,1557,1559],{"class":58,"line":59},[56,1533,63],{"class":62},[56,1535,67],{"class":66},[56,1537,71],{"class":70},[56,1539,75],{"class":74},[56,1541,1542],{"class":78},"missing_folder\u002Fnotes.txt",[56,1544,75],{"class":74},[56,1546,84],{"class":70},[56,1548,87],{"class":74},[56,1550,90],{"class":78},[56,1552,75],{"class":74},[56,1554,95],{"class":70},[56,1556,98],{"class":62},[56,1558,102],{"class":101},[56,1560,105],{"class":70},[56,1562,1563,1565,1567,1569,1571,1573,1576,1578,1580],{"class":58,"line":108},[56,1564,111],{"class":101},[56,1566,114],{"class":70},[56,1568,118],{"class":117},[56,1570,71],{"class":70},[56,1572,75],{"class":74},[56,1574,1575],{"class":78},"Hello",[56,1577,129],{"class":128},[56,1579,75],{"class":74},[56,1581,134],{"class":70},[14,1583,1584],{},"If the folder does not exist, writing will fail.",[14,1586,1587,1588,114],{},"If you are seeing a missing file or bad path problem, see ",[283,1589,1591,1592,1595],{"href":1590},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","FileNotFoundError: ",[56,1593,1594],{},"Errno 2"," No such file or directory fix",[228,1597,1599],{"id":1598},"_5-permission-errors","5. Permission errors",[14,1601,1167],{},[26,1603,1604],{},[29,1605,1606],{},"Python does not have permission to write to that location",[14,1608,1609],{},"This can happen with protected folders or system locations.",[14,1611,1612,1613,114],{},"If that happens, see ",[283,1614,1616,1617,1620],{"href":1615},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[56,1618,1619],{},"Errno 13"," Permission denied fix",[42,1622,1624],{"id":1623},"simple-debugging-checks","Simple debugging checks",[14,1626,1627],{},"If writing to a file is not working, check these first:",[26,1629,1630,1633,1641,1647],{},[29,1631,1632],{},"Print the file name or full path you are using",[29,1634,1635,1636,1638,1639,411],{},"Check whether you opened the file in ",[18,1637,146],{}," or ",[18,1640,487],{},[29,1642,1643,1644,1646],{},"Make sure the value passed to ",[18,1645,248],{}," is a string",[29,1648,1649],{},"Check folder permissions if writing fails",[14,1651,1652],{},"Useful debug commands:",[47,1654,1656],{"className":49,"code":1655,"language":51,"meta":52,"style":52},"print(type(value))\nprint(file_path)\n\nimport os\nprint(os.getcwd())\n\nimport os\nprint(os.path.exists(file_path))\n",[18,1657,1658,1675,1686,1690,1698,1716,1721,1728],{"__ignoreMap":52},[56,1659,1660,1663,1665,1668,1670,1673],{"class":58,"line":59},[56,1661,1662],{"class":66},"print",[56,1664,71],{"class":70},[56,1666,1667],{"class":967},"type",[56,1669,71],{"class":70},[56,1671,1672],{"class":117},"value",[56,1674,975],{"class":70},[56,1676,1677,1679,1681,1684],{"class":58,"line":108},[56,1678,1662],{"class":66},[56,1680,71],{"class":70},[56,1682,1683],{"class":117},"file_path",[56,1685,134],{"class":70},[56,1687,1688],{"class":58,"line":706},[56,1689,1002],{"emptyLinePlaceholder":1001},[56,1691,1692,1695],{"class":58,"line":728},[56,1693,1694],{"class":62},"import",[56,1696,1697],{"class":988}," os\n",[56,1699,1701,1703,1705,1708,1710,1713],{"class":58,"line":1700},5,[56,1702,1662],{"class":66},[56,1704,71],{"class":70},[56,1706,1707],{"class":117},"os",[56,1709,114],{"class":70},[56,1711,1712],{"class":117},"getcwd",[56,1714,1715],{"class":70},"())\n",[56,1717,1719],{"class":58,"line":1718},6,[56,1720,1002],{"emptyLinePlaceholder":1001},[56,1722,1724,1726],{"class":58,"line":1723},7,[56,1725,1694],{"class":62},[56,1727,1697],{"class":988},[56,1729,1731,1733,1735,1737,1739,1743,1745,1748,1750,1752],{"class":58,"line":1730},8,[56,1732,1662],{"class":66},[56,1734,71],{"class":70},[56,1736,1707],{"class":117},[56,1738,114],{"class":70},[56,1740,1742],{"class":1741},"skxfh","path",[56,1744,114],{"class":70},[56,1746,1747],{"class":117},"exists",[56,1749,71],{"class":70},[56,1751,1683],{"class":117},[56,1753,975],{"class":70},[228,1755,1757],{"id":1756},"what-these-help-you-check","What these help you check",[26,1759,1760,1766,1772,1778],{},[29,1761,1762,1765],{},[18,1763,1764],{},"print(type(value))"," shows whether the value is a string",[29,1767,1768,1771],{},[18,1769,1770],{},"print(file_path)"," shows the exact file path being used",[29,1773,1774,1777],{},[18,1775,1776],{},"os.getcwd()"," shows the current working folder",[29,1779,1780,1783],{},[18,1781,1782],{},"os.path.exists(file_path)"," checks whether the path exists",[14,1785,1786,1787,114],{},"If you need help with file locations, read ",[283,1788,1790],{"href":1789},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","how to check if a file exists in Python",[42,1792,1794],{"id":1793},"faq","FAQ",[228,1796,1798,1799,1801],{"id":1797},"does-open-create-the-file-if-it-does-not-exist","Does ",[18,1800,20],{}," create the file if it does not exist?",[14,1803,1804,1805,1807,1808,1810],{},"Yes. In ",[18,1806,146],{}," and ",[18,1809,487],{}," mode, Python creates the file if it does not already exist.",[228,1812,1814,1815,1807,1817,1819],{"id":1813},"what-is-the-difference-between-w-and-a-mode","What is the difference between ",[18,1816,146],{},[18,1818,487],{}," mode?",[14,1821,1822,1824,1825,1827],{},[18,1823,146],{}," overwrites the file. ",[18,1826,487],{}," adds new content to the end.",[228,1829,1831,1832,1834],{"id":1830},"why-does-write-not-put-text-on-a-new-line-automatically","Why does ",[18,1833,248],{}," not put text on a new line automatically?",[14,1836,1837,1839,1840,1842],{},[18,1838,248],{}," writes exactly what you give it. Add ",[18,1841,129],{}," if you want a line break.",[228,1844,1846],{"id":1845},"can-i-write-numbers-to-a-file","Can I write numbers to a file?",[14,1848,1849,1850,1852,1853,1855],{},"Yes, but convert them first with ",[18,1851,918],{},". The ",[18,1854,248],{}," method expects a string.",[228,1857,1859],{"id":1858},"do-i-need-to-close-the-file-manually","Do I need to close the file manually?",[14,1861,1862,1863,1865],{},"Not if you use a ",[18,1864,63],{}," statement. Python closes it for you automatically.",[42,1867,1869],{"id":1868},"see-also","See also",[26,1871,1872,1876,1881,1887,1893],{},[29,1873,1874],{},[283,1875,286],{"href":285},[29,1877,1878],{},[283,1879,1880],{"href":559},"How to append to a file in Python",[29,1882,1883],{},[283,1884,1886],{"href":1885},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","How to read a file in Python",[29,1888,1889],{},[283,1890,1892],{"href":1891},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics: read and write",[29,1894,1895],{},[283,1896,1897],{"href":1789},"How to check if a file exists in Python",[1899,1900,1901],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":52,"searchDepth":108,"depth":108,"links":1903},[1904,1905,1906,1910,1911,1919,1920,1923,1924,1932,1935,1945],{"id":44,"depth":108,"text":45},{"id":150,"depth":108,"text":151},{"id":168,"depth":108,"text":169,"children":1907},[1908,1909],{"id":230,"depth":706,"text":231},{"id":260,"depth":706,"text":261},{"id":289,"depth":108,"text":290},{"id":401,"depth":108,"text":402,"children":1912},[1913,1915,1917],{"id":408,"depth":706,"text":1914},"\"w\" mode",{"id":484,"depth":706,"text":1916},"\"a\" mode",{"id":563,"depth":706,"text":1918},"\"x\" mode",{"id":640,"depth":108,"text":641},{"id":837,"depth":108,"text":838,"children":1921},[1922],{"id":911,"depth":706,"text":912},{"id":1055,"depth":108,"text":1056},{"id":1156,"depth":108,"text":1157,"children":1925},[1926,1927,1928,1930,1931],{"id":1163,"depth":706,"text":1164},{"id":1251,"depth":706,"text":1252},{"id":1424,"depth":706,"text":1929},"3. TypeError when writing a number",{"id":1511,"depth":706,"text":1512},{"id":1598,"depth":706,"text":1599},{"id":1623,"depth":108,"text":1624,"children":1933},[1934],{"id":1756,"depth":706,"text":1757},{"id":1793,"depth":108,"text":1794,"children":1936},[1937,1939,1941,1943,1944],{"id":1797,"depth":706,"text":1938},"Does open() create the file if it does not exist?",{"id":1813,"depth":706,"text":1940},"What is the difference between \"w\" and \"a\" mode?",{"id":1830,"depth":706,"text":1942},"Why does write() not put text on a new line automatically?",{"id":1845,"depth":706,"text":1846},{"id":1858,"depth":706,"text":1859},{"id":1868,"depth":108,"text":1869},"Master how to write to a file in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python",{"title":5,"description":1946},"how-to\u002Fhow-to-write-to-a-file-in-python","zEgufHsaZwSQGib0jMokQq9mng5zbMGF-ZVeIx9TuyE",1777585505076]