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