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