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