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