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