[{"data":1,"prerenderedAt":2824},["ShallowReactive",2],{"doc-\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix":3},{"id":4,"title":5,"body":6,"description":2817,"extension":2818,"meta":2819,"navigation":93,"path":2820,"seo":2821,"stem":2822,"__hash__":2823},"content\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix.md","FileNotFoundError: [Errno 2] No such file or directory (Fix)",{"type":7,"value":8,"toc":2774},"minimark",[9,19,27,42,58,61,278,281,286,289,292,306,309,335,351,355,361,382,385,424,428,431,505,511,647,650,685,688,692,695,700,703,706,734,740,744,747,750,775,778,782,788,821,824,836,840,843,905,908,970,974,977,981,1085,1090,1199,1208,1212,1215,1221,1290,1293,1437,1441,1444,1448,1510,1513,1517,1542,1546,1585,1591,1629,1637,1641,1697,1704,1708,1714,1718,1721,1725,1728,1731,1757,1760,1770,1774,1777,1779,1807,1810,1814,1817,1820,1826,1937,1940,1944,1947,1953,1958,2018,2022,2124,2130,2198,2202,2319,2323,2326,2353,2357,2360,2384,2421,2457,2493,2530,2534,2538,2544,2551,2554,2573,2577,2582,2596,2600,2603,2606,2699,2706,2710,2770],[10,11,13,14,18],"h1",{"id":12},"filenotfounderror-errno-2-no-such-file-or-directory-fix","FileNotFoundError: ",[15,16,17],"span",{},"Errno 2"," No such file or directory (Fix)",[20,21,22,26],"p",{},[23,24,25],"code",{},"FileNotFoundError: [Errno 2] No such file or directory"," means Python could not find the file or folder path you gave it.",[20,28,29,30,33,34,37,38,41],{},"This error is very common when working with ",[23,31,32],{},"open()",", ",[23,35,36],{},"os",", or ",[23,39,40],{},"pathlib",". In most cases, the problem is one of these:",[43,44,45,49,52,55],"ul",{},[46,47,48],"li",{},"The file name is wrong",[46,50,51],{},"The file is in a different folder",[46,53,54],{},"The path is relative, but Python is running from a different working directory",[46,56,57],{},"A folder in the path does not exist",[20,59,60],{},"If you want a fast way to check the path before opening the file, use this:",[62,63,68],"pre",{"className":64,"code":65,"language":66,"meta":67,"style":67},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif file_path.exists():\n    with open(file_path, \"r\", encoding=\"utf-8\") as file:\n        content = file.read()\n        print(content)\nelse:\n    print(f\"File not found: {file_path.resolve()}\")\n","python","",[23,69,70,88,95,126,131,149,203,221,234,242],{"__ignoreMap":67},[15,71,74,78,82,85],{"class":72,"line":73},"line",1,[15,75,77],{"class":76},"sVHd0","from",[15,79,81],{"class":80},"su5hD"," pathlib ",[15,83,84],{"class":76},"import",[15,86,87],{"class":80}," Path\n",[15,89,91],{"class":72,"line":90},2,[15,92,94],{"emptyLinePlaceholder":93},true,"\n",[15,96,98,101,105,109,113,117,121,123],{"class":72,"line":97},3,[15,99,100],{"class":80},"file_path ",[15,102,104],{"class":103},"smGrS","=",[15,106,108],{"class":107},"slqww"," Path",[15,110,112],{"class":111},"sP7_E","(",[15,114,116],{"class":115},"sjJ54","\"",[15,118,120],{"class":119},"s_sjI","data.txt",[15,122,116],{"class":115},[15,124,125],{"class":111},")\n",[15,127,129],{"class":72,"line":128},4,[15,130,94],{"emptyLinePlaceholder":93},[15,132,134,137,140,143,146],{"class":72,"line":133},5,[15,135,136],{"class":76},"if",[15,138,139],{"class":80}," file_path",[15,141,142],{"class":111},".",[15,144,145],{"class":107},"exists",[15,147,148],{"class":111},"():\n",[15,150,152,155,159,161,164,167,170,173,175,177,181,183,185,188,190,193,196,200],{"class":72,"line":151},6,[15,153,154],{"class":76},"    with",[15,156,158],{"class":157},"sptTA"," open",[15,160,112],{"class":111},[15,162,163],{"class":107},"file_path",[15,165,166],{"class":111},",",[15,168,169],{"class":115}," \"",[15,171,172],{"class":119},"r",[15,174,116],{"class":115},[15,176,166],{"class":111},[15,178,180],{"class":179},"s99_P"," encoding",[15,182,104],{"class":103},[15,184,116],{"class":115},[15,186,187],{"class":119},"utf-8",[15,189,116],{"class":115},[15,191,192],{"class":111},")",[15,194,195],{"class":76}," as",[15,197,199],{"class":198},"sMMDD"," file",[15,201,202],{"class":111},":\n",[15,204,206,209,211,213,215,218],{"class":72,"line":205},7,[15,207,208],{"class":80},"        content ",[15,210,104],{"class":103},[15,212,199],{"class":198},[15,214,142],{"class":111},[15,216,217],{"class":107},"read",[15,219,220],{"class":111},"()\n",[15,222,224,227,229,232],{"class":72,"line":223},8,[15,225,226],{"class":157},"        print",[15,228,112],{"class":111},[15,230,231],{"class":107},"content",[15,233,125],{"class":111},[15,235,237,240],{"class":72,"line":236},9,[15,238,239],{"class":76},"else",[15,241,202],{"class":111},[15,243,245,248,250,254,257,261,263,265,268,271,274,276],{"class":72,"line":244},10,[15,246,247],{"class":157},"    print",[15,249,112],{"class":111},[15,251,253],{"class":252},"sbsja","f",[15,255,256],{"class":119},"\"File not found: ",[15,258,260],{"class":259},"srdBf","{",[15,262,163],{"class":107},[15,264,142],{"class":111},[15,266,267],{"class":107},"resolve",[15,269,270],{"class":111},"()",[15,272,273],{"class":259},"}",[15,275,116],{"class":119},[15,277,125],{"class":111},[20,279,280],{},"Use this when you are not sure whether the file exists. It checks the path before opening it and shows the full path for debugging.",[282,283,285],"h2",{"id":284},"what-this-error-means","What this error means",[20,287,288],{},"This error means Python tried to access a path, but that path was not found.",[20,290,291],{},"That can happen when:",[43,293,294,297,300,303],{},[46,295,296],{},"The file does not exist",[46,298,299],{},"The folder does not exist",[46,301,302],{},"The path points to the wrong location",[46,304,305],{},"You expected Python to run from one folder, but it is actually running from another",[20,307,308],{},"You will often see this error with:",[43,310,311,315,320,325,330],{},[46,312,313],{},[23,314,32],{},[46,316,317],{},[23,318,319],{},"os.listdir()",[46,321,322],{},[23,323,324],{},"os.remove()",[46,326,327],{},[23,328,329],{},"pathlib.Path.read_text()",[46,331,332],{},[23,333,334],{},"pathlib.Path.open()",[20,336,337,338,346,347,142],{},"If you are new to file handling, it helps to first understand the ",[339,340,342,343,345],"a",{"href":341},"\u002Freference\u002Fpython-open-function-explained\u002F","Python ",[23,344,32],{}," function"," and ",[339,348,350],{"href":349},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python",[282,352,354],{"id":353},"when-this-error-usually-happens","When this error usually happens",[20,356,357,360],{},[23,358,359],{},"FileNotFoundError"," usually appears in one of these situations:",[43,362,363,366,373,376,379],{},[46,364,365],{},"You try to open a file that does not exist",[46,367,368,369,372],{},"You use a relative path like ",[23,370,371],{},"\"data.txt\"",", but Python is running from a different folder",[46,374,375],{},"You misspell the file name or extension",[46,377,378],{},"You try to access a folder path that does not exist yet",[46,380,381],{},"You build the path incorrectly with slashes or manual string concatenation",[20,383,384],{},"Common examples:",[43,386,387,397,407,418],{},[46,388,389,390,393,394],{},"Writing ",[23,391,392],{},"report.txt"," when the real file is ",[23,395,396],{},"reports.txt",[46,398,399,400,403,404],{},"Using ",[23,401,402],{},".txt"," when the file is really ",[23,405,406],{},".csv",[46,408,409,410,413,414,417],{},"Trying to write to ",[23,411,412],{},"output\u002Fresult.txt"," when the ",[23,415,416],{},"output"," folder does not exist",[46,419,399,420,423],{},[23,421,422],{},"folder\" + \"\u002F\" + filename"," and accidentally creating the wrong path",[282,425,427],{"id":426},"example-that-causes-the-error","Example that causes the error",[20,429,430],{},"Here is a minimal example:",[62,432,434],{"className":64,"code":433,"language":66,"meta":67,"style":67},"with open(\"missing.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[23,435,436,480,495],{"__ignoreMap":67},[15,437,438,441,443,445,447,450,452,454,456,458,460,462,464,466,468,470,472,474,476,478],{"class":72,"line":73},[15,439,440],{"class":76},"with",[15,442,158],{"class":157},[15,444,112],{"class":111},[15,446,116],{"class":115},[15,448,449],{"class":119},"missing.txt",[15,451,116],{"class":115},[15,453,166],{"class":111},[15,455,169],{"class":115},[15,457,172],{"class":119},[15,459,116],{"class":115},[15,461,166],{"class":111},[15,463,180],{"class":179},[15,465,104],{"class":103},[15,467,116],{"class":115},[15,469,187],{"class":119},[15,471,116],{"class":115},[15,473,192],{"class":111},[15,475,195],{"class":76},[15,477,199],{"class":198},[15,479,202],{"class":111},[15,481,482,485,487,489,491,493],{"class":72,"line":90},[15,483,484],{"class":80},"    content ",[15,486,104],{"class":103},[15,488,199],{"class":198},[15,490,142],{"class":111},[15,492,217],{"class":107},[15,494,220],{"class":111},[15,496,497,499,501,503],{"class":72,"line":97},[15,498,247],{"class":157},[15,500,112],{"class":111},[15,502,231],{"class":107},[15,504,125],{"class":111},[20,506,507,508,510],{},"If ",[23,509,449],{}," is not in the current working directory, Python raises:",[62,512,514],{"className":64,"code":513,"language":66,"meta":67,"style":67},"Traceback (most recent call last):\n  File \"example.py\", line 1, in \u003Cmodule>\n    with open(\"missing.txt\", \"r\", encoding=\"utf-8\") as file:\nFileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'\n",[23,515,516,529,563,605],{"__ignoreMap":67},[15,517,518,521,523,526],{"class":72,"line":73},[15,519,520],{"class":107},"Traceback ",[15,522,112],{"class":111},[15,524,525],{"class":107},"most recent call last",[15,527,528],{"class":111},"):\n",[15,530,531,534,536,539,541,543,546,549,551,554,557,560],{"class":72,"line":90},[15,532,533],{"class":80},"  File ",[15,535,116],{"class":115},[15,537,538],{"class":119},"example.py",[15,540,116],{"class":115},[15,542,166],{"class":111},[15,544,545],{"class":80}," line ",[15,547,548],{"class":259},"1",[15,550,166],{"class":111},[15,552,553],{"class":103}," in",[15,555,556],{"class":103}," \u003C",[15,558,559],{"class":80},"module",[15,561,562],{"class":103},">\n",[15,564,565,567,569,571,573,575,577,579,581,583,585,587,589,591,593,595,597,599,601,603],{"class":72,"line":97},[15,566,154],{"class":76},[15,568,158],{"class":157},[15,570,112],{"class":111},[15,572,116],{"class":115},[15,574,449],{"class":119},[15,576,116],{"class":115},[15,578,166],{"class":111},[15,580,169],{"class":115},[15,582,172],{"class":119},[15,584,116],{"class":115},[15,586,166],{"class":111},[15,588,180],{"class":179},[15,590,104],{"class":103},[15,592,116],{"class":115},[15,594,187],{"class":119},[15,596,116],{"class":115},[15,598,192],{"class":111},[15,600,195],{"class":76},[15,602,199],{"class":198},[15,604,202],{"class":111},[15,606,607,610,613,616,619,622,625,628,631,634,637,639,642,644],{"class":72,"line":128},[15,608,359],{"class":609},"sZMiF",[15,611,612],{"class":111},":",[15,614,615],{"class":111}," [",[15,617,618],{"class":80},"Errno ",[15,620,621],{"class":259},"2",[15,623,624],{"class":111},"]",[15,626,627],{"class":80}," No such ",[15,629,630],{"class":198},"file",[15,632,633],{"class":103}," or",[15,635,636],{"class":80}," directory",[15,638,612],{"class":111},[15,640,641],{"class":115}," '",[15,643,449],{"class":119},[15,645,646],{"class":115},"'\n",[20,648,649],{},"The important part is the last line:",[62,651,653],{"className":64,"code":652,"language":66,"meta":67,"style":67},"FileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'\n",[23,654,655],{"__ignoreMap":67},[15,656,657,659,661,663,665,667,669,671,673,675,677,679,681,683],{"class":72,"line":73},[15,658,359],{"class":609},[15,660,612],{"class":111},[15,662,615],{"class":111},[15,664,618],{"class":80},[15,666,621],{"class":259},[15,668,624],{"class":111},[15,670,627],{"class":80},[15,672,630],{"class":198},[15,674,633],{"class":103},[15,676,636],{"class":80},[15,678,612],{"class":111},[15,680,641],{"class":115},[15,682,449],{"class":119},[15,684,646],{"class":115},[20,686,687],{},"That tells you exactly which path Python failed to find.",[282,689,691],{"id":690},"how-to-fix-it","How to fix it",[20,693,694],{},"Here are the fastest ways to fix this error.",[696,697,699],"h3",{"id":698},"_1-check-that-the-file-really-exists","1. Check that the file really exists",[20,701,702],{},"Make sure the file is actually there.",[20,704,705],{},"For example, if your code uses:",[62,707,709],{"className":64,"code":708,"language":66,"meta":67,"style":67},"open(\"data.txt\", \"r\")\n",[23,710,711],{"__ignoreMap":67},[15,712,713,716,718,720,722,724,726,728,730,732],{"class":72,"line":73},[15,714,715],{"class":157},"open",[15,717,112],{"class":111},[15,719,116],{"class":115},[15,721,120],{"class":119},[15,723,116],{"class":115},[15,725,166],{"class":111},[15,727,169],{"class":115},[15,729,172],{"class":119},[15,731,116],{"class":115},[15,733,125],{"class":111},[20,735,736,737,739],{},"confirm that ",[23,738,120],{}," exists in the folder where Python is running.",[696,741,743],{"id":742},"_2-check-the-file-name-extension-and-capitalization","2. Check the file name, extension, and capitalization",[20,745,746],{},"Small differences matter.",[20,748,749],{},"Check for mistakes like:",[43,751,752,760,767],{},[46,753,754,756,757],{},[23,755,120],{}," vs ",[23,758,759],{},"Data.txt",[46,761,762,756,765],{},[23,763,764],{},"report.csv",[23,766,392],{},[46,768,769,756,772],{},[23,770,771],{},"image.PNG",[23,773,774],{},"image.png",[20,776,777],{},"On some systems, capitalization matters.",[696,779,781],{"id":780},"_3-print-the-current-working-directory","3. Print the current working directory",[20,783,784,785,787],{},"A relative path like ",[23,786,371],{}," depends on the current working directory, not always the script location.",[62,789,791],{"className":64,"code":790,"language":66,"meta":67,"style":67},"import os\n\nprint(os.getcwd())\n",[23,792,793,800,804],{"__ignoreMap":67},[15,794,795,797],{"class":72,"line":73},[15,796,84],{"class":76},[15,798,799],{"class":80}," os\n",[15,801,802],{"class":72,"line":90},[15,803,94],{"emptyLinePlaceholder":93},[15,805,806,809,811,813,815,818],{"class":72,"line":97},[15,807,808],{"class":157},"print",[15,810,112],{"class":111},[15,812,36],{"class":107},[15,814,142],{"class":111},[15,816,817],{"class":107},"getcwd",[15,819,820],{"class":111},"())\n",[20,822,823],{},"If this folder is not where your file is, Python will not find it.",[20,825,826,827,829,830,142],{},"If you are using the ",[23,828,36],{}," module, this is also a good time to review the ",[339,831,342,833,835],{"href":832},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F",[23,834,36],{}," module overview",[696,837,839],{"id":838},"_4-use-the-correct-absolute-or-relative-path","4. Use the correct absolute or relative path",[20,841,842],{},"If the file is in a subfolder, include that folder:",[62,844,846],{"className":64,"code":845,"language":66,"meta":67,"style":67},"with open(\"files\u002Fdata.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[23,847,848,891],{"__ignoreMap":67},[15,849,850,852,854,856,858,861,863,865,867,869,871,873,875,877,879,881,883,885,887,889],{"class":72,"line":73},[15,851,440],{"class":76},[15,853,158],{"class":157},[15,855,112],{"class":111},[15,857,116],{"class":115},[15,859,860],{"class":119},"files\u002Fdata.txt",[15,862,116],{"class":115},[15,864,166],{"class":111},[15,866,169],{"class":115},[15,868,172],{"class":119},[15,870,116],{"class":115},[15,872,166],{"class":111},[15,874,180],{"class":179},[15,876,104],{"class":103},[15,878,116],{"class":115},[15,880,187],{"class":119},[15,882,116],{"class":115},[15,884,192],{"class":111},[15,886,195],{"class":76},[15,888,199],{"class":198},[15,890,202],{"class":111},[15,892,893,895,897,899,901,903],{"class":72,"line":90},[15,894,247],{"class":157},[15,896,112],{"class":111},[15,898,630],{"class":198},[15,900,142],{"class":111},[15,902,217],{"class":107},[15,904,820],{"class":111},[20,906,907],{},"An absolute path also works:",[62,909,911],{"className":64,"code":910,"language":66,"meta":67,"style":67},"with open(\"\u002Ffull\u002Fpath\u002Fto\u002Fdata.txt\", \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[23,912,913,956],{"__ignoreMap":67},[15,914,915,917,919,921,923,926,928,930,932,934,936,938,940,942,944,946,948,950,952,954],{"class":72,"line":73},[15,916,440],{"class":76},[15,918,158],{"class":157},[15,920,112],{"class":111},[15,922,116],{"class":115},[15,924,925],{"class":119},"\u002Ffull\u002Fpath\u002Fto\u002Fdata.txt",[15,927,116],{"class":115},[15,929,166],{"class":111},[15,931,169],{"class":115},[15,933,172],{"class":119},[15,935,116],{"class":115},[15,937,166],{"class":111},[15,939,180],{"class":179},[15,941,104],{"class":103},[15,943,116],{"class":115},[15,945,187],{"class":119},[15,947,116],{"class":115},[15,949,192],{"class":111},[15,951,195],{"class":76},[15,953,199],{"class":198},[15,955,202],{"class":111},[15,957,958,960,962,964,966,968],{"class":72,"line":90},[15,959,247],{"class":157},[15,961,112],{"class":111},[15,963,630],{"class":198},[15,965,142],{"class":111},[15,967,217],{"class":107},[15,969,820],{"class":111},[696,971,973],{"id":972},"_5-build-paths-safely","5. Build paths safely",[20,975,976],{},"Avoid building paths by hand when possible.",[20,978,399,979,612],{},[23,980,40],{},[62,982,984],{"className":64,"code":983,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nfile_path = Path(\"files\") \u002F \"data.txt\"\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[23,985,986,996,1000,1029,1033,1071],{"__ignoreMap":67},[15,987,988,990,992,994],{"class":72,"line":73},[15,989,77],{"class":76},[15,991,81],{"class":80},[15,993,84],{"class":76},[15,995,87],{"class":80},[15,997,998],{"class":72,"line":90},[15,999,94],{"emptyLinePlaceholder":93},[15,1001,1002,1004,1006,1008,1010,1012,1015,1017,1019,1022,1024,1026],{"class":72,"line":97},[15,1003,100],{"class":80},[15,1005,104],{"class":103},[15,1007,108],{"class":107},[15,1009,112],{"class":111},[15,1011,116],{"class":115},[15,1013,1014],{"class":119},"files",[15,1016,116],{"class":115},[15,1018,192],{"class":111},[15,1020,1021],{"class":103}," \u002F",[15,1023,169],{"class":115},[15,1025,120],{"class":119},[15,1027,1028],{"class":115},"\"\n",[15,1030,1031],{"class":72,"line":128},[15,1032,94],{"emptyLinePlaceholder":93},[15,1034,1035,1037,1039,1041,1043,1045,1047,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067,1069],{"class":72,"line":133},[15,1036,440],{"class":76},[15,1038,158],{"class":157},[15,1040,112],{"class":111},[15,1042,163],{"class":107},[15,1044,166],{"class":111},[15,1046,169],{"class":115},[15,1048,172],{"class":119},[15,1050,116],{"class":115},[15,1052,166],{"class":111},[15,1054,180],{"class":179},[15,1056,104],{"class":103},[15,1058,116],{"class":115},[15,1060,187],{"class":119},[15,1062,116],{"class":115},[15,1064,192],{"class":111},[15,1066,195],{"class":76},[15,1068,199],{"class":198},[15,1070,202],{"class":111},[15,1072,1073,1075,1077,1079,1081,1083],{"class":72,"line":151},[15,1074,247],{"class":157},[15,1076,112],{"class":111},[15,1078,630],{"class":198},[15,1080,142],{"class":111},[15,1082,217],{"class":107},[15,1084,820],{"class":111},[20,1086,399,1087,612],{},[23,1088,1089],{},"os.path.join()",[62,1091,1093],{"className":64,"code":1092,"language":66,"meta":67,"style":67},"import os\n\nfile_path = os.path.join(\"files\", \"data.txt\")\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[23,1094,1095,1101,1105,1143,1147,1185],{"__ignoreMap":67},[15,1096,1097,1099],{"class":72,"line":73},[15,1098,84],{"class":76},[15,1100,799],{"class":80},[15,1102,1103],{"class":72,"line":90},[15,1104,94],{"emptyLinePlaceholder":93},[15,1106,1107,1109,1111,1114,1116,1120,1122,1125,1127,1129,1131,1133,1135,1137,1139,1141],{"class":72,"line":97},[15,1108,100],{"class":80},[15,1110,104],{"class":103},[15,1112,1113],{"class":80}," os",[15,1115,142],{"class":111},[15,1117,1119],{"class":1118},"skxfh","path",[15,1121,142],{"class":111},[15,1123,1124],{"class":107},"join",[15,1126,112],{"class":111},[15,1128,116],{"class":115},[15,1130,1014],{"class":119},[15,1132,116],{"class":115},[15,1134,166],{"class":111},[15,1136,169],{"class":115},[15,1138,120],{"class":119},[15,1140,116],{"class":115},[15,1142,125],{"class":111},[15,1144,1145],{"class":72,"line":128},[15,1146,94],{"emptyLinePlaceholder":93},[15,1148,1149,1151,1153,1155,1157,1159,1161,1163,1165,1167,1169,1171,1173,1175,1177,1179,1181,1183],{"class":72,"line":133},[15,1150,440],{"class":76},[15,1152,158],{"class":157},[15,1154,112],{"class":111},[15,1156,163],{"class":107},[15,1158,166],{"class":111},[15,1160,169],{"class":115},[15,1162,172],{"class":119},[15,1164,116],{"class":115},[15,1166,166],{"class":111},[15,1168,180],{"class":179},[15,1170,104],{"class":103},[15,1172,116],{"class":115},[15,1174,187],{"class":119},[15,1176,116],{"class":115},[15,1178,192],{"class":111},[15,1180,195],{"class":76},[15,1182,199],{"class":198},[15,1184,202],{"class":111},[15,1186,1187,1189,1191,1193,1195,1197],{"class":72,"line":151},[15,1188,247],{"class":157},[15,1190,112],{"class":111},[15,1192,630],{"class":198},[15,1194,142],{"class":111},[15,1196,217],{"class":107},[15,1198,820],{"class":111},[20,1200,1201,1202,142],{},"If you want a method-by-method explanation, see ",[339,1203,1205,1207],{"href":1204},"\u002Fstandard-library\u002Fos.path.join-function-explained",[23,1206,1089],{}," explained",[696,1209,1211],{"id":1210},"_6-create-the-folder-first-when-writing","6. Create the folder first when writing",[20,1213,1214],{},"If you write to a file inside a missing folder, Python cannot create the file until the folder exists.",[20,1216,1217,1218,1220],{},"This will fail if ",[23,1219,416],{}," does not exist:",[62,1222,1224],{"className":64,"code":1223,"language":66,"meta":67,"style":67},"with open(\"output\u002Fresult.txt\", \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[23,1225,1226,1269],{"__ignoreMap":67},[15,1227,1228,1230,1232,1234,1236,1238,1240,1242,1244,1247,1249,1251,1253,1255,1257,1259,1261,1263,1265,1267],{"class":72,"line":73},[15,1229,440],{"class":76},[15,1231,158],{"class":157},[15,1233,112],{"class":111},[15,1235,116],{"class":115},[15,1237,412],{"class":119},[15,1239,116],{"class":115},[15,1241,166],{"class":111},[15,1243,169],{"class":115},[15,1245,1246],{"class":119},"w",[15,1248,116],{"class":115},[15,1250,166],{"class":111},[15,1252,180],{"class":179},[15,1254,104],{"class":103},[15,1256,116],{"class":115},[15,1258,187],{"class":119},[15,1260,116],{"class":115},[15,1262,192],{"class":111},[15,1264,195],{"class":76},[15,1266,199],{"class":198},[15,1268,202],{"class":111},[15,1270,1271,1274,1276,1279,1281,1283,1286,1288],{"class":72,"line":90},[15,1272,1273],{"class":198},"    file",[15,1275,142],{"class":111},[15,1277,1278],{"class":107},"write",[15,1280,112],{"class":111},[15,1282,116],{"class":115},[15,1284,1285],{"class":119},"Hello",[15,1287,116],{"class":115},[15,1289,125],{"class":111},[20,1291,1292],{},"Fix it like this:",[62,1294,1296],{"className":64,"code":1295,"language":66,"meta":67,"style":67},"from pathlib import Path\n\noutput_dir = Path(\"output\")\noutput_dir.mkdir(exist_ok=True)\n\nfile_path = output_dir \u002F \"result.txt\"\n\nwith open(file_path, \"w\", encoding=\"utf-8\") as file:\n    file.write(\"Hello\")\n",[23,1297,1298,1308,1312,1331,1354,1358,1377,1381,1419],{"__ignoreMap":67},[15,1299,1300,1302,1304,1306],{"class":72,"line":73},[15,1301,77],{"class":76},[15,1303,81],{"class":80},[15,1305,84],{"class":76},[15,1307,87],{"class":80},[15,1309,1310],{"class":72,"line":90},[15,1311,94],{"emptyLinePlaceholder":93},[15,1313,1314,1317,1319,1321,1323,1325,1327,1329],{"class":72,"line":97},[15,1315,1316],{"class":80},"output_dir ",[15,1318,104],{"class":103},[15,1320,108],{"class":107},[15,1322,112],{"class":111},[15,1324,116],{"class":115},[15,1326,416],{"class":119},[15,1328,116],{"class":115},[15,1330,125],{"class":111},[15,1332,1333,1336,1338,1341,1343,1346,1348,1352],{"class":72,"line":128},[15,1334,1335],{"class":80},"output_dir",[15,1337,142],{"class":111},[15,1339,1340],{"class":107},"mkdir",[15,1342,112],{"class":111},[15,1344,1345],{"class":179},"exist_ok",[15,1347,104],{"class":103},[15,1349,1351],{"class":1350},"s39Yj","True",[15,1353,125],{"class":111},[15,1355,1356],{"class":72,"line":133},[15,1357,94],{"emptyLinePlaceholder":93},[15,1359,1360,1362,1364,1367,1370,1372,1375],{"class":72,"line":151},[15,1361,100],{"class":80},[15,1363,104],{"class":103},[15,1365,1366],{"class":80}," output_dir ",[15,1368,1369],{"class":103},"\u002F",[15,1371,169],{"class":115},[15,1373,1374],{"class":119},"result.txt",[15,1376,1028],{"class":115},[15,1378,1379],{"class":72,"line":205},[15,1380,94],{"emptyLinePlaceholder":93},[15,1382,1383,1385,1387,1389,1391,1393,1395,1397,1399,1401,1403,1405,1407,1409,1411,1413,1415,1417],{"class":72,"line":223},[15,1384,440],{"class":76},[15,1386,158],{"class":157},[15,1388,112],{"class":111},[15,1390,163],{"class":107},[15,1392,166],{"class":111},[15,1394,169],{"class":115},[15,1396,1246],{"class":119},[15,1398,116],{"class":115},[15,1400,166],{"class":111},[15,1402,180],{"class":179},[15,1404,104],{"class":103},[15,1406,116],{"class":115},[15,1408,187],{"class":119},[15,1410,116],{"class":115},[15,1412,192],{"class":111},[15,1414,195],{"class":76},[15,1416,199],{"class":198},[15,1418,202],{"class":111},[15,1420,1421,1423,1425,1427,1429,1431,1433,1435],{"class":72,"line":236},[15,1422,1273],{"class":198},[15,1424,142],{"class":111},[15,1426,1278],{"class":107},[15,1428,112],{"class":111},[15,1430,116],{"class":115},[15,1432,1285],{"class":119},[15,1434,116],{"class":115},[15,1436,125],{"class":111},[282,1438,1440],{"id":1439},"debugging-steps-to-try","Debugging steps to try",[20,1442,1443],{},"When you are not sure what is wrong, do these checks in order.",[696,1445,1447],{"id":1446},"print-the-exact-path-your-code-is-using","Print the exact path your code is using",[62,1449,1451],{"className":64,"code":1450,"language":66,"meta":67,"style":67},"from pathlib import Path\n\npath = Path(\"data.txt\")\nprint(path)\nprint(path.resolve())\n",[23,1452,1453,1463,1467,1486,1496],{"__ignoreMap":67},[15,1454,1455,1457,1459,1461],{"class":72,"line":73},[15,1456,77],{"class":76},[15,1458,81],{"class":80},[15,1460,84],{"class":76},[15,1462,87],{"class":80},[15,1464,1465],{"class":72,"line":90},[15,1466,94],{"emptyLinePlaceholder":93},[15,1468,1469,1472,1474,1476,1478,1480,1482,1484],{"class":72,"line":97},[15,1470,1471],{"class":80},"path ",[15,1473,104],{"class":103},[15,1475,108],{"class":107},[15,1477,112],{"class":111},[15,1479,116],{"class":115},[15,1481,120],{"class":119},[15,1483,116],{"class":115},[15,1485,125],{"class":111},[15,1487,1488,1490,1492,1494],{"class":72,"line":128},[15,1489,808],{"class":157},[15,1491,112],{"class":111},[15,1493,1119],{"class":107},[15,1495,125],{"class":111},[15,1497,1498,1500,1502,1504,1506,1508],{"class":72,"line":133},[15,1499,808],{"class":157},[15,1501,112],{"class":111},[15,1503,1119],{"class":107},[15,1505,142],{"class":111},[15,1507,267],{"class":107},[15,1509,820],{"class":111},[20,1511,1512],{},"This shows both the path you wrote and the full resolved path.",[696,1514,1516],{"id":1515},"print-the-current-working-directory","Print the current working directory",[62,1518,1520],{"className":64,"code":1519,"language":66,"meta":67,"style":67},"import os\nprint(os.getcwd())\n",[23,1521,1522,1528],{"__ignoreMap":67},[15,1523,1524,1526],{"class":72,"line":73},[15,1525,84],{"class":76},[15,1527,799],{"class":80},[15,1529,1530,1532,1534,1536,1538,1540],{"class":72,"line":90},[15,1531,808],{"class":157},[15,1533,112],{"class":111},[15,1535,36],{"class":107},[15,1537,142],{"class":111},[15,1539,817],{"class":107},[15,1541,820],{"class":111},[696,1543,1545],{"id":1544},"check-whether-the-file-exists","Check whether the file exists",[62,1547,1549],{"className":64,"code":1548,"language":66,"meta":67,"style":67},"from pathlib import Path\nprint(Path(\"data.txt\").exists())\n",[23,1550,1551,1561],{"__ignoreMap":67},[15,1552,1553,1555,1557,1559],{"class":72,"line":73},[15,1554,77],{"class":76},[15,1556,81],{"class":80},[15,1558,84],{"class":76},[15,1560,87],{"class":80},[15,1562,1563,1565,1567,1570,1572,1574,1576,1578,1581,1583],{"class":72,"line":90},[15,1564,808],{"class":157},[15,1566,112],{"class":111},[15,1568,1569],{"class":107},"Path",[15,1571,112],{"class":111},[15,1573,116],{"class":115},[15,1575,120],{"class":119},[15,1577,116],{"class":115},[15,1579,1580],{"class":111},").",[15,1582,145],{"class":107},[15,1584,820],{"class":111},[20,1586,1587,1588,612],{},"You can also use ",[23,1589,1590],{},"os.path.exists()",[62,1592,1594],{"className":64,"code":1593,"language":66,"meta":67,"style":67},"import os\nprint(os.path.exists(\"data.txt\"))\n",[23,1595,1596,1602],{"__ignoreMap":67},[15,1597,1598,1600],{"class":72,"line":73},[15,1599,84],{"class":76},[15,1601,799],{"class":80},[15,1603,1604,1606,1608,1610,1612,1614,1616,1618,1620,1622,1624,1626],{"class":72,"line":90},[15,1605,808],{"class":157},[15,1607,112],{"class":111},[15,1609,36],{"class":107},[15,1611,142],{"class":111},[15,1613,1119],{"class":1118},[15,1615,142],{"class":111},[15,1617,145],{"class":107},[15,1619,112],{"class":111},[15,1621,116],{"class":115},[15,1623,120],{"class":119},[15,1625,116],{"class":115},[15,1627,1628],{"class":111},"))\n",[20,1630,1631,1632,142],{},"If you want more detail, see ",[339,1633,1635,1207],{"href":1634},"\u002Fstandard-library\u002Fos.path.exists-function-explained",[23,1636,1590],{},[696,1638,1640],{"id":1639},"if-writing-a-file-check-the-parent-folder","If writing a file, check the parent folder",[62,1642,1644],{"className":64,"code":1643,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nfile_path = Path(\"output\u002Fresult.txt\")\nprint(file_path.parent.exists())\n",[23,1645,1646,1656,1660,1678],{"__ignoreMap":67},[15,1647,1648,1650,1652,1654],{"class":72,"line":73},[15,1649,77],{"class":76},[15,1651,81],{"class":80},[15,1653,84],{"class":76},[15,1655,87],{"class":80},[15,1657,1658],{"class":72,"line":90},[15,1659,94],{"emptyLinePlaceholder":93},[15,1661,1662,1664,1666,1668,1670,1672,1674,1676],{"class":72,"line":97},[15,1663,100],{"class":80},[15,1665,104],{"class":103},[15,1667,108],{"class":107},[15,1669,112],{"class":111},[15,1671,116],{"class":115},[15,1673,412],{"class":119},[15,1675,116],{"class":115},[15,1677,125],{"class":111},[15,1679,1680,1682,1684,1686,1688,1691,1693,1695],{"class":72,"line":128},[15,1681,808],{"class":157},[15,1683,112],{"class":111},[15,1685,163],{"class":107},[15,1687,142],{"class":111},[15,1689,1690],{"class":1118},"parent",[15,1692,142],{"class":111},[15,1694,145],{"class":107},[15,1696,820],{"class":111},[20,1698,1699,1700,1703],{},"If this prints ",[23,1701,1702],{},"False",", create the folder before writing.",[696,1705,1707],{"id":1706},"avoid-guessing-where-python-is-running-from","Avoid guessing where Python is running from",[20,1709,1710,1711,142],{},"Do not assume Python runs from the same folder as your script. Check it directly with ",[23,1712,1713],{},"os.getcwd()",[282,1715,1717],{"id":1716},"relative-path-vs-absolute-path","Relative path vs absolute path",[20,1719,1720],{},"This causes a lot of confusion for beginners.",[696,1722,1724],{"id":1723},"relative-path","Relative path",[20,1726,1727],{},"A relative path depends on the current working directory.",[20,1729,1730],{},"Example:",[62,1732,1733],{"className":64,"code":708,"language":66,"meta":67,"style":67},[23,1734,1735],{"__ignoreMap":67},[15,1736,1737,1739,1741,1743,1745,1747,1749,1751,1753,1755],{"class":72,"line":73},[15,1738,715],{"class":157},[15,1740,112],{"class":111},[15,1742,116],{"class":115},[15,1744,120],{"class":119},[15,1746,116],{"class":115},[15,1748,166],{"class":111},[15,1750,169],{"class":115},[15,1752,172],{"class":119},[15,1754,116],{"class":115},[15,1756,125],{"class":111},[20,1758,1759],{},"This means:",[43,1761,1762,1767],{},[46,1763,1764,1765],{},"Look for ",[23,1766,120],{},[46,1768,1769],{},"In the folder Python is currently running from",[696,1771,1773],{"id":1772},"absolute-path","Absolute path",[20,1775,1776],{},"An absolute path gives the full location of the file.",[20,1778,1730],{},[62,1780,1782],{"className":64,"code":1781,"language":66,"meta":67,"style":67},"open(\"\u002Fhome\u002Fuser\u002Fproject\u002Fdata.txt\", \"r\")\n",[23,1783,1784],{"__ignoreMap":67},[15,1785,1786,1788,1790,1792,1795,1797,1799,1801,1803,1805],{"class":72,"line":73},[15,1787,715],{"class":157},[15,1789,112],{"class":111},[15,1791,116],{"class":115},[15,1793,1794],{"class":119},"\u002Fhome\u002Fuser\u002Fproject\u002Fdata.txt",[15,1796,116],{"class":115},[15,1798,166],{"class":111},[15,1800,169],{"class":115},[15,1802,172],{"class":119},[15,1804,116],{"class":115},[15,1806,125],{"class":111},[20,1808,1809],{},"This does not depend on the current working directory.",[696,1811,1813],{"id":1812},"important-beginner-mistake","Important beginner mistake",[20,1815,1816],{},"Many beginners think file paths are relative to the script file.",[20,1818,1819],{},"Often, they are relative to where the program was started instead.",[20,1821,1822,1823,612],{},"If your file is next to your script, you can build the path from ",[23,1824,1825],{},"__file__",[62,1827,1829],{"className":64,"code":1828,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nscript_folder = Path(__file__).parent\nfile_path = script_folder \u002F \"data.txt\"\n\nwith open(file_path, \"r\", encoding=\"utf-8\") as file:\n    print(file.read())\n",[23,1830,1831,1841,1845,1864,1881,1885,1923],{"__ignoreMap":67},[15,1832,1833,1835,1837,1839],{"class":72,"line":73},[15,1834,77],{"class":76},[15,1836,81],{"class":80},[15,1838,84],{"class":76},[15,1840,87],{"class":80},[15,1842,1843],{"class":72,"line":90},[15,1844,94],{"emptyLinePlaceholder":93},[15,1846,1847,1850,1852,1854,1856,1859,1861],{"class":72,"line":97},[15,1848,1849],{"class":80},"script_folder ",[15,1851,104],{"class":103},[15,1853,108],{"class":107},[15,1855,112],{"class":111},[15,1857,1825],{"class":1858},"s_hVV",[15,1860,1580],{"class":111},[15,1862,1863],{"class":1118},"parent\n",[15,1865,1866,1868,1870,1873,1875,1877,1879],{"class":72,"line":128},[15,1867,100],{"class":80},[15,1869,104],{"class":103},[15,1871,1872],{"class":80}," script_folder ",[15,1874,1369],{"class":103},[15,1876,169],{"class":115},[15,1878,120],{"class":119},[15,1880,1028],{"class":115},[15,1882,1883],{"class":72,"line":133},[15,1884,94],{"emptyLinePlaceholder":93},[15,1886,1887,1889,1891,1893,1895,1897,1899,1901,1903,1905,1907,1909,1911,1913,1915,1917,1919,1921],{"class":72,"line":151},[15,1888,440],{"class":76},[15,1890,158],{"class":157},[15,1892,112],{"class":111},[15,1894,163],{"class":107},[15,1896,166],{"class":111},[15,1898,169],{"class":115},[15,1900,172],{"class":119},[15,1902,116],{"class":115},[15,1904,166],{"class":111},[15,1906,180],{"class":179},[15,1908,104],{"class":103},[15,1910,116],{"class":115},[15,1912,187],{"class":119},[15,1914,116],{"class":115},[15,1916,192],{"class":111},[15,1918,195],{"class":76},[15,1920,199],{"class":198},[15,1922,202],{"class":111},[15,1924,1925,1927,1929,1931,1933,1935],{"class":72,"line":205},[15,1926,247],{"class":157},[15,1928,112],{"class":111},[15,1930,630],{"class":198},[15,1932,142],{"class":111},[15,1934,217],{"class":107},[15,1936,820],{"class":111},[20,1938,1939],{},"This is one of the safest ways to access files stored beside your script.",[282,1941,1943],{"id":1942},"how-to-prevent-this-error","How to prevent this error",[20,1945,1946],{},"You cannot avoid every missing-file case, but you can make your code much safer.",[696,1948,1950,1951],{"id":1949},"use-pathlib","Use ",[23,1952,40],{},[20,1954,1955,1957],{},[23,1956,40],{}," makes paths easier to read and harder to build incorrectly.",[62,1959,1961],{"className":64,"code":1960,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nfile_path = Path(\"data\") \u002F \"report.txt\"\nprint(file_path.exists())\n",[23,1962,1963,1973,1977,2004],{"__ignoreMap":67},[15,1964,1965,1967,1969,1971],{"class":72,"line":73},[15,1966,77],{"class":76},[15,1968,81],{"class":80},[15,1970,84],{"class":76},[15,1972,87],{"class":80},[15,1974,1975],{"class":72,"line":90},[15,1976,94],{"emptyLinePlaceholder":93},[15,1978,1979,1981,1983,1985,1987,1989,1992,1994,1996,1998,2000,2002],{"class":72,"line":97},[15,1980,100],{"class":80},[15,1982,104],{"class":103},[15,1984,108],{"class":107},[15,1986,112],{"class":111},[15,1988,116],{"class":115},[15,1990,1991],{"class":119},"data",[15,1993,116],{"class":115},[15,1995,192],{"class":111},[15,1997,1021],{"class":103},[15,1999,169],{"class":115},[15,2001,392],{"class":119},[15,2003,1028],{"class":115},[15,2005,2006,2008,2010,2012,2014,2016],{"class":72,"line":128},[15,2007,808],{"class":157},[15,2009,112],{"class":111},[15,2011,163],{"class":107},[15,2013,142],{"class":111},[15,2015,145],{"class":107},[15,2017,820],{"class":111},[696,2019,2021],{"id":2020},"check-file-existence-before-reading","Check file existence before reading",[62,2023,2025],{"className":64,"code":2024,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nfile_path = Path(\"data.txt\")\n\nif file_path.exists():\n    print(file_path.read_text(encoding=\"utf-8\"))\nelse:\n    print(\"File not found\")\n",[23,2026,2027,2037,2041,2059,2063,2075,2103,2109],{"__ignoreMap":67},[15,2028,2029,2031,2033,2035],{"class":72,"line":73},[15,2030,77],{"class":76},[15,2032,81],{"class":80},[15,2034,84],{"class":76},[15,2036,87],{"class":80},[15,2038,2039],{"class":72,"line":90},[15,2040,94],{"emptyLinePlaceholder":93},[15,2042,2043,2045,2047,2049,2051,2053,2055,2057],{"class":72,"line":97},[15,2044,100],{"class":80},[15,2046,104],{"class":103},[15,2048,108],{"class":107},[15,2050,112],{"class":111},[15,2052,116],{"class":115},[15,2054,120],{"class":119},[15,2056,116],{"class":115},[15,2058,125],{"class":111},[15,2060,2061],{"class":72,"line":128},[15,2062,94],{"emptyLinePlaceholder":93},[15,2064,2065,2067,2069,2071,2073],{"class":72,"line":133},[15,2066,136],{"class":76},[15,2068,139],{"class":80},[15,2070,142],{"class":111},[15,2072,145],{"class":107},[15,2074,148],{"class":111},[15,2076,2077,2079,2081,2083,2085,2088,2090,2093,2095,2097,2099,2101],{"class":72,"line":151},[15,2078,247],{"class":157},[15,2080,112],{"class":111},[15,2082,163],{"class":107},[15,2084,142],{"class":111},[15,2086,2087],{"class":107},"read_text",[15,2089,112],{"class":111},[15,2091,2092],{"class":179},"encoding",[15,2094,104],{"class":103},[15,2096,116],{"class":115},[15,2098,187],{"class":119},[15,2100,116],{"class":115},[15,2102,1628],{"class":111},[15,2104,2105,2107],{"class":72,"line":205},[15,2106,239],{"class":76},[15,2108,202],{"class":111},[15,2110,2111,2113,2115,2117,2120,2122],{"class":72,"line":223},[15,2112,247],{"class":157},[15,2114,112],{"class":111},[15,2116,116],{"class":115},[15,2118,2119],{"class":119},"File not found",[15,2121,116],{"class":115},[15,2123,125],{"class":111},[696,2125,1950,2127,2129],{"id":2126},"use-__file__-when-the-file-is-next-to-your-script",[23,2128,1825],{}," when the file is next to your script",[62,2131,2133],{"className":64,"code":2132,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nbase_dir = Path(__file__).parent\nfile_path = base_dir \u002F \"config.json\"\n\nprint(file_path)\n",[23,2134,2135,2145,2149,2166,2184,2188],{"__ignoreMap":67},[15,2136,2137,2139,2141,2143],{"class":72,"line":73},[15,2138,77],{"class":76},[15,2140,81],{"class":80},[15,2142,84],{"class":76},[15,2144,87],{"class":80},[15,2146,2147],{"class":72,"line":90},[15,2148,94],{"emptyLinePlaceholder":93},[15,2150,2151,2154,2156,2158,2160,2162,2164],{"class":72,"line":97},[15,2152,2153],{"class":80},"base_dir ",[15,2155,104],{"class":103},[15,2157,108],{"class":107},[15,2159,112],{"class":111},[15,2161,1825],{"class":1858},[15,2163,1580],{"class":111},[15,2165,1863],{"class":1118},[15,2167,2168,2170,2172,2175,2177,2179,2182],{"class":72,"line":128},[15,2169,100],{"class":80},[15,2171,104],{"class":103},[15,2173,2174],{"class":80}," base_dir ",[15,2176,1369],{"class":103},[15,2178,169],{"class":115},[15,2180,2181],{"class":119},"config.json",[15,2183,1028],{"class":115},[15,2185,2186],{"class":72,"line":133},[15,2187,94],{"emptyLinePlaceholder":93},[15,2189,2190,2192,2194,2196],{"class":72,"line":151},[15,2191,808],{"class":157},[15,2193,112],{"class":111},[15,2195,163],{"class":107},[15,2197,125],{"class":111},[696,2199,2201],{"id":2200},"create-missing-folders-before-writing","Create missing folders before writing",[62,2203,2205],{"className":64,"code":2204,"language":66,"meta":67,"style":67},"from pathlib import Path\n\nfolder = Path(\"logs\")\nfolder.mkdir(exist_ok=True)\n\nlog_file = folder \u002F \"app.log\"\nlog_file.write_text(\"Started\\n\", encoding=\"utf-8\")\n",[23,2206,2207,2217,2221,2241,2260,2264,2283],{"__ignoreMap":67},[15,2208,2209,2211,2213,2215],{"class":72,"line":73},[15,2210,77],{"class":76},[15,2212,81],{"class":80},[15,2214,84],{"class":76},[15,2216,87],{"class":80},[15,2218,2219],{"class":72,"line":90},[15,2220,94],{"emptyLinePlaceholder":93},[15,2222,2223,2226,2228,2230,2232,2234,2237,2239],{"class":72,"line":97},[15,2224,2225],{"class":80},"folder ",[15,2227,104],{"class":103},[15,2229,108],{"class":107},[15,2231,112],{"class":111},[15,2233,116],{"class":115},[15,2235,2236],{"class":119},"logs",[15,2238,116],{"class":115},[15,2240,125],{"class":111},[15,2242,2243,2246,2248,2250,2252,2254,2256,2258],{"class":72,"line":128},[15,2244,2245],{"class":80},"folder",[15,2247,142],{"class":111},[15,2249,1340],{"class":107},[15,2251,112],{"class":111},[15,2253,1345],{"class":179},[15,2255,104],{"class":103},[15,2257,1351],{"class":1350},[15,2259,125],{"class":111},[15,2261,2262],{"class":72,"line":133},[15,2263,94],{"emptyLinePlaceholder":93},[15,2265,2266,2269,2271,2274,2276,2278,2281],{"class":72,"line":151},[15,2267,2268],{"class":80},"log_file ",[15,2270,104],{"class":103},[15,2272,2273],{"class":80}," folder ",[15,2275,1369],{"class":103},[15,2277,169],{"class":115},[15,2279,2280],{"class":119},"app.log",[15,2282,1028],{"class":115},[15,2284,2285,2288,2290,2293,2295,2297,2300,2303,2305,2307,2309,2311,2313,2315,2317],{"class":72,"line":205},[15,2286,2287],{"class":80},"log_file",[15,2289,142],{"class":111},[15,2291,2292],{"class":107},"write_text",[15,2294,112],{"class":111},[15,2296,116],{"class":115},[15,2298,2299],{"class":119},"Started",[15,2301,2302],{"class":1858},"\\n",[15,2304,116],{"class":115},[15,2306,166],{"class":111},[15,2308,180],{"class":179},[15,2310,104],{"class":103},[15,2312,116],{"class":115},[15,2314,187],{"class":119},[15,2316,116],{"class":115},[15,2318,125],{"class":111},[282,2320,2322],{"id":2321},"common-causes","Common causes",[20,2324,2325],{},"Here are the most common reasons this error happens:",[43,2327,2328,2331,2338,2341,2344,2347,2350],{},[46,2329,2330],{},"Wrong file name",[46,2332,2333,2334,756,2336],{},"Wrong file extension such as ",[23,2335,402],{},[23,2337,406],{},[46,2339,2340],{},"File is in a different folder",[46,2342,2343],{},"Current working directory is not the script folder",[46,2345,2346],{},"Missing parent directory when writing a file",[46,2348,2349],{},"Incorrect path separators or manually joined path strings",[46,2351,2352],{},"Capitalization mismatch on case-sensitive systems",[282,2354,2356],{"id":2355},"useful-debugging-commands","Useful debugging commands",[20,2358,2359],{},"These short commands can help you find the problem quickly:",[62,2361,2362],{"className":64,"code":1519,"language":66,"meta":67,"style":67},[23,2363,2364,2370],{"__ignoreMap":67},[15,2365,2366,2368],{"class":72,"line":73},[15,2367,84],{"class":76},[15,2369,799],{"class":80},[15,2371,2372,2374,2376,2378,2380,2382],{"class":72,"line":90},[15,2373,808],{"class":157},[15,2375,112],{"class":111},[15,2377,36],{"class":107},[15,2379,142],{"class":111},[15,2381,817],{"class":107},[15,2383,820],{"class":111},[62,2385,2387],{"className":64,"code":2386,"language":66,"meta":67,"style":67},"from pathlib import Path\nprint(Path(\"data.txt\").resolve())\n",[23,2388,2389,2399],{"__ignoreMap":67},[15,2390,2391,2393,2395,2397],{"class":72,"line":73},[15,2392,77],{"class":76},[15,2394,81],{"class":80},[15,2396,84],{"class":76},[15,2398,87],{"class":80},[15,2400,2401,2403,2405,2407,2409,2411,2413,2415,2417,2419],{"class":72,"line":90},[15,2402,808],{"class":157},[15,2404,112],{"class":111},[15,2406,1569],{"class":107},[15,2408,112],{"class":111},[15,2410,116],{"class":115},[15,2412,120],{"class":119},[15,2414,116],{"class":115},[15,2416,1580],{"class":111},[15,2418,267],{"class":107},[15,2420,820],{"class":111},[62,2422,2423],{"className":64,"code":1548,"language":66,"meta":67,"style":67},[23,2424,2425,2435],{"__ignoreMap":67},[15,2426,2427,2429,2431,2433],{"class":72,"line":73},[15,2428,77],{"class":76},[15,2430,81],{"class":80},[15,2432,84],{"class":76},[15,2434,87],{"class":80},[15,2436,2437,2439,2441,2443,2445,2447,2449,2451,2453,2455],{"class":72,"line":90},[15,2438,808],{"class":157},[15,2440,112],{"class":111},[15,2442,1569],{"class":107},[15,2444,112],{"class":111},[15,2446,116],{"class":115},[15,2448,120],{"class":119},[15,2450,116],{"class":115},[15,2452,1580],{"class":111},[15,2454,145],{"class":107},[15,2456,820],{"class":111},[62,2458,2459],{"className":64,"code":1593,"language":66,"meta":67,"style":67},[23,2460,2461,2467],{"__ignoreMap":67},[15,2462,2463,2465],{"class":72,"line":73},[15,2464,84],{"class":76},[15,2466,799],{"class":80},[15,2468,2469,2471,2473,2475,2477,2479,2481,2483,2485,2487,2489,2491],{"class":72,"line":90},[15,2470,808],{"class":157},[15,2472,112],{"class":111},[15,2474,36],{"class":107},[15,2476,142],{"class":111},[15,2478,1119],{"class":1118},[15,2480,142],{"class":111},[15,2482,145],{"class":107},[15,2484,112],{"class":111},[15,2486,116],{"class":115},[15,2488,120],{"class":119},[15,2490,116],{"class":115},[15,2492,1628],{"class":111},[62,2494,2496],{"className":64,"code":2495,"language":66,"meta":67,"style":67},"from pathlib import Path\nprint(Path(\"output\").exists())\n",[23,2497,2498,2508],{"__ignoreMap":67},[15,2499,2500,2502,2504,2506],{"class":72,"line":73},[15,2501,77],{"class":76},[15,2503,81],{"class":80},[15,2505,84],{"class":76},[15,2507,87],{"class":80},[15,2509,2510,2512,2514,2516,2518,2520,2522,2524,2526,2528],{"class":72,"line":90},[15,2511,808],{"class":157},[15,2513,112],{"class":111},[15,2515,1569],{"class":107},[15,2517,112],{"class":111},[15,2519,116],{"class":115},[15,2521,416],{"class":119},[15,2523,116],{"class":115},[15,2525,1580],{"class":111},[15,2527,145],{"class":107},[15,2529,820],{"class":111},[282,2531,2533],{"id":2532},"faq","FAQ",[696,2535,2537],{"id":2536},"why-do-i-get-filenotfounderror-even-though-the-file-is-in-my-project-folder","Why do I get FileNotFoundError even though the file is in my project folder?",[20,2539,2540,2541,2543],{},"Your program may be running from a different working directory. Check ",[23,2542,1713],{}," and compare it to the real file location.",[696,2545,2547,2548,2550],{"id":2546},"does-open-create-a-file-if-it-does-not-exist","Does ",[23,2549,32],{}," create a file if it does not exist?",[20,2552,2553],{},"It depends on the mode:",[43,2555,2556,2564,2570],{},[46,2557,2558,2561,2562],{},[23,2559,2560],{},"\"r\""," raises ",[23,2563,359],{},[46,2565,2566,2569],{},[23,2567,2568],{},"\"w\""," creates the file if needed",[46,2571,2572],{},"But the parent folder must already exist",[696,2574,2576],{"id":2575},"what-is-the-difference-between-filenotfounderror-and-permissionerror","What is the difference between FileNotFoundError and PermissionError?",[20,2578,2579,2581],{},[23,2580,359],{}," means Python cannot find the path.",[20,2583,2584,2587,2588,142],{},[23,2585,2586],{},"PermissionError"," means the path exists, but Python is not allowed to access it. See ",[339,2589,2591,2592,2595],{"href":2590},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","PermissionError: ",[15,2593,2594],{},"Errno 13"," Permission denied (Fix)",[696,2597,2599],{"id":2598},"should-i-use-try-except-for-this-error","Should I use try-except for this error?",[20,2601,2602],{},"Yes, if the file may genuinely be missing.",[20,2604,2605],{},"For example:",[62,2607,2609],{"className":64,"code":2608,"language":66,"meta":67,"style":67},"try:\n    with open(\"data.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"The file was not found.\")\n",[23,2610,2611,2618,2660,2674,2684],{"__ignoreMap":67},[15,2612,2613,2616],{"class":72,"line":73},[15,2614,2615],{"class":76},"try",[15,2617,202],{"class":111},[15,2619,2620,2622,2624,2626,2628,2630,2632,2634,2636,2638,2640,2642,2644,2646,2648,2650,2652,2654,2656,2658],{"class":72,"line":90},[15,2621,154],{"class":76},[15,2623,158],{"class":157},[15,2625,112],{"class":111},[15,2627,116],{"class":115},[15,2629,120],{"class":119},[15,2631,116],{"class":115},[15,2633,166],{"class":111},[15,2635,169],{"class":115},[15,2637,172],{"class":119},[15,2639,116],{"class":115},[15,2641,166],{"class":111},[15,2643,180],{"class":179},[15,2645,104],{"class":103},[15,2647,116],{"class":115},[15,2649,187],{"class":119},[15,2651,116],{"class":115},[15,2653,192],{"class":111},[15,2655,195],{"class":76},[15,2657,199],{"class":198},[15,2659,202],{"class":111},[15,2661,2662,2664,2666,2668,2670,2672],{"class":72,"line":97},[15,2663,226],{"class":157},[15,2665,112],{"class":111},[15,2667,630],{"class":198},[15,2669,142],{"class":111},[15,2671,217],{"class":107},[15,2673,820],{"class":111},[15,2675,2676,2679,2682],{"class":72,"line":128},[15,2677,2678],{"class":76},"except",[15,2680,2681],{"class":609}," FileNotFoundError",[15,2683,202],{"class":111},[15,2685,2686,2688,2690,2692,2695,2697],{"class":72,"line":133},[15,2687,247],{"class":157},[15,2689,112],{"class":111},[15,2691,116],{"class":115},[15,2693,2694],{"class":119},"The file was not found.",[15,2696,116],{"class":115},[15,2698,125],{"class":111},[20,2700,2701,2702,2705],{},"But do not use ",[23,2703,2704],{},"try-except"," as a substitute for checking your path logic. You should still find out why the path is wrong.",[282,2707,2709],{"id":2708},"see-also","See also",[43,2711,2712,2718,2723,2730,2736,2742,2748,2754,2764],{},[46,2713,2714],{},[339,2715,2717],{"href":2716},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","How to read a file in Python",[46,2719,2720],{},[339,2721,2722],{"href":349},"Working with file paths in Python",[46,2724,2725],{},[339,2726,342,2727,2729],{"href":341},[23,2728,32],{}," function explained",[46,2731,2732],{},[339,2733,342,2734,835],{"href":832},[23,2735,36],{},[46,2737,2738],{},[339,2739,2740,2729],{"href":1634},[23,2741,1590],{},[46,2743,2744],{},[339,2745,2746,2729],{"href":1204},[23,2747,1089],{},[46,2749,2750],{},[339,2751,2591,2752,2595],{"href":2590},[15,2753,2594],{},[46,2755,2756],{},[339,2757,2759,2760,2763],{"href":2758},"\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix\u002F","IsADirectoryError: ",[15,2761,2762],{},"Errno 21"," Is a directory (Fix)",[46,2765,2766],{},[339,2767,2769],{"href":2768},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes\u002F","FileNotFoundError in Python: causes and fixes",[2771,2772,2773],"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 .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 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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--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 .sMMDD, html code.shiki .sMMDD{--shiki-light:#90A4AE;--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":67,"searchDepth":90,"depth":90,"links":2775},[2776,2777,2778,2779,2787,2794,2799,2807,2808,2809,2816],{"id":284,"depth":90,"text":285},{"id":353,"depth":90,"text":354},{"id":426,"depth":90,"text":427},{"id":690,"depth":90,"text":691,"children":2780},[2781,2782,2783,2784,2785,2786],{"id":698,"depth":97,"text":699},{"id":742,"depth":97,"text":743},{"id":780,"depth":97,"text":781},{"id":838,"depth":97,"text":839},{"id":972,"depth":97,"text":973},{"id":1210,"depth":97,"text":1211},{"id":1439,"depth":90,"text":1440,"children":2788},[2789,2790,2791,2792,2793],{"id":1446,"depth":97,"text":1447},{"id":1515,"depth":97,"text":1516},{"id":1544,"depth":97,"text":1545},{"id":1639,"depth":97,"text":1640},{"id":1706,"depth":97,"text":1707},{"id":1716,"depth":90,"text":1717,"children":2795},[2796,2797,2798],{"id":1723,"depth":97,"text":1724},{"id":1772,"depth":97,"text":1773},{"id":1812,"depth":97,"text":1813},{"id":1942,"depth":90,"text":1943,"children":2800},[2801,2803,2804,2806],{"id":1949,"depth":97,"text":2802},"Use pathlib",{"id":2020,"depth":97,"text":2021},{"id":2126,"depth":97,"text":2805},"Use __file__ when the file is next to your script",{"id":2200,"depth":97,"text":2201},{"id":2321,"depth":90,"text":2322},{"id":2355,"depth":90,"text":2356},{"id":2532,"depth":90,"text":2533,"children":2810},[2811,2812,2814,2815],{"id":2536,"depth":97,"text":2537},{"id":2546,"depth":97,"text":2813},"Does open() create a file if it does not exist?",{"id":2575,"depth":97,"text":2576},{"id":2598,"depth":97,"text":2599},{"id":2708,"depth":90,"text":2709},"Master filenotfounderror errno 2 no such file or directory fix in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix",{"title":5,"description":2817},"errors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix","qJC1LyTxKs22SEV5n8fq35n0dJ58p_QYJ122KpTY23w",1777585498472]