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