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