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