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