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