[{"data":1,"prerenderedAt":1312},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-read-and-write-text-file-example":3},{"id":4,"title":5,"body":6,"description":1305,"extension":1306,"meta":1307,"navigation":64,"path":1308,"seo":1309,"stem":1310,"__hash__":1311},"content\u002Fexamples\u002Fpython-read-and-write-text-file-example.md","Python Read and Write Text File Example",{"type":7,"value":8,"toc":1275},"minimark",[9,13,17,20,25,228,231,235,270,283,287,290,307,310,313,334,337,363,366,370,373,460,463,491,498,502,505,564,567,592,599,603,606,614,617,620,632,636,641,712,718,722,728,734,738,783,786,793,798,806,821,824,828,831,854,857,879,889,892,907,937,974,1028,1031,1047,1051,1054,1085,1088,1156,1163,1167,1178,1183,1190,1195,1206,1211,1215,1223,1227,1235,1239,1271],[10,11,5],"h1",{"id":12},"python-read-and-write-text-file-example",[14,15,16],"p",{},"This example shows a small Python script that writes text to a file and then reads it back.",[14,18,19],{},"It is a practical beginner example. The goal is simple: create or overwrite a text file, add some text, read the file, and print the result.",[21,22,24],"h2",{"id":23},"quick-example","Quick example",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","file_name = \"notes.txt\"\n\nwith open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n\nwith open(file_name, \"r\") as file:\n    content = file.read()\n\nprint(content)\n","python","",[33,34,35,59,66,109,137,159,164,192,210,215],"code",{"__ignoreMap":31},[36,37,40,44,48,52,56],"span",{"class":38,"line":39},"line",1,[36,41,43],{"class":42},"su5hD","file_name ",[36,45,47],{"class":46},"smGrS","=",[36,49,51],{"class":50},"sjJ54"," \"",[36,53,55],{"class":54},"s_sjI","notes.txt",[36,57,58],{"class":50},"\"\n",[36,60,62],{"class":38,"line":61},2,[36,63,65],{"emptyLinePlaceholder":64},true,"\n",[36,67,69,73,77,81,85,88,90,93,96,99,102,106],{"class":38,"line":68},3,[36,70,72],{"class":71},"sVHd0","with",[36,74,76],{"class":75},"sptTA"," open",[36,78,80],{"class":79},"sP7_E","(",[36,82,84],{"class":83},"slqww","file_name",[36,86,87],{"class":79},",",[36,89,51],{"class":50},[36,91,92],{"class":54},"w",[36,94,95],{"class":50},"\"",[36,97,98],{"class":79},")",[36,100,101],{"class":71}," as",[36,103,105],{"class":104},"sMMDD"," file",[36,107,108],{"class":79},":\n",[36,110,112,115,118,121,123,125,128,132,134],{"class":38,"line":111},4,[36,113,114],{"class":104},"    file",[36,116,117],{"class":79},".",[36,119,120],{"class":83},"write",[36,122,80],{"class":79},[36,124,95],{"class":50},[36,126,127],{"class":54},"Hello, Python!",[36,129,131],{"class":130},"s_hVV","\\n",[36,133,95],{"class":50},[36,135,136],{"class":79},")\n",[36,138,140,142,144,146,148,150,153,155,157],{"class":38,"line":139},5,[36,141,114],{"class":104},[36,143,117],{"class":79},[36,145,120],{"class":83},[36,147,80],{"class":79},[36,149,95],{"class":50},[36,151,152],{"class":54},"This is a text file example.",[36,154,131],{"class":130},[36,156,95],{"class":50},[36,158,136],{"class":79},[36,160,162],{"class":38,"line":161},6,[36,163,65],{"emptyLinePlaceholder":64},[36,165,167,169,171,173,175,177,179,182,184,186,188,190],{"class":38,"line":166},7,[36,168,72],{"class":71},[36,170,76],{"class":75},[36,172,80],{"class":79},[36,174,84],{"class":83},[36,176,87],{"class":79},[36,178,51],{"class":50},[36,180,181],{"class":54},"r",[36,183,95],{"class":50},[36,185,98],{"class":79},[36,187,101],{"class":71},[36,189,105],{"class":104},[36,191,108],{"class":79},[36,193,195,198,200,202,204,207],{"class":38,"line":194},8,[36,196,197],{"class":42},"    content ",[36,199,47],{"class":46},[36,201,105],{"class":104},[36,203,117],{"class":79},[36,205,206],{"class":83},"read",[36,208,209],{"class":79},"()\n",[36,211,213],{"class":38,"line":212},9,[36,214,65],{"emptyLinePlaceholder":64},[36,216,218,221,223,226],{"class":38,"line":217},10,[36,219,220],{"class":75},"print",[36,222,80],{"class":79},[36,224,225],{"class":83},"content",[36,227,136],{"class":79},[14,229,230],{},"This creates or overwrites a text file, writes two lines, reads the full file, and prints the result.",[21,232,234],{"id":233},"what-this-example-shows","What this example shows",[236,237,238,245,251,257,263],"ul",{},[239,240,241,242],"li",{},"How to open a file for writing with mode ",[33,243,244],{},"\"w\"",[239,246,247,248],{},"How to write text using ",[33,249,250],{},"write()",[239,252,253,254],{},"How to open the same file for reading with mode ",[33,255,256],{},"\"r\"",[239,258,259,260],{},"How to read all file content with ",[33,261,262],{},"read()",[239,264,265,266,269],{},"Why using ",[33,267,268],{},"with open(...)"," is the beginner-friendly safe pattern",[14,271,272,273,278,279,117],{},"If you want a fuller introduction, see ",[274,275,277],"a",{"href":276},"\u002Flearn\u002Fpython-file-handling-basics-read-and-write\u002F","Python file handling basics: read and write"," or the ",[274,280,282],{"href":281},"\u002Freference\u002Fpython-open-function-explained\u002F","Python open() function explained",[21,284,286],{"id":285},"example-setup","Example setup",[14,288,289],{},"In this example, the file name is:",[26,291,293],{"className":28,"code":292,"language":30,"meta":31,"style":31},"file_name = \"notes.txt\"\n",[33,294,295],{"__ignoreMap":31},[36,296,297,299,301,303,305],{"class":38,"line":39},[36,298,43],{"class":42},[36,300,47],{"class":46},[36,302,51],{"class":50},[36,304,55],{"class":54},[36,306,58],{"class":50},[14,308,309],{},"This means Python will look for the file in the current folder where your script runs.",[14,311,312],{},"A few important points:",[236,314,315,320,323,329],{},[239,316,317,319],{},[33,318,244],{}," means write mode",[239,321,322],{},"If the file does not exist, Python creates it",[239,324,325,326,328],{},"If the file already exists, ",[33,327,244],{}," removes the old content first",[239,330,331,333],{},[33,332,131],{}," adds a new line",[14,335,336],{},"So this line:",[26,338,340],{"className":28,"code":339,"language":30,"meta":31,"style":31},"file.write(\"Hello, Python!\\n\")\n",[33,341,342],{"__ignoreMap":31},[36,343,344,347,349,351,353,355,357,359,361],{"class":38,"line":39},[36,345,346],{"class":104},"file",[36,348,117],{"class":79},[36,350,120],{"class":83},[36,352,80],{"class":79},[36,354,95],{"class":50},[36,356,127],{"class":54},[36,358,131],{"class":130},[36,360,95],{"class":50},[36,362,136],{"class":79},[14,364,365],{},"writes the text and then moves to the next line.",[21,367,369],{"id":368},"write-text-to-the-file","Write text to the file",[14,371,372],{},"Here is the writing part by itself:",[26,374,376],{"className":28,"code":375,"language":30,"meta":31,"style":31},"file_name = \"notes.txt\"\n\nwith open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n",[33,377,378,390,394,420,440],{"__ignoreMap":31},[36,379,380,382,384,386,388],{"class":38,"line":39},[36,381,43],{"class":42},[36,383,47],{"class":46},[36,385,51],{"class":50},[36,387,55],{"class":54},[36,389,58],{"class":50},[36,391,392],{"class":38,"line":61},[36,393,65],{"emptyLinePlaceholder":64},[36,395,396,398,400,402,404,406,408,410,412,414,416,418],{"class":38,"line":68},[36,397,72],{"class":71},[36,399,76],{"class":75},[36,401,80],{"class":79},[36,403,84],{"class":83},[36,405,87],{"class":79},[36,407,51],{"class":50},[36,409,92],{"class":54},[36,411,95],{"class":50},[36,413,98],{"class":79},[36,415,101],{"class":71},[36,417,105],{"class":104},[36,419,108],{"class":79},[36,421,422,424,426,428,430,432,434,436,438],{"class":38,"line":111},[36,423,114],{"class":104},[36,425,117],{"class":79},[36,427,120],{"class":83},[36,429,80],{"class":79},[36,431,95],{"class":50},[36,433,127],{"class":54},[36,435,131],{"class":130},[36,437,95],{"class":50},[36,439,136],{"class":79},[36,441,442,444,446,448,450,452,454,456,458],{"class":38,"line":139},[36,443,114],{"class":104},[36,445,117],{"class":79},[36,447,120],{"class":83},[36,449,80],{"class":79},[36,451,95],{"class":50},[36,453,152],{"class":54},[36,455,131],{"class":130},[36,457,95],{"class":50},[36,459,136],{"class":79},[14,461,462],{},"What to notice:",[236,464,465,471,476,481],{},[239,466,467,470],{},[33,468,469],{},"open(file_name, \"w\")"," opens the file in write mode",[239,472,473,475],{},[33,474,72],{}," automatically closes the file when the block ends",[239,477,478,480],{},[33,479,250],{}," writes exactly the string you give it",[239,482,483,485,486,490],{},[33,484,250],{}," does ",[487,488,489],"strong",{},"not"," add a new line by itself",[14,492,493,494,117],{},"If you want to learn more about writing files, see ",[274,495,497],{"href":496},"\u002Fhow-to\u002Fhow-to-write-to-a-file-in-python\u002F","how to write to a file in Python",[21,499,501],{"id":500},"read-text-from-the-file","Read text from the file",[14,503,504],{},"Here is the reading part:",[26,506,508],{"className":28,"code":507,"language":30,"meta":31,"style":31},"with open(file_name, \"r\") as file:\n    content = file.read()\n\nprint(content)\n",[33,509,510,536,550,554],{"__ignoreMap":31},[36,511,512,514,516,518,520,522,524,526,528,530,532,534],{"class":38,"line":39},[36,513,72],{"class":71},[36,515,76],{"class":75},[36,517,80],{"class":79},[36,519,84],{"class":83},[36,521,87],{"class":79},[36,523,51],{"class":50},[36,525,181],{"class":54},[36,527,95],{"class":50},[36,529,98],{"class":79},[36,531,101],{"class":71},[36,533,105],{"class":104},[36,535,108],{"class":79},[36,537,538,540,542,544,546,548],{"class":38,"line":61},[36,539,197],{"class":42},[36,541,47],{"class":46},[36,543,105],{"class":104},[36,545,117],{"class":79},[36,547,206],{"class":83},[36,549,209],{"class":79},[36,551,552],{"class":38,"line":68},[36,553,65],{"emptyLinePlaceholder":64},[36,555,556,558,560,562],{"class":38,"line":111},[36,557,220],{"class":75},[36,559,80],{"class":79},[36,561,225],{"class":83},[36,563,136],{"class":79},[14,565,566],{},"What this does:",[236,568,569,575,581,586],{},[239,570,571,574],{},[33,572,573],{},"open(file_name, \"r\")"," opens the file in read mode",[239,576,577,580],{},[33,578,579],{},"file.read()"," reads the whole file as one string",[239,582,583,584],{},"The result is stored in ",[33,585,225],{},[239,587,588,591],{},[33,589,590],{},"print(content)"," shows the text in the terminal",[14,593,594,595,117],{},"If you want more reading examples, see ",[274,596,598],{"href":597},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","how to read a file in Python",[21,600,602],{"id":601},"expected-output","Expected output",[14,604,605],{},"The script prints:",[26,607,612],{"className":608,"code":610,"language":611,"meta":31},[609],"language-text","Hello, Python!\nThis is a text file example.\n","text",[33,613,610],{"__ignoreMap":31},[14,615,616],{},"This matches the two lines written earlier.",[14,618,619],{},"That is the main idea of this example:",[621,622,623,626,629],"ol",{},[239,624,625],{},"Write text into the file",[239,627,628],{},"Read the same text back",[239,630,631],{},"Print it so you can see the result",[21,633,635],{"id":634},"how-this-example-works-step-by-step","How this example works step by step",[637,638,640],"h3",{"id":639},"_1-the-first-block-opens-the-file-and-writes-text","1. The first block opens the file and writes text",[26,642,644],{"className":28,"code":643,"language":30,"meta":31,"style":31},"with open(file_name, \"w\") as file:\n    file.write(\"Hello, Python!\\n\")\n    file.write(\"This is a text file example.\\n\")\n",[33,645,646,672,692],{"__ignoreMap":31},[36,647,648,650,652,654,656,658,660,662,664,666,668,670],{"class":38,"line":39},[36,649,72],{"class":71},[36,651,76],{"class":75},[36,653,80],{"class":79},[36,655,84],{"class":83},[36,657,87],{"class":79},[36,659,51],{"class":50},[36,661,92],{"class":54},[36,663,95],{"class":50},[36,665,98],{"class":79},[36,667,101],{"class":71},[36,669,105],{"class":104},[36,671,108],{"class":79},[36,673,674,676,678,680,682,684,686,688,690],{"class":38,"line":61},[36,675,114],{"class":104},[36,677,117],{"class":79},[36,679,120],{"class":83},[36,681,80],{"class":79},[36,683,95],{"class":50},[36,685,127],{"class":54},[36,687,131],{"class":130},[36,689,95],{"class":50},[36,691,136],{"class":79},[36,693,694,696,698,700,702,704,706,708,710],{"class":38,"line":68},[36,695,114],{"class":104},[36,697,117],{"class":79},[36,699,120],{"class":83},[36,701,80],{"class":79},[36,703,95],{"class":50},[36,705,152],{"class":54},[36,707,131],{"class":130},[36,709,95],{"class":50},[36,711,136],{"class":79},[14,713,714,715,717],{},"This creates ",[33,716,55],{}," if needed and writes two lines into it.",[637,719,721],{"id":720},"_2-the-file-is-closed-automatically","2. The file is closed automatically",[14,723,724,725,727],{},"When the ",[33,726,72],{}," block ends, Python closes the file for you.",[14,729,730,731,733],{},"This is why ",[33,732,268],{}," is the safest beginner pattern.",[637,735,737],{"id":736},"_3-the-second-block-opens-the-file-again-for-reading","3. The second block opens the file again for reading",[26,739,741],{"className":28,"code":740,"language":30,"meta":31,"style":31},"with open(file_name, \"r\") as file:\n    content = file.read()\n",[33,742,743,769],{"__ignoreMap":31},[36,744,745,747,749,751,753,755,757,759,761,763,765,767],{"class":38,"line":39},[36,746,72],{"class":71},[36,748,76],{"class":75},[36,750,80],{"class":79},[36,752,84],{"class":83},[36,754,87],{"class":79},[36,756,51],{"class":50},[36,758,181],{"class":54},[36,760,95],{"class":50},[36,762,98],{"class":79},[36,764,101],{"class":71},[36,766,105],{"class":104},[36,768,108],{"class":79},[36,770,771,773,775,777,779,781],{"class":38,"line":61},[36,772,197],{"class":42},[36,774,47],{"class":46},[36,776,105],{"class":104},[36,778,117],{"class":79},[36,780,206],{"class":83},[36,782,209],{"class":79},[14,784,785],{},"Now Python opens the same file in read mode.",[637,787,789,790,792],{"id":788},"_4-read-returns-the-file-content-as-a-string","4. ",[33,791,262],{}," returns the file content as a string",[14,794,795,796,117],{},"The full contents of the file are stored in ",[33,797,225],{},[637,799,801,802,805],{"id":800},"_5-print-displays-that-string","5. ",[33,803,804],{},"print()"," displays that string",[26,807,809],{"className":28,"code":808,"language":30,"meta":31,"style":31},"print(content)\n",[33,810,811],{"__ignoreMap":31},[36,812,813,815,817,819],{"class":38,"line":39},[36,814,220],{"class":75},[36,816,80],{"class":79},[36,818,225],{"class":83},[36,820,136],{"class":79},[14,822,823],{},"This prints the file text to the terminal or console.",[21,825,827],{"id":826},"common-beginner-mistakes","Common beginner mistakes",[14,829,830],{},"Here are some common problems with this kind of script:",[236,832,833,839,845,848,851],{},[239,834,835,836,838],{},"Using ",[33,837,244],{}," when you wanted to keep old file content",[239,840,841,842,844],{},"Forgetting ",[33,843,131],{}," and expecting separate lines",[239,846,847],{},"Trying to read before the file was written",[239,849,850],{},"Using the wrong file path",[239,852,853],{},"Misspelling the file name",[14,855,856],{},"Also watch out for these common causes:",[236,858,859,865,868,871,874],{},[239,860,861,862,864],{},"Using write mode ",[33,863,244],{}," and accidentally overwriting existing content",[239,866,867],{},"Reading a file from a different folder than expected",[239,869,870],{},"Forgetting newline characters when writing multiple lines",[239,872,873],{},"Confusing text file reading with CSV or JSON reading",[239,875,876,877],{},"Trying to open a file that does not exist when using ",[33,878,256],{},[14,880,881,882,117],{},"If Python says the file cannot be found, see ",[274,883,885,886],{"href":884},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","how to fix ",[33,887,888],{},"FileNotFoundError: [Errno 2] No such file or directory",[14,890,891],{},"Useful debugging checks:",[26,893,895],{"className":28,"code":894,"language":30,"meta":31,"style":31},"print(file_name)\n",[33,896,897],{"__ignoreMap":31},[36,898,899,901,903,905],{"class":38,"line":39},[36,900,220],{"class":75},[36,902,80],{"class":79},[36,904,84],{"class":83},[36,906,136],{"class":79},[26,908,910],{"className":28,"code":909,"language":30,"meta":31,"style":31},"import os\nprint(os.getcwd())\n",[33,911,912,920],{"__ignoreMap":31},[36,913,914,917],{"class":38,"line":39},[36,915,916],{"class":71},"import",[36,918,919],{"class":42}," os\n",[36,921,922,924,926,929,931,934],{"class":38,"line":61},[36,923,220],{"class":75},[36,925,80],{"class":79},[36,927,928],{"class":83},"os",[36,930,117],{"class":79},[36,932,933],{"class":83},"getcwd",[36,935,936],{"class":79},"())\n",[26,938,940],{"className":28,"code":939,"language":30,"meta":31,"style":31},"import os\nprint(os.path.exists(file_name))\n",[33,941,942,948],{"__ignoreMap":31},[36,943,944,946],{"class":38,"line":39},[36,945,916],{"class":71},[36,947,919],{"class":42},[36,949,950,952,954,956,958,962,964,967,969,971],{"class":38,"line":61},[36,951,220],{"class":75},[36,953,80],{"class":79},[36,955,928],{"class":83},[36,957,117],{"class":79},[36,959,961],{"class":960},"skxfh","path",[36,963,117],{"class":79},[36,965,966],{"class":83},"exists",[36,968,80],{"class":79},[36,970,84],{"class":83},[36,972,973],{"class":79},"))\n",[26,975,977],{"className":28,"code":976,"language":30,"meta":31,"style":31},"with open(file_name, 'r') as file:\n    print(repr(file.read()))\n",[33,978,979,1007],{"__ignoreMap":31},[36,980,981,983,985,987,989,991,994,996,999,1001,1003,1005],{"class":38,"line":39},[36,982,72],{"class":71},[36,984,76],{"class":75},[36,986,80],{"class":79},[36,988,84],{"class":83},[36,990,87],{"class":79},[36,992,993],{"class":50}," '",[36,995,181],{"class":54},[36,997,998],{"class":50},"'",[36,1000,98],{"class":79},[36,1002,101],{"class":71},[36,1004,105],{"class":104},[36,1006,108],{"class":79},[36,1008,1009,1012,1014,1017,1019,1021,1023,1025],{"class":38,"line":61},[36,1010,1011],{"class":75},"    print",[36,1013,80],{"class":79},[36,1015,1016],{"class":75},"repr",[36,1018,80],{"class":79},[36,1020,346],{"class":104},[36,1022,117],{"class":79},[36,1024,206],{"class":83},[36,1026,1027],{"class":79},"()))\n",[14,1029,1030],{},"These help you check:",[236,1032,1033,1036,1039,1042],{},[239,1034,1035],{},"the file name you are using",[239,1037,1038],{},"the current working folder",[239,1040,1041],{},"whether the file actually exists",[239,1043,1044,1045],{},"the exact text in the file, including ",[33,1046,131],{},[21,1048,1050],{"id":1049},"try-a-small-variation","Try a small variation",[14,1052,1053],{},"Once this example works, try changing one small part at a time:",[236,1055,1056,1059,1062,1075],{},[239,1057,1058],{},"Change the file name",[239,1060,1061],{},"Write three lines instead of two",[239,1063,1064,1065,1067,1068,1071,1072],{},"Replace ",[33,1066,262],{}," with ",[33,1069,1070],{},"readline()"," or ",[33,1073,1074],{},"readlines()",[239,1076,1077,1078,1080,1081,1084],{},"Switch from ",[33,1079,244],{}," to ",[33,1082,1083],{},"\"a\""," and see what changes",[14,1086,1087],{},"For example, append mode keeps the old content instead of replacing it:",[26,1089,1091],{"className":28,"code":1090,"language":30,"meta":31,"style":31},"file_name = \"notes.txt\"\n\nwith open(file_name, \"a\") as file:\n    file.write(\"This line was added later.\\n\")\n",[33,1092,1093,1105,1109,1135],{"__ignoreMap":31},[36,1094,1095,1097,1099,1101,1103],{"class":38,"line":39},[36,1096,43],{"class":42},[36,1098,47],{"class":46},[36,1100,51],{"class":50},[36,1102,55],{"class":54},[36,1104,58],{"class":50},[36,1106,1107],{"class":38,"line":61},[36,1108,65],{"emptyLinePlaceholder":64},[36,1110,1111,1113,1115,1117,1119,1121,1123,1125,1127,1129,1131,1133],{"class":38,"line":68},[36,1112,72],{"class":71},[36,1114,76],{"class":75},[36,1116,80],{"class":79},[36,1118,84],{"class":83},[36,1120,87],{"class":79},[36,1122,51],{"class":50},[36,1124,274],{"class":54},[36,1126,95],{"class":50},[36,1128,98],{"class":79},[36,1130,101],{"class":71},[36,1132,105],{"class":104},[36,1134,108],{"class":79},[36,1136,1137,1139,1141,1143,1145,1147,1150,1152,1154],{"class":38,"line":111},[36,1138,114],{"class":104},[36,1140,117],{"class":79},[36,1142,120],{"class":83},[36,1144,80],{"class":79},[36,1146,95],{"class":50},[36,1148,1149],{"class":54},"This line was added later.",[36,1151,131],{"class":130},[36,1153,95],{"class":50},[36,1155,136],{"class":79},[14,1157,1158,1159,117],{},"If you want to practice that next, see ",[274,1160,1162],{"href":1161},"\u002Fhow-to\u002Fhow-to-append-to-a-file-in-python\u002F","how to append to a file in Python",[21,1164,1166],{"id":1165},"faq","FAQ",[637,1168,1170,1171,1173,1174,1177],{"id":1169},"what-does-w-mean-in-open","What does ",[33,1172,244],{}," mean in ",[33,1175,1176],{},"open()","?",[14,1179,1180,1182],{},[33,1181,244],{}," means write mode. It creates a new file if needed and overwrites old content if the file already exists.",[637,1184,1170,1186,1173,1188,1177],{"id":1185},"what-does-r-mean-in-open",[33,1187,256],{},[33,1189,1176],{},[14,1191,1192,1194],{},[33,1193,256],{}," means read mode. It opens an existing file so you can read its contents.",[637,1196,1198,1199,1202,1203,1205],{"id":1197},"why-use-with-open-instead-of-open-by-itself","Why use ",[33,1200,1201],{},"with open()"," instead of ",[33,1204,1176],{}," by itself?",[14,1207,1208,1210],{},[33,1209,1201],{}," closes the file automatically, which is safer and easier for beginners.",[637,1212,1214],{"id":1213},"how-do-i-keep-existing-text-instead-of-replacing-it","How do I keep existing text instead of replacing it?",[14,1216,1217,1218,1220,1221,117],{},"Use append mode ",[33,1219,1083],{}," instead of write mode ",[33,1222,244],{},[637,1224,1226],{"id":1225},"why-is-my-text-all-on-one-line","Why is my text all on one line?",[14,1228,1229,1231,1232,1234],{},[33,1230,250],{}," does not add line breaks automatically. Add ",[33,1233,131],{}," where you want a new line.",[21,1236,1238],{"id":1237},"see-also","See also",[236,1240,1241,1246,1251,1256,1260,1264],{},[239,1242,1243],{},[274,1244,1245],{"href":597},"How to read a file in Python",[239,1247,1248],{},[274,1249,1250],{"href":496},"How to write to a file in Python",[239,1252,1253],{},[274,1254,1255],{"href":1161},"How to append to a file in Python",[239,1257,1258],{},[274,1259,282],{"href":281},[239,1261,1262],{},[274,1263,277],{"href":276},[239,1265,1266],{},[274,1267,1268,1269],{"href":884},"How to fix ",[33,1270,888],{},[1272,1273,1274],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":31,"searchDepth":61,"depth":61,"links":1276},[1277,1278,1279,1280,1281,1282,1283,1292,1293,1294,1304],{"id":23,"depth":61,"text":24},{"id":233,"depth":61,"text":234},{"id":285,"depth":61,"text":286},{"id":368,"depth":61,"text":369},{"id":500,"depth":61,"text":501},{"id":601,"depth":61,"text":602},{"id":634,"depth":61,"text":635,"children":1284},[1285,1286,1287,1288,1290],{"id":639,"depth":68,"text":640},{"id":720,"depth":68,"text":721},{"id":736,"depth":68,"text":737},{"id":788,"depth":68,"text":1289},"4. read() returns the file content as a string",{"id":800,"depth":68,"text":1291},"5. print() displays that string",{"id":826,"depth":61,"text":827},{"id":1049,"depth":61,"text":1050},{"id":1165,"depth":61,"text":1166,"children":1295},[1296,1298,1300,1302,1303],{"id":1169,"depth":68,"text":1297},"What does \"w\" mean in open()?",{"id":1185,"depth":68,"text":1299},"What does \"r\" mean in open()?",{"id":1197,"depth":68,"text":1301},"Why use with open() instead of open() by itself?",{"id":1213,"depth":68,"text":1214},{"id":1225,"depth":68,"text":1226},{"id":1237,"depth":61,"text":1238},"Master python read and write text file example in our comprehensive Python beginner guide.","md",{},"\u002Fexamples\u002Fpython-read-and-write-text-file-example",{"title":5,"description":1305},"examples\u002Fpython-read-and-write-text-file-example","HZk6PryjTKtz2CQuV-T9mAn12ptaoaP5OgD_IoWkeuo",1777585477859]