[{"data":1,"prerenderedAt":1389},["ShallowReactive",2],{"doc-\u002Ferrors\u002Foserror-in-python-causes-and-fixes":3},{"id":4,"title":5,"body":6,"description":1382,"extension":1383,"meta":1384,"navigation":86,"path":1385,"seo":1386,"stem":1387,"__hash__":1388},"content\u002Ferrors\u002Foserror-in-python-causes-and-fixes.md","OSError in Python: Causes and Fixes",{"type":7,"value":8,"toc":1365},"minimark",[9,13,21,24,40,43,56,59,248,254,259,264,267,281,287,309,315,319,324,344,347,370,374,377,383,404,407,431,435,438,470,473,496,505,509,512,590,593,638,646,654,661,665,670,779,782,785,824,827,918,921,925,928,1061,1064,1096,1099,1204,1207,1219,1223,1228,1231,1234,1252,1255,1266,1269,1289,1293,1298,1305,1309,1312,1317,1321,1324,1328,1331,1335,1361],[10,11,5],"h1",{"id":12},"oserror-in-python-causes-and-fixes",[14,15,16,20],"p",{},[17,18,19],"code",{},"OSError"," is a general Python error for operating system related problems.",[14,22,23],{},"You will often see it when your code works with:",[25,26,27,31,34,37],"ul",{},[28,29,30],"li",{},"files",[28,32,33],{},"folders",[28,35,36],{},"paths",[28,38,39],{},"permissions",[14,41,42],{},"For beginners, the most important thing to know is this:",[25,44,45,50],{},[28,46,47,49],{},[17,48,19],{}," usually means something is wrong with the path, file, folder, or access",[28,51,52,53,55],{},"the error message after ",[17,54,19],{}," usually tells you what the real problem is",[14,57,58],{},"A quick way to start debugging is to check whether the path actually exists before using it.",[60,61,66],"pre",{"className":62,"code":63,"language":64,"meta":65,"style":65},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import os\n\npath = 'example.txt'\n\nif os.path.exists(path):\n    with open(path, 'r', encoding='utf-8') as file:\n        print(file.read())\nelse:\n    print('File not found:', path)\n","python","",[17,67,68,81,88,109,114,145,198,217,225],{"__ignoreMap":65},[69,70,73,77],"span",{"class":71,"line":72},"line",1,[69,74,76],{"class":75},"sVHd0","import",[69,78,80],{"class":79},"su5hD"," os\n",[69,82,84],{"class":71,"line":83},2,[69,85,87],{"emptyLinePlaceholder":86},true,"\n",[69,89,91,94,98,102,106],{"class":71,"line":90},3,[69,92,93],{"class":79},"path ",[69,95,97],{"class":96},"smGrS","=",[69,99,101],{"class":100},"sjJ54"," '",[69,103,105],{"class":104},"s_sjI","example.txt",[69,107,108],{"class":100},"'\n",[69,110,112],{"class":71,"line":111},4,[69,113,87],{"emptyLinePlaceholder":86},[69,115,117,120,123,127,131,133,137,140,142],{"class":71,"line":116},5,[69,118,119],{"class":75},"if",[69,121,122],{"class":79}," os",[69,124,126],{"class":125},"sP7_E",".",[69,128,130],{"class":129},"skxfh","path",[69,132,126],{"class":125},[69,134,136],{"class":135},"slqww","exists",[69,138,139],{"class":125},"(",[69,141,130],{"class":135},[69,143,144],{"class":125},"):\n",[69,146,148,151,155,157,159,162,164,167,170,172,176,178,180,183,185,188,191,195],{"class":71,"line":147},6,[69,149,150],{"class":75},"    with",[69,152,154],{"class":153},"sptTA"," open",[69,156,139],{"class":125},[69,158,130],{"class":135},[69,160,161],{"class":125},",",[69,163,101],{"class":100},[69,165,166],{"class":104},"r",[69,168,169],{"class":100},"'",[69,171,161],{"class":125},[69,173,175],{"class":174},"s99_P"," encoding",[69,177,97],{"class":96},[69,179,169],{"class":100},[69,181,182],{"class":104},"utf-8",[69,184,169],{"class":100},[69,186,187],{"class":125},")",[69,189,190],{"class":75}," as",[69,192,194],{"class":193},"sMMDD"," file",[69,196,197],{"class":125},":\n",[69,199,201,204,206,209,211,214],{"class":71,"line":200},7,[69,202,203],{"class":153},"        print",[69,205,139],{"class":125},[69,207,208],{"class":193},"file",[69,210,126],{"class":125},[69,212,213],{"class":135},"read",[69,215,216],{"class":125},"())\n",[69,218,220,223],{"class":71,"line":219},8,[69,221,222],{"class":75},"else",[69,224,197],{"class":125},[69,226,228,231,233,235,238,240,242,245],{"class":71,"line":227},9,[69,229,230],{"class":153},"    print",[69,232,139],{"class":125},[69,234,169],{"class":100},[69,236,237],{"class":104},"File not found:",[69,239,169],{"class":100},[69,241,161],{"class":125},[69,243,244],{"class":135}," path",[69,246,247],{"class":125},")\n",[14,249,250,251,253],{},"This helps with many common cases. A lot of ",[17,252,19],{}," problems happen because the path is wrong, missing, or points to the wrong thing.",[255,256,258],"h2",{"id":257},"what-oserror-means","What OSError means",[14,260,261,263],{},[17,262,19],{}," is a broad error category for operating system related problems.",[14,265,266],{},"In practice, that usually means Python failed while trying to do something like:",[25,268,269,272,275,278],{},[28,270,271],{},"open a file",[28,273,274],{},"create or remove a folder",[28,276,277],{},"rename a path",[28,279,280],{},"access a location without permission",[14,282,283,284,286],{},"Also, many common errors are part of the ",[17,285,19],{}," family, including:",[25,288,289,294,299,304],{},[28,290,291],{},[17,292,293],{},"FileNotFoundError",[28,295,296],{},[17,297,298],{},"PermissionError",[28,300,301],{},[17,302,303],{},"IsADirectoryError",[28,305,306],{},[17,307,308],{},"NotADirectoryError",[14,310,311,312,314],{},"So even if you do not see plain ",[17,313,19],{},", you may still be dealing with the same family of problem.",[255,316,318],{"id":317},"when-oserror-happens","When OSError happens",[14,320,321,323],{},[17,322,19],{}," and its subclasses often happen in situations like these:",[25,325,326,329,332,335,338,341],{},[28,327,328],{},"Opening a file that does not exist",[28,330,331],{},"Trying to write to a location without permission",[28,333,334],{},"Using a directory path where a file path is expected",[28,336,337],{},"Using a file path where a directory path is expected",[28,339,340],{},"Renaming, deleting, or listing invalid paths",[28,342,343],{},"Working with broken or platform-specific paths",[14,345,346],{},"For example:",[25,348,349,357,365],{},[28,350,351,354,355],{},[17,352,353],{},"open(\"missing.txt\")"," may raise ",[17,356,293],{},[28,358,359,360,354,363],{},"opening a folder with ",[17,361,362],{},"open()",[17,364,303],{},[28,366,367,368],{},"writing to a protected location may raise ",[17,369,298],{},[255,371,373],{"id":372},"read-the-error-message-carefully","Read the error message carefully",[14,375,376],{},"The traceback usually gives the best clue.",[14,378,379,380,382],{},"When you see an ",[17,381,19],{},", check these things first:",[25,384,385,395,398,401],{},[28,386,387,388,391,392],{},"The error number, such as ",[17,389,390],{},"Errno 2"," or ",[17,393,394],{},"Errno 13",[28,396,397],{},"The exact path shown in the error",[28,399,400],{},"Whether Python expected a file or a directory",[28,402,403],{},"Whether the problem is a missing path, wrong path, or blocked access",[14,405,406],{},"A few common examples:",[25,408,409,414,419,425],{},[28,410,411,413],{},[17,412,390],{}," often means the file or folder was not found",[28,415,416,418],{},[17,417,394],{}," often means permission was denied",[28,420,421,424],{},[17,422,423],{},"Errno 21"," often means you used a directory where a file was expected",[28,426,427,430],{},[17,428,429],{},"Errno 20"," often means you used a file where a directory was expected",[255,432,434],{"id":433},"common-ways-to-fix-oserror","Common ways to fix OSError",[14,436,437],{},"Here are the most common fixes:",[25,439,440,443,446,449,456,459,465],{},[28,441,442],{},"Make sure the file or folder really exists",[28,444,445],{},"Use the correct absolute or relative path",[28,447,448],{},"Check your current working directory",[28,450,451,452,455],{},"Use ",[17,453,454],{},"os.path.exists()"," before file operations when helpful",[28,457,458],{},"Make sure you have permission to read or write",[28,460,451,461,464],{},[17,462,463],{},"os.path.join()"," to build paths safely",[28,466,467,468],{},"Do not try to open a directory with ",[17,469,362],{},[14,471,472],{},"Common causes include:",[25,474,475,478,481,484,487,490,493],{},[28,476,477],{},"Wrong file path",[28,479,480],{},"Relative path used from the wrong working directory",[28,482,483],{},"Missing file or folder",[28,485,486],{},"No permission to access the path",[28,488,489],{},"Using a directory as if it were a file",[28,491,492],{},"Using a file as if it were a directory",[28,494,495],{},"Invalid characters or unsupported path format on the current system",[14,497,498,499,504],{},"If you are new to paths, see ",[500,501,503],"a",{"href":502},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python"," for a clearer explanation of relative and absolute paths.",[255,506,508],{"id":507},"example-missing-file-causes-an-oserror-family-error","Example: missing file causes an OSError family error",[14,510,511],{},"A missing file is one of the most common causes.",[60,513,515],{"className":62,"code":514,"language":64,"meta":65,"style":65},"with open(\"missing_file.txt\", \"r\", encoding=\"utf-8\") as file:\n    content = file.read()\n    print(content)\n",[17,516,517,563,579],{"__ignoreMap":65},[69,518,519,522,524,526,529,532,534,536,539,541,543,545,547,549,551,553,555,557,559,561],{"class":71,"line":72},[69,520,521],{"class":75},"with",[69,523,154],{"class":153},[69,525,139],{"class":125},[69,527,528],{"class":100},"\"",[69,530,531],{"class":104},"missing_file.txt",[69,533,528],{"class":100},[69,535,161],{"class":125},[69,537,538],{"class":100}," \"",[69,540,166],{"class":104},[69,542,528],{"class":100},[69,544,161],{"class":125},[69,546,175],{"class":174},[69,548,97],{"class":96},[69,550,528],{"class":100},[69,552,182],{"class":104},[69,554,528],{"class":100},[69,556,187],{"class":125},[69,558,190],{"class":75},[69,560,194],{"class":193},[69,562,197],{"class":125},[69,564,565,568,570,572,574,576],{"class":71,"line":83},[69,566,567],{"class":79},"    content ",[69,569,97],{"class":96},[69,571,194],{"class":193},[69,573,126],{"class":125},[69,575,213],{"class":135},[69,577,578],{"class":125},"()\n",[69,580,581,583,585,588],{"class":71,"line":90},[69,582,230],{"class":153},[69,584,139],{"class":125},[69,586,587],{"class":135},"content",[69,589,247],{"class":125},[14,591,592],{},"Expected result:",[60,594,596],{"className":62,"code":595,"language":64,"meta":65,"style":65},"FileNotFoundError: [Errno 2] No such file or directory: 'missing_file.txt'\n",[17,597,598],{"__ignoreMap":65},[69,599,600,603,606,609,612,616,619,622,624,627,630,632,634,636],{"class":71,"line":72},[69,601,293],{"class":602},"sZMiF",[69,604,605],{"class":125},":",[69,607,608],{"class":125}," [",[69,610,611],{"class":79},"Errno ",[69,613,615],{"class":614},"srdBf","2",[69,617,618],{"class":125},"]",[69,620,621],{"class":79}," No such ",[69,623,208],{"class":193},[69,625,626],{"class":96}," or",[69,628,629],{"class":79}," directory",[69,631,605],{"class":125},[69,633,101],{"class":100},[69,635,531],{"class":104},[69,637,108],{"class":100},[14,639,640,641,643,644,126],{},"In modern Python, this usually raises ",[17,642,293],{}," instead of plain ",[17,645,19],{},[14,647,648,649,651,652,126],{},"That is still part of the same error family. ",[17,650,293],{}," is a more specific subclass of ",[17,653,19],{},[14,655,656,657,126],{},"If this is your exact problem, see ",[500,658,660],{"href":659},"\u002Ferrors\u002Ffilenotfounderror-in-python-causes-and-fixes","FileNotFoundError in Python: causes and fixes",[255,662,664],{"id":663},"example-handling-oserror-with-try-except","Example: handling OSError with try-except",[14,666,667,668,126],{},"If you want to handle several operating system related problems together, you can catch ",[17,669,19],{},[60,671,673],{"className":62,"code":672,"language":64,"meta":65,"style":65},"try:\n    with open(\"example.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept OSError as error:\n    print(\"Something went wrong:\")\n    print(error)\n",[17,674,675,682,724,738,753,768],{"__ignoreMap":65},[69,676,677,680],{"class":71,"line":72},[69,678,679],{"class":75},"try",[69,681,197],{"class":125},[69,683,684,686,688,690,692,694,696,698,700,702,704,706,708,710,712,714,716,718,720,722],{"class":71,"line":83},[69,685,150],{"class":75},[69,687,154],{"class":153},[69,689,139],{"class":125},[69,691,528],{"class":100},[69,693,105],{"class":104},[69,695,528],{"class":100},[69,697,161],{"class":125},[69,699,538],{"class":100},[69,701,166],{"class":104},[69,703,528],{"class":100},[69,705,161],{"class":125},[69,707,175],{"class":174},[69,709,97],{"class":96},[69,711,528],{"class":100},[69,713,182],{"class":104},[69,715,528],{"class":100},[69,717,187],{"class":125},[69,719,190],{"class":75},[69,721,194],{"class":193},[69,723,197],{"class":125},[69,725,726,728,730,732,734,736],{"class":71,"line":90},[69,727,203],{"class":153},[69,729,139],{"class":125},[69,731,208],{"class":193},[69,733,126],{"class":125},[69,735,213],{"class":135},[69,737,216],{"class":125},[69,739,740,743,746,748,751],{"class":71,"line":111},[69,741,742],{"class":75},"except",[69,744,745],{"class":602}," OSError",[69,747,190],{"class":75},[69,749,750],{"class":79}," error",[69,752,197],{"class":125},[69,754,755,757,759,761,764,766],{"class":71,"line":116},[69,756,230],{"class":153},[69,758,139],{"class":125},[69,760,528],{"class":100},[69,762,763],{"class":104},"Something went wrong:",[69,765,528],{"class":100},[69,767,247],{"class":125},[69,769,770,772,774,777],{"class":71,"line":147},[69,771,230],{"class":153},[69,773,139],{"class":125},[69,775,776],{"class":135},"error",[69,778,247],{"class":125},[14,780,781],{},"This is useful when the exact subclass does not matter and you just want to show the error or fail safely.",[14,783,784],{},"For example, the output might be:",[60,786,788],{"className":62,"code":787,"language":64,"meta":65,"style":65},"Something went wrong:\n[Errno 2] No such file or directory: 'example.txt'\n",[17,789,790,797],{"__ignoreMap":65},[69,791,792,795],{"class":71,"line":72},[69,793,794],{"class":79},"Something went wrong",[69,796,197],{"class":125},[69,798,799,802,804,806,808,810,812,814,816,818,820,822],{"class":71,"line":83},[69,800,801],{"class":125},"[",[69,803,611],{"class":79},[69,805,615],{"class":614},[69,807,618],{"class":125},[69,809,621],{"class":79},[69,811,208],{"class":193},[69,813,626],{"class":96},[69,815,629],{"class":79},[69,817,605],{"class":125},[69,819,101],{"class":100},[69,821,105],{"class":104},[69,823,108],{"class":100},[14,825,826],{},"If you already know the likely problem, catching the specific error is better.",[60,828,830],{"className":62,"code":829,"language":64,"meta":65,"style":65},"try:\n    with open(\"example.txt\", \"r\", encoding=\"utf-8\") as file:\n        print(file.read())\nexcept FileNotFoundError:\n    print(\"The file does not exist.\")\n",[17,831,832,838,880,894,903],{"__ignoreMap":65},[69,833,834,836],{"class":71,"line":72},[69,835,679],{"class":75},[69,837,197],{"class":125},[69,839,840,842,844,846,848,850,852,854,856,858,860,862,864,866,868,870,872,874,876,878],{"class":71,"line":83},[69,841,150],{"class":75},[69,843,154],{"class":153},[69,845,139],{"class":125},[69,847,528],{"class":100},[69,849,105],{"class":104},[69,851,528],{"class":100},[69,853,161],{"class":125},[69,855,538],{"class":100},[69,857,166],{"class":104},[69,859,528],{"class":100},[69,861,161],{"class":125},[69,863,175],{"class":174},[69,865,97],{"class":96},[69,867,528],{"class":100},[69,869,182],{"class":104},[69,871,528],{"class":100},[69,873,187],{"class":125},[69,875,190],{"class":75},[69,877,194],{"class":193},[69,879,197],{"class":125},[69,881,882,884,886,888,890,892],{"class":71,"line":90},[69,883,203],{"class":153},[69,885,139],{"class":125},[69,887,208],{"class":193},[69,889,126],{"class":125},[69,891,213],{"class":135},[69,893,216],{"class":125},[69,895,896,898,901],{"class":71,"line":111},[69,897,742],{"class":75},[69,899,900],{"class":602}," FileNotFoundError",[69,902,197],{"class":125},[69,904,905,907,909,911,914,916],{"class":71,"line":116},[69,906,230],{"class":153},[69,908,139],{"class":125},[69,910,528],{"class":100},[69,912,913],{"class":104},"The file does not exist.",[69,915,528],{"class":100},[69,917,247],{"class":125},[14,919,920],{},"Specific exceptions are easier to understand and usually easier to fix.",[255,922,924],{"id":923},"debugging-checklist","Debugging checklist",[14,926,927],{},"When you are not sure what is wrong, test the path directly.",[60,929,931],{"className":62,"code":930,"language":64,"meta":65,"style":65},"import os\n\npath = \"example.txt\"\n\nprint(os.getcwd())\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(repr(path))\n",[17,932,933,939,943,956,960,977,1000,1023,1046],{"__ignoreMap":65},[69,934,935,937],{"class":71,"line":72},[69,936,76],{"class":75},[69,938,80],{"class":79},[69,940,941],{"class":71,"line":83},[69,942,87],{"emptyLinePlaceholder":86},[69,944,945,947,949,951,953],{"class":71,"line":90},[69,946,93],{"class":79},[69,948,97],{"class":96},[69,950,538],{"class":100},[69,952,105],{"class":104},[69,954,955],{"class":100},"\"\n",[69,957,958],{"class":71,"line":111},[69,959,87],{"emptyLinePlaceholder":86},[69,961,962,965,967,970,972,975],{"class":71,"line":116},[69,963,964],{"class":153},"print",[69,966,139],{"class":125},[69,968,969],{"class":135},"os",[69,971,126],{"class":125},[69,973,974],{"class":135},"getcwd",[69,976,216],{"class":125},[69,978,979,981,983,985,987,989,991,993,995,997],{"class":71,"line":147},[69,980,964],{"class":153},[69,982,139],{"class":125},[69,984,969],{"class":135},[69,986,126],{"class":125},[69,988,130],{"class":129},[69,990,126],{"class":125},[69,992,136],{"class":135},[69,994,139],{"class":125},[69,996,130],{"class":135},[69,998,999],{"class":125},"))\n",[69,1001,1002,1004,1006,1008,1010,1012,1014,1017,1019,1021],{"class":71,"line":200},[69,1003,964],{"class":153},[69,1005,139],{"class":125},[69,1007,969],{"class":135},[69,1009,126],{"class":125},[69,1011,130],{"class":129},[69,1013,126],{"class":125},[69,1015,1016],{"class":135},"isfile",[69,1018,139],{"class":125},[69,1020,130],{"class":135},[69,1022,999],{"class":125},[69,1024,1025,1027,1029,1031,1033,1035,1037,1040,1042,1044],{"class":71,"line":219},[69,1026,964],{"class":153},[69,1028,139],{"class":125},[69,1030,969],{"class":135},[69,1032,126],{"class":125},[69,1034,130],{"class":129},[69,1036,126],{"class":125},[69,1038,1039],{"class":135},"isdir",[69,1041,139],{"class":125},[69,1043,130],{"class":135},[69,1045,999],{"class":125},[69,1047,1048,1050,1052,1055,1057,1059],{"class":71,"line":227},[69,1049,964],{"class":153},[69,1051,139],{"class":125},[69,1053,1054],{"class":153},"repr",[69,1056,139],{"class":125},[69,1058,130],{"class":135},[69,1060,999],{"class":125},[14,1062,1063],{},"What each line helps you check:",[25,1065,1066,1072,1078,1084,1090],{},[28,1067,1068,1071],{},[17,1069,1070],{},"os.getcwd()"," shows your current working folder",[28,1073,1074,1077],{},[17,1075,1076],{},"os.path.exists(path)"," checks whether the path exists",[28,1079,1080,1083],{},[17,1081,1082],{},"os.path.isfile(path)"," checks whether it is a file",[28,1085,1086,1089],{},[17,1087,1088],{},"os.path.isdir(path)"," checks whether it is a directory",[28,1091,1092,1095],{},[17,1093,1094],{},"repr(path)"," shows the exact string, including hidden spaces or escape characters",[14,1097,1098],{},"These are the most useful debugging commands:",[60,1100,1102],{"className":62,"code":1101,"language":64,"meta":65,"style":65},"import os\nprint(os.getcwd())\nprint(os.path.exists(path))\nprint(os.path.isfile(path))\nprint(os.path.isdir(path))\nprint(repr(path))\n",[17,1103,1104,1110,1124,1146,1168,1190],{"__ignoreMap":65},[69,1105,1106,1108],{"class":71,"line":72},[69,1107,76],{"class":75},[69,1109,80],{"class":79},[69,1111,1112,1114,1116,1118,1120,1122],{"class":71,"line":83},[69,1113,964],{"class":153},[69,1115,139],{"class":125},[69,1117,969],{"class":135},[69,1119,126],{"class":125},[69,1121,974],{"class":135},[69,1123,216],{"class":125},[69,1125,1126,1128,1130,1132,1134,1136,1138,1140,1142,1144],{"class":71,"line":90},[69,1127,964],{"class":153},[69,1129,139],{"class":125},[69,1131,969],{"class":135},[69,1133,126],{"class":125},[69,1135,130],{"class":129},[69,1137,126],{"class":125},[69,1139,136],{"class":135},[69,1141,139],{"class":125},[69,1143,130],{"class":135},[69,1145,999],{"class":125},[69,1147,1148,1150,1152,1154,1156,1158,1160,1162,1164,1166],{"class":71,"line":111},[69,1149,964],{"class":153},[69,1151,139],{"class":125},[69,1153,969],{"class":135},[69,1155,126],{"class":125},[69,1157,130],{"class":129},[69,1159,126],{"class":125},[69,1161,1016],{"class":135},[69,1163,139],{"class":125},[69,1165,130],{"class":135},[69,1167,999],{"class":125},[69,1169,1170,1172,1174,1176,1178,1180,1182,1184,1186,1188],{"class":71,"line":116},[69,1171,964],{"class":153},[69,1173,139],{"class":125},[69,1175,969],{"class":135},[69,1177,126],{"class":125},[69,1179,130],{"class":129},[69,1181,126],{"class":125},[69,1183,1039],{"class":135},[69,1185,139],{"class":125},[69,1187,130],{"class":135},[69,1189,999],{"class":125},[69,1191,1192,1194,1196,1198,1200,1202],{"class":71,"line":147},[69,1193,964],{"class":153},[69,1195,139],{"class":125},[69,1197,1054],{"class":153},[69,1199,139],{"class":125},[69,1201,130],{"class":135},[69,1203,999],{"class":125},[14,1205,1206],{},"You can also try a simpler path that you know exists. That helps you tell whether the problem is your code or the specific file location.",[14,1208,1209,1210,1214,1215,126],{},"If you want a broader introduction to these tools, see the ",[500,1211,1213],{"href":1212},"\u002Fstandard-library\u002Fpython-os-module-overview\u002F","Python os module overview"," and ",[500,1216,1218],{"href":1217},"\u002Fstandard-library\u002Fos.path.exists-function-explained","os.path.exists() explained",[255,1220,1222],{"id":1221},"oserror-vs-specific-errors","OSError vs specific errors",[14,1224,1225,1227],{},[17,1226,19],{}," is the broad parent category.",[14,1229,1230],{},"Specific subclasses give you a clearer description of what went wrong.",[14,1232,1233],{},"Common examples include:",[25,1235,1236,1240,1244,1248],{},[28,1237,1238],{},[17,1239,293],{},[28,1241,1242],{},[17,1243,298],{},[28,1245,1246],{},[17,1247,303],{},[28,1249,1250],{},[17,1251,308],{},[14,1253,1254],{},"In general:",[25,1256,1257,1263],{},[28,1258,1259,1260,1262],{},"Catch ",[17,1261,19],{}," when you want to handle many OS-related problems together",[28,1264,1265],{},"Catch a specific subclass when you know the likely cause",[14,1267,1268],{},"For exact fixes, see:",[25,1270,1271,1277,1283],{},[28,1272,1273],{},[500,1274,1276],{"href":1275},"\u002Ferrors\u002Fpermissionerror-in-python-causes-and-fixes","PermissionError in Python: causes and fixes",[28,1278,1279],{},[500,1280,1282],{"href":1281},"\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix","IsADirectoryError: Errno 21 is a directory fix",[28,1284,1285],{},[500,1286,1288],{"href":1287},"\u002Ferrors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix","NotADirectoryError: Errno 20 not a directory fix",[255,1290,1292],{"id":1291},"faq","FAQ",[1294,1295,1297],"h3",{"id":1296},"is-oserror-the-same-as-filenotfounderror","Is OSError the same as FileNotFoundError?",[14,1299,1300,1301,651,1303,126],{},"No. ",[17,1302,293],{},[17,1304,19],{},[1294,1306,1308],{"id":1307},"should-i-catch-oserror-or-filenotfounderror","Should I catch OSError or FileNotFoundError?",[14,1310,1311],{},"Catch the specific error when you know the problem.",[14,1313,1259,1314,1316],{},[17,1315,19],{}," when you want to handle several operating system related errors together.",[1294,1318,1320],{"id":1319},"why-does-my-code-work-in-one-folder-but-not-another","Why does my code work in one folder but not another?",[14,1322,1323],{},"The current working directory may be different, or the second location may have different files or permissions.",[1294,1325,1327],{"id":1326},"can-oserror-happen-without-using-files","Can OSError happen without using files?",[14,1329,1330],{},"Yes. It can also happen with other operating system operations such as directories, process actions, or some path operations.",[255,1332,1334],{"id":1333},"see-also","See also",[25,1336,1337,1343,1348,1352,1357],{},[28,1338,1339],{},[500,1340,1342],{"href":1341},"\u002Fhow-to\u002Fhow-to-read-a-file-in-python\u002F","How to read a file in Python",[28,1344,1345],{},[500,1346,1347],{"href":502},"Working with file paths in Python",[28,1349,1350],{},[500,1351,1213],{"href":1212},[28,1353,1354],{},[500,1355,1356],{"href":1217},"os.path.exists() function explained",[28,1358,1359],{},[500,1360,660],{"href":659},[1362,1363,1364],"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 .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 .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html 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 .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 .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":65,"searchDepth":83,"depth":83,"links":1366},[1367,1368,1369,1370,1371,1372,1373,1374,1375,1381],{"id":257,"depth":83,"text":258},{"id":317,"depth":83,"text":318},{"id":372,"depth":83,"text":373},{"id":433,"depth":83,"text":434},{"id":507,"depth":83,"text":508},{"id":663,"depth":83,"text":664},{"id":923,"depth":83,"text":924},{"id":1221,"depth":83,"text":1222},{"id":1291,"depth":83,"text":1292,"children":1376},[1377,1378,1379,1380],{"id":1296,"depth":90,"text":1297},{"id":1307,"depth":90,"text":1308},{"id":1319,"depth":90,"text":1320},{"id":1326,"depth":90,"text":1327},{"id":1333,"depth":83,"text":1334},"Master oserror in python causes and fixes in our comprehensive Python beginner guide.","md",{},"\u002Ferrors\u002Foserror-in-python-causes-and-fixes",{"title":5,"description":1382},"errors\u002Foserror-in-python-causes-and-fixes","7_WrEz6P-8OCYP4_PVTslUwpuHdfOrwykiK4iX-ocCI",1777585480777]