[{"data":1,"prerenderedAt":1568},["ShallowReactive",2],{"doc-\u002Fstandard-library\u002Fos.chdir-function-explained":3},{"id":4,"title":5,"body":6,"description":1561,"extension":1562,"meta":1563,"navigation":79,"path":1564,"seo":1565,"stem":1566,"__hash__":1567},"content\u002Fstandard-library\u002Fos.chdir-function-explained.md","os.chdir() Function Explained",{"type":7,"value":8,"toc":1534},"minimark",[9,13,21,41,47,52,145,155,159,167,170,187,190,207,210,231,237,248,266,270,273,293,315,318,351,361,365,368,459,462,518,523,544,550,554,560,564,567,614,617,630,633,637,640,688,691,770,777,781,787,798,800,848,854,857,861,864,868,871,905,908,956,963,967,970,1004,1010,1016,1020,1023,1057,1060,1066,1070,1075,1078,1098,1100,1286,1295,1299,1304,1321,1324,1348,1384,1407,1443,1446,1460,1464,1468,1475,1479,1484,1488,1491,1495,1498,1502,1530],[10,11,5],"h1",{"id":12},"oschdir-function-explained",[14,15,16,20],"p",{},[17,18,19],"code",{},"os.chdir()"," changes Python’s current working directory.",[14,22,23,24,28,29,32,33,36,37,40],{},"This matters because the current working directory is the folder Python uses when you give a ",[25,26,27],"strong",{},"relative path"," like ",[17,30,31],{},"\"data.txt\""," or ",[17,34,35],{},"\"images\u002Fphoto.jpg\"",". After you change directories, file operations such as ",[17,38,39],{},"open()"," will use the new folder.",[14,42,43,44,46],{},"If you are learning how files and folders work in Python, ",[17,45,19],{}," can be useful. But it can also cause confusion if you are not sure which folder your program is currently using.",[48,49,51],"h2",{"id":50},"quick-example","Quick example",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import os\n\nprint(os.getcwd())\nos.chdir(\"my_folder\")\nprint(os.getcwd())\n","python","",[17,60,61,74,81,105,130],{"__ignoreMap":58},[62,63,66,70],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"sVHd0","import",[62,71,73],{"class":72},"su5hD"," os\n",[62,75,77],{"class":64,"line":76},2,[62,78,80],{"emptyLinePlaceholder":79},true,"\n",[62,82,84,88,92,96,99,102],{"class":64,"line":83},3,[62,85,87],{"class":86},"sptTA","print",[62,89,91],{"class":90},"sP7_E","(",[62,93,95],{"class":94},"slqww","os",[62,97,98],{"class":90},".",[62,100,101],{"class":94},"getcwd",[62,103,104],{"class":90},"())\n",[62,106,108,110,112,115,117,121,125,127],{"class":64,"line":107},4,[62,109,95],{"class":72},[62,111,98],{"class":90},[62,113,114],{"class":94},"chdir",[62,116,91],{"class":90},[62,118,120],{"class":119},"sjJ54","\"",[62,122,124],{"class":123},"s_sjI","my_folder",[62,126,120],{"class":119},[62,128,129],{"class":90},")\n",[62,131,133,135,137,139,141,143],{"class":64,"line":132},5,[62,134,87],{"class":86},[62,136,91],{"class":90},[62,138,95],{"class":94},[62,140,98],{"class":90},[62,142,101],{"class":94},[62,144,104],{"class":90},[14,146,147,148,151,152,154],{},"Use ",[17,149,150],{},"os.getcwd()"," before and after ",[17,153,19],{}," to confirm that the working directory changed.",[48,156,158],{"id":157},"what-oschdir-does","What os.chdir() does",[14,160,161,163,164,98],{},[17,162,19],{}," changes the program's ",[25,165,166],{},"current working directory",[14,168,169],{},"In simple terms:",[171,172,173,177,180],"ul",{},[174,175,176],"li",{},"The working directory is the folder Python is currently “inside”",[174,178,179],{},"Relative file paths are based on that folder",[174,181,182,183,186],{},"After changing it, code like ",[17,184,185],{},"open(\"notes.txt\")"," looks in the new folder",[14,188,189],{},"For example, if your current folder is:",[53,191,193],{"className":55,"code":192,"language":57,"meta":58,"style":58},"C:\\Users\\Sam\\project\n",[17,194,195],{"__ignoreMap":58},[62,196,197,200,203],{"class":64,"line":65},[62,198,199],{"class":72},"C",[62,201,202],{"class":90},":\\",[62,204,206],{"class":205},"srjyR","Users\\Sam\\project\n",[14,208,209],{},"and you run:",[53,211,213],{"className":55,"code":212,"language":57,"meta":58,"style":58},"open(\"data.txt\")\n",[17,214,215],{"__ignoreMap":58},[62,216,217,220,222,224,227,229],{"class":64,"line":65},[62,218,219],{"class":86},"open",[62,221,91],{"class":90},[62,223,120],{"class":119},[62,225,226],{"class":123},"data.txt",[62,228,120],{"class":119},[62,230,129],{"class":90},[14,232,233,234,236],{},"Python looks for ",[17,235,226],{}," inside that folder.",[14,238,239,240,243,244,247],{},"If you then use ",[17,241,242],{},"os.chdir(\"reports\")",", Python will start looking relative to the ",[17,245,246],{},"reports"," folder instead.",[14,249,250,251,258,259,265],{},"If you are new to this idea, the ",[252,253,255,257],"a",{"href":254},"\u002Fstandard-library\u002Fpython-os-module-overview",[17,256,95],{}," module overview"," and ",[252,260,262,264],{"href":261},"\u002Fstandard-library\u002Fos.getcwd-function-explained",[17,263,150],{}," function explained"," pages are good next steps.",[48,267,269],{"id":268},"basic-syntax","Basic syntax",[14,271,272],{},"The syntax is:",[53,274,276],{"className":55,"code":275,"language":57,"meta":58,"style":58},"os.chdir(path)\n",[17,277,278],{"__ignoreMap":58},[62,279,280,282,284,286,288,291],{"class":64,"line":65},[62,281,95],{"class":72},[62,283,98],{"class":90},[62,285,114],{"class":94},[62,287,91],{"class":90},[62,289,290],{"class":94},"path",[62,292,129],{"class":90},[171,294,295,300,309],{},[174,296,297,299],{},[17,298,290],{}," is the folder you want Python to switch to",[174,301,302,303,32,306],{},"The path can be ",[25,304,305],{},"relative",[25,307,308],{},"absolute",[174,310,311,312,314],{},"You must import the ",[17,313,95],{}," module first",[14,316,317],{},"Example:",[53,319,321],{"className":55,"code":320,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"my_folder\")\n",[17,322,323,329,333],{"__ignoreMap":58},[62,324,325,327],{"class":64,"line":65},[62,326,69],{"class":68},[62,328,73],{"class":72},[62,330,331],{"class":64,"line":76},[62,332,80],{"emptyLinePlaceholder":79},[62,334,335,337,339,341,343,345,347,349],{"class":64,"line":83},[62,336,95],{"class":72},[62,338,98],{"class":90},[62,340,114],{"class":94},[62,342,91],{"class":90},[62,344,120],{"class":119},[62,346,124],{"class":123},[62,348,120],{"class":119},[62,350,129],{"class":90},[14,352,353,354,356,357,360],{},"One important detail: ",[17,355,19],{}," does ",[25,358,359],{},"not"," return the new path. It only changes the current directory.",[48,362,364],{"id":363},"simple-example","Simple example",[14,366,367],{},"This example shows the current folder, changes to another folder, and then shows the new folder.",[53,369,371],{"className":55,"code":370,"language":57,"meta":58,"style":58},"import os\n\nprint(\"Before:\", os.getcwd())\n\nos.chdir(\"my_folder\")\n\nprint(\"After:\", os.getcwd())\n",[17,372,373,379,383,408,412,430,435],{"__ignoreMap":58},[62,374,375,377],{"class":64,"line":65},[62,376,69],{"class":68},[62,378,73],{"class":72},[62,380,381],{"class":64,"line":76},[62,382,80],{"emptyLinePlaceholder":79},[62,384,385,387,389,391,394,396,399,402,404,406],{"class":64,"line":83},[62,386,87],{"class":86},[62,388,91],{"class":90},[62,390,120],{"class":119},[62,392,393],{"class":123},"Before:",[62,395,120],{"class":119},[62,397,398],{"class":90},",",[62,400,401],{"class":94}," os",[62,403,98],{"class":90},[62,405,101],{"class":94},[62,407,104],{"class":90},[62,409,410],{"class":64,"line":107},[62,411,80],{"emptyLinePlaceholder":79},[62,413,414,416,418,420,422,424,426,428],{"class":64,"line":132},[62,415,95],{"class":72},[62,417,98],{"class":90},[62,419,114],{"class":94},[62,421,91],{"class":90},[62,423,120],{"class":119},[62,425,124],{"class":123},[62,427,120],{"class":119},[62,429,129],{"class":90},[62,431,433],{"class":64,"line":432},6,[62,434,80],{"emptyLinePlaceholder":79},[62,436,438,440,442,444,447,449,451,453,455,457],{"class":64,"line":437},7,[62,439,87],{"class":86},[62,441,91],{"class":90},[62,443,120],{"class":119},[62,445,446],{"class":123},"After:",[62,448,120],{"class":119},[62,450,398],{"class":90},[62,452,401],{"class":94},[62,454,98],{"class":90},[62,456,101],{"class":94},[62,458,104],{"class":90},[14,460,461],{},"Example output:",[53,463,465],{"className":55,"code":464,"language":57,"meta":58,"style":58},"Before: \u002FUsers\u002Falex\u002Fproject\nAfter: \u002FUsers\u002Falex\u002Fproject\u002Fmy_folder\n",[17,466,467,493],{"__ignoreMap":58},[62,468,469,472,475,479,482,485,488,490],{"class":64,"line":65},[62,470,471],{"class":72},"Before",[62,473,474],{"class":90},":",[62,476,478],{"class":477},"smGrS"," \u002F",[62,480,481],{"class":72},"Users",[62,483,484],{"class":477},"\u002F",[62,486,487],{"class":72},"alex",[62,489,484],{"class":477},[62,491,492],{"class":72},"project\n",[62,494,495,498,500,502,504,506,508,510,513,515],{"class":64,"line":76},[62,496,497],{"class":72},"After",[62,499,474],{"class":90},[62,501,478],{"class":477},[62,503,481],{"class":72},[62,505,484],{"class":477},[62,507,487],{"class":72},[62,509,484],{"class":477},[62,511,512],{"class":72},"project",[62,514,484],{"class":477},[62,516,517],{"class":72},"my_folder\n",[519,520,522],"h3",{"id":521},"what-the-code-does","What the code does",[171,524,525,530,538],{},[174,526,527,529],{},[17,528,150],{}," gets the current working directory",[174,531,532,535,536],{},[17,533,534],{},"os.chdir(\"my_folder\")"," changes into a folder named ",[17,537,124],{},[174,539,540,541,543],{},"The second ",[17,542,150],{}," confirms the change",[14,545,546,547,549],{},"This only works if ",[17,548,124],{}," exists inside the current folder.",[48,551,553],{"id":552},"using-relative-and-absolute-paths","Using relative and absolute paths",[14,555,556,557,559],{},"You can use ",[17,558,19],{}," with either a relative path or an absolute path.",[519,561,563],{"id":562},"relative-path","Relative path",[14,565,566],{},"A relative path is based on the current working directory.",[53,568,570],{"className":55,"code":569,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"my_folder\")\nprint(os.getcwd())\n",[17,571,572,578,582,600],{"__ignoreMap":58},[62,573,574,576],{"class":64,"line":65},[62,575,69],{"class":68},[62,577,73],{"class":72},[62,579,580],{"class":64,"line":76},[62,581,80],{"emptyLinePlaceholder":79},[62,583,584,586,588,590,592,594,596,598],{"class":64,"line":83},[62,585,95],{"class":72},[62,587,98],{"class":90},[62,589,114],{"class":94},[62,591,91],{"class":90},[62,593,120],{"class":119},[62,595,124],{"class":123},[62,597,120],{"class":119},[62,599,129],{"class":90},[62,601,602,604,606,608,610,612],{"class":64,"line":107},[62,603,87],{"class":86},[62,605,91],{"class":90},[62,607,95],{"class":94},[62,609,98],{"class":90},[62,611,101],{"class":94},[62,613,104],{"class":90},[14,615,616],{},"This means:",[171,618,619,622,627],{},[174,620,621],{},"Start from the current folder",[174,623,624,625],{},"Look for a folder called ",[17,626,124],{},[174,628,629],{},"Move into it",[14,631,632],{},"If the current folder is not what you expect, the relative path may fail.",[519,634,636],{"id":635},"absolute-path","Absolute path",[14,638,639],{},"An absolute path gives the full location of the folder.",[53,641,643],{"className":55,"code":642,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"\u002FUsers\u002Falex\u002Fproject\u002Fmy_folder\")\nprint(os.getcwd())\n",[17,644,645,651,655,674],{"__ignoreMap":58},[62,646,647,649],{"class":64,"line":65},[62,648,69],{"class":68},[62,650,73],{"class":72},[62,652,653],{"class":64,"line":76},[62,654,80],{"emptyLinePlaceholder":79},[62,656,657,659,661,663,665,667,670,672],{"class":64,"line":83},[62,658,95],{"class":72},[62,660,98],{"class":90},[62,662,114],{"class":94},[62,664,91],{"class":90},[62,666,120],{"class":119},[62,668,669],{"class":123},"\u002FUsers\u002Falex\u002Fproject\u002Fmy_folder",[62,671,120],{"class":119},[62,673,129],{"class":90},[62,675,676,678,680,682,684,686],{"class":64,"line":107},[62,677,87],{"class":86},[62,679,91],{"class":90},[62,681,95],{"class":94},[62,683,98],{"class":90},[62,685,101],{"class":94},[62,687,104],{"class":90},[14,689,690],{},"On Windows, it might look like this:",[53,692,694],{"className":55,"code":693,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(r\"C:\\Users\\Alex\\project\\my_folder\")\nprint(os.getcwd())\n",[17,695,696,702,706,756],{"__ignoreMap":58},[62,697,698,700],{"class":64,"line":65},[62,699,69],{"class":68},[62,701,73],{"class":72},[62,703,704],{"class":64,"line":76},[62,705,80],{"emptyLinePlaceholder":79},[62,707,708,710,712,714,716,720,722,726,730,733,737,740,743,746,749,752,754],{"class":64,"line":83},[62,709,95],{"class":72},[62,711,98],{"class":90},[62,713,114],{"class":94},[62,715,91],{"class":90},[62,717,719],{"class":718},"sbsja","r",[62,721,120],{"class":119},[62,723,725],{"class":724},"sQRbd","C:",[62,727,729],{"class":728},"sjYin","\\U",[62,731,732],{"class":724},"sers",[62,734,736],{"class":735},"stzsN","\\A",[62,738,739],{"class":724},"lex",[62,741,742],{"class":728},"\\p",[62,744,745],{"class":724},"roject",[62,747,748],{"class":728},"\\m",[62,750,751],{"class":724},"y_folder",[62,753,120],{"class":119},[62,755,129],{"class":90},[62,757,758,760,762,764,766,768],{"class":64,"line":107},[62,759,87],{"class":86},[62,761,91],{"class":90},[62,763,95],{"class":94},[62,765,98],{"class":90},[62,767,101],{"class":94},[62,769,104],{"class":90},[14,771,772,773,98],{},"Absolute paths are often easier to debug because they do not depend on the current folder. If file paths are still confusing, see ",[252,774,776],{"href":775},"\u002Flearn\u002Fworking-with-file-paths-in-python\u002F","working with file paths in Python",[48,778,780],{"id":779},"why-beginners-use-oschdir","Why beginners use os.chdir()",[14,782,783,784,786],{},"Beginners often use ",[17,785,19],{}," for simple tasks like these:",[171,788,789,792,795],{},[174,790,791],{},"To make file reading and writing easier in small scripts",[174,793,794],{},"To switch into a project folder before listing files",[174,796,797],{},"To work with folders step by step while learning",[14,799,317],{},[53,801,803],{"className":55,"code":802,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"my_folder\")\nprint(os.listdir())\n",[17,804,805,811,815,833],{"__ignoreMap":58},[62,806,807,809],{"class":64,"line":65},[62,808,69],{"class":68},[62,810,73],{"class":72},[62,812,813],{"class":64,"line":76},[62,814,80],{"emptyLinePlaceholder":79},[62,816,817,819,821,823,825,827,829,831],{"class":64,"line":83},[62,818,95],{"class":72},[62,820,98],{"class":90},[62,822,114],{"class":94},[62,824,91],{"class":90},[62,826,120],{"class":119},[62,828,124],{"class":123},[62,830,120],{"class":119},[62,832,129],{"class":90},[62,834,835,837,839,841,843,846],{"class":64,"line":107},[62,836,87],{"class":86},[62,838,91],{"class":90},[62,840,95],{"class":94},[62,842,98],{"class":90},[62,844,845],{"class":94},"listdir",[62,847,104],{"class":90},[14,849,850,851,853],{},"This changes into ",[17,852,124],{}," and then lists the files inside it.",[14,855,856],{},"For small practice scripts, this can be convenient. In larger programs, changing directories too often can make code harder to understand.",[48,858,860],{"id":859},"common-errors-when-using-oschdir","Common errors when using os.chdir()",[14,862,863],{},"A few common errors happen when the path is wrong or inaccessible.",[519,865,867],{"id":866},"filenotfounderror","FileNotFoundError",[14,869,870],{},"This happens when the folder does not exist.",[53,872,874],{"className":55,"code":873,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"missing_folder\")\n",[17,875,876,882,886],{"__ignoreMap":58},[62,877,878,880],{"class":64,"line":65},[62,879,69],{"class":68},[62,881,73],{"class":72},[62,883,884],{"class":64,"line":76},[62,885,80],{"emptyLinePlaceholder":79},[62,887,888,890,892,894,896,898,901,903],{"class":64,"line":83},[62,889,95],{"class":72},[62,891,98],{"class":90},[62,893,114],{"class":94},[62,895,91],{"class":90},[62,897,120],{"class":119},[62,899,900],{"class":123},"missing_folder",[62,902,120],{"class":119},[62,904,129],{"class":90},[14,906,907],{},"Python may show an error like:",[53,909,911],{"className":55,"code":910,"language":57,"meta":58,"style":58},"FileNotFoundError: [Errno 2] No such file or directory: 'missing_folder'\n",[17,912,913],{"__ignoreMap":58},[62,914,915,918,920,923,926,930,933,936,940,943,946,948,951,953],{"class":64,"line":65},[62,916,867],{"class":917},"sZMiF",[62,919,474],{"class":90},[62,921,922],{"class":90}," [",[62,924,925],{"class":72},"Errno ",[62,927,929],{"class":928},"srdBf","2",[62,931,932],{"class":90},"]",[62,934,935],{"class":72}," No such ",[62,937,939],{"class":938},"sMMDD","file",[62,941,942],{"class":477}," or",[62,944,945],{"class":72}," directory",[62,947,474],{"class":90},[62,949,950],{"class":119}," '",[62,952,900],{"class":123},[62,954,955],{"class":119},"'\n",[14,957,958,959,98],{},"See ",[252,960,962],{"href":961},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix\u002F","how to fix FileNotFoundError: No such file or directory",[519,964,966],{"id":965},"notadirectoryerror","NotADirectoryError",[14,968,969],{},"This happens when the path points to a file instead of a folder.",[53,971,973],{"className":55,"code":972,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"notes.txt\")\n",[17,974,975,981,985],{"__ignoreMap":58},[62,976,977,979],{"class":64,"line":65},[62,978,69],{"class":68},[62,980,73],{"class":72},[62,982,983],{"class":64,"line":76},[62,984,80],{"emptyLinePlaceholder":79},[62,986,987,989,991,993,995,997,1000,1002],{"class":64,"line":83},[62,988,95],{"class":72},[62,990,98],{"class":90},[62,992,114],{"class":94},[62,994,91],{"class":90},[62,996,120],{"class":119},[62,998,999],{"class":123},"notes.txt",[62,1001,120],{"class":119},[62,1003,129],{"class":90},[14,1005,1006,1007,1009],{},"If ",[17,1008,999],{}," is a file, not a directory, Python raises this error.",[14,1011,958,1012,98],{},[252,1013,1015],{"href":1014},"\u002Ferrors\u002Fnotadirectoryerror-errno-20-not-a-directory-fix\u002F","how to fix NotADirectoryError",[519,1017,1019],{"id":1018},"permissionerror","PermissionError",[14,1021,1022],{},"This happens when Python does not have permission to enter the folder.",[53,1024,1026],{"className":55,"code":1025,"language":57,"meta":58,"style":58},"import os\n\nos.chdir(\"\u002Froot\")\n",[17,1027,1028,1034,1038],{"__ignoreMap":58},[62,1029,1030,1032],{"class":64,"line":65},[62,1031,69],{"class":68},[62,1033,73],{"class":72},[62,1035,1036],{"class":64,"line":76},[62,1037,80],{"emptyLinePlaceholder":79},[62,1039,1040,1042,1044,1046,1048,1050,1053,1055],{"class":64,"line":83},[62,1041,95],{"class":72},[62,1043,98],{"class":90},[62,1045,114],{"class":94},[62,1047,91],{"class":90},[62,1049,120],{"class":119},[62,1051,1052],{"class":123},"\u002Froot",[62,1054,120],{"class":119},[62,1056,129],{"class":90},[14,1058,1059],{},"Some system folders are restricted.",[14,1061,958,1062,98],{},[252,1063,1065],{"href":1064},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix\u002F","how to fix PermissionError: permission denied",[48,1067,1069],{"id":1068},"safer-ways-to-work-with-paths","Safer ways to work with paths",[14,1071,1072,1074],{},[17,1073,19],{}," is useful, but it helps to be careful.",[14,1076,1077],{},"Here are some safer habits:",[171,1079,1080,1086,1092,1095],{},[174,1081,1082,1083,1085],{},"Check the current folder with ",[17,1084,150],{}," before changing it",[174,1087,147,1088,1091],{},[17,1089,1090],{},"os.path.exists()"," to check whether a path exists",[174,1093,1094],{},"Consider using full paths when debugging",[174,1096,1097],{},"Avoid changing directories many times in one script",[14,1099,317],{},[53,1101,1103],{"className":55,"code":1102,"language":57,"meta":58,"style":58},"import os\n\nfolder = \"my_folder\"\n\nprint(\"Current folder:\", os.getcwd())\nprint(\"Exists:\", os.path.exists(folder))\n\nif os.path.exists(folder):\n    os.chdir(folder)\n    print(\"Changed to:\", os.getcwd())\nelse:\n    print(\"Folder not found\")\n",[17,1104,1105,1111,1115,1131,1135,1158,1193,1197,1220,1236,1261,1270],{"__ignoreMap":58},[62,1106,1107,1109],{"class":64,"line":65},[62,1108,69],{"class":68},[62,1110,73],{"class":72},[62,1112,1113],{"class":64,"line":76},[62,1114,80],{"emptyLinePlaceholder":79},[62,1116,1117,1120,1123,1126,1128],{"class":64,"line":83},[62,1118,1119],{"class":72},"folder ",[62,1121,1122],{"class":477},"=",[62,1124,1125],{"class":119}," \"",[62,1127,124],{"class":123},[62,1129,1130],{"class":119},"\"\n",[62,1132,1133],{"class":64,"line":107},[62,1134,80],{"emptyLinePlaceholder":79},[62,1136,1137,1139,1141,1143,1146,1148,1150,1152,1154,1156],{"class":64,"line":132},[62,1138,87],{"class":86},[62,1140,91],{"class":90},[62,1142,120],{"class":119},[62,1144,1145],{"class":123},"Current folder:",[62,1147,120],{"class":119},[62,1149,398],{"class":90},[62,1151,401],{"class":94},[62,1153,98],{"class":90},[62,1155,101],{"class":94},[62,1157,104],{"class":90},[62,1159,1160,1162,1164,1166,1169,1171,1173,1175,1177,1180,1182,1185,1187,1190],{"class":64,"line":432},[62,1161,87],{"class":86},[62,1163,91],{"class":90},[62,1165,120],{"class":119},[62,1167,1168],{"class":123},"Exists:",[62,1170,120],{"class":119},[62,1172,398],{"class":90},[62,1174,401],{"class":94},[62,1176,98],{"class":90},[62,1178,290],{"class":1179},"skxfh",[62,1181,98],{"class":90},[62,1183,1184],{"class":94},"exists",[62,1186,91],{"class":90},[62,1188,1189],{"class":94},"folder",[62,1191,1192],{"class":90},"))\n",[62,1194,1195],{"class":64,"line":437},[62,1196,80],{"emptyLinePlaceholder":79},[62,1198,1200,1203,1205,1207,1209,1211,1213,1215,1217],{"class":64,"line":1199},8,[62,1201,1202],{"class":68},"if",[62,1204,401],{"class":72},[62,1206,98],{"class":90},[62,1208,290],{"class":1179},[62,1210,98],{"class":90},[62,1212,1184],{"class":94},[62,1214,91],{"class":90},[62,1216,1189],{"class":94},[62,1218,1219],{"class":90},"):\n",[62,1221,1223,1226,1228,1230,1232,1234],{"class":64,"line":1222},9,[62,1224,1225],{"class":72},"    os",[62,1227,98],{"class":90},[62,1229,114],{"class":94},[62,1231,91],{"class":90},[62,1233,1189],{"class":94},[62,1235,129],{"class":90},[62,1237,1239,1242,1244,1246,1249,1251,1253,1255,1257,1259],{"class":64,"line":1238},10,[62,1240,1241],{"class":86},"    print",[62,1243,91],{"class":90},[62,1245,120],{"class":119},[62,1247,1248],{"class":123},"Changed to:",[62,1250,120],{"class":119},[62,1252,398],{"class":90},[62,1254,401],{"class":94},[62,1256,98],{"class":90},[62,1258,101],{"class":94},[62,1260,104],{"class":90},[62,1262,1264,1267],{"class":64,"line":1263},11,[62,1265,1266],{"class":68},"else",[62,1268,1269],{"class":90},":\n",[62,1271,1273,1275,1277,1279,1282,1284],{"class":64,"line":1272},12,[62,1274,1241],{"class":86},[62,1276,91],{"class":90},[62,1278,120],{"class":119},[62,1280,1281],{"class":123},"Folder not found",[62,1283,120],{"class":119},[62,1285,129],{"class":90},[14,1287,1288,1289,1294],{},"You can learn more about ",[252,1290,1292],{"href":1291},"\u002Fstandard-library\u002Fos.path.exists-function-explained",[17,1293,1090],{}," if you want to check paths before using them.",[48,1296,1298],{"id":1297},"common-mistakes","Common mistakes",[14,1300,1301,1302,474],{},"These are the most common causes of problems with ",[17,1303,19],{},[171,1305,1306,1309,1312,1315,1318],{},[174,1307,1308],{},"Trying to change to a folder that does not exist",[174,1310,1311],{},"Using the wrong relative path",[174,1313,1314],{},"Forgetting which folder the script started in",[174,1316,1317],{},"Passing a file path instead of a directory path",[174,1319,1320],{},"Using a path that Python does not have permission to access",[14,1322,1323],{},"Helpful debugging commands:",[53,1325,1327],{"className":55,"code":1326,"language":57,"meta":58,"style":58},"import os; print(os.getcwd())\n",[17,1328,1329],{"__ignoreMap":58},[62,1330,1331,1333,1336,1338,1340,1342,1344,1346],{"class":64,"line":65},[62,1332,69],{"class":68},[62,1334,1335],{"class":72}," os; ",[62,1337,87],{"class":86},[62,1339,91],{"class":90},[62,1341,95],{"class":94},[62,1343,98],{"class":90},[62,1345,101],{"class":94},[62,1347,104],{"class":90},[53,1349,1351],{"className":55,"code":1350,"language":57,"meta":58,"style":58},"import os; print(os.path.exists('my_folder'))\n",[17,1352,1353],{"__ignoreMap":58},[62,1354,1355,1357,1359,1361,1363,1365,1367,1369,1371,1373,1375,1378,1380,1382],{"class":64,"line":65},[62,1356,69],{"class":68},[62,1358,1335],{"class":72},[62,1360,87],{"class":86},[62,1362,91],{"class":90},[62,1364,95],{"class":94},[62,1366,98],{"class":90},[62,1368,290],{"class":1179},[62,1370,98],{"class":90},[62,1372,1184],{"class":94},[62,1374,91],{"class":90},[62,1376,1377],{"class":119},"'",[62,1379,124],{"class":123},[62,1381,1377],{"class":119},[62,1383,1192],{"class":90},[53,1385,1387],{"className":55,"code":1386,"language":57,"meta":58,"style":58},"import os; print(os.listdir())\n",[17,1388,1389],{"__ignoreMap":58},[62,1390,1391,1393,1395,1397,1399,1401,1403,1405],{"class":64,"line":65},[62,1392,69],{"class":68},[62,1394,1335],{"class":72},[62,1396,87],{"class":86},[62,1398,91],{"class":90},[62,1400,95],{"class":94},[62,1402,98],{"class":90},[62,1404,845],{"class":94},[62,1406,104],{"class":90},[53,1408,1410],{"className":55,"code":1409,"language":57,"meta":58,"style":58},"import os; print(os.path.isdir('my_folder'))\n",[17,1411,1412],{"__ignoreMap":58},[62,1413,1414,1416,1418,1420,1422,1424,1426,1428,1430,1433,1435,1437,1439,1441],{"class":64,"line":65},[62,1415,69],{"class":68},[62,1417,1335],{"class":72},[62,1419,87],{"class":86},[62,1421,91],{"class":90},[62,1423,95],{"class":94},[62,1425,98],{"class":90},[62,1427,290],{"class":1179},[62,1429,98],{"class":90},[62,1431,1432],{"class":94},"isdir",[62,1434,91],{"class":90},[62,1436,1377],{"class":119},[62,1438,124],{"class":123},[62,1440,1377],{"class":119},[62,1442,1192],{"class":90},[14,1444,1445],{},"These commands help you answer basic questions:",[171,1447,1448,1451,1454,1457],{},[174,1449,1450],{},"Where am I right now?",[174,1452,1453],{},"Does this path exist?",[174,1455,1456],{},"What files and folders are here?",[174,1458,1459],{},"Is this path a directory?",[48,1461,1463],{"id":1462},"faq","FAQ",[519,1465,1467],{"id":1466},"what-does-oschdir-return","What does os.chdir() return?",[14,1469,1470,1471,1474],{},"It returns ",[17,1472,1473],{},"None",". It changes the current working directory but does not return the new path.",[519,1476,1478],{"id":1477},"how-do-i-check-the-current-working-directory","How do I check the current working directory?",[14,1480,147,1481,1483],{},[17,1482,150],{}," to print the current working directory.",[519,1485,1487],{"id":1486},"can-i-use-oschdir-with-a-relative-path","Can I use os.chdir() with a relative path?",[14,1489,1490],{},"Yes. Python will interpret the path relative to the current working directory.",[519,1492,1494],{"id":1493},"why-does-oschdir-break-my-file-code","Why does os.chdir() break my file code?",[14,1496,1497],{},"Changing the working directory changes how relative paths are resolved, so files may no longer be found where your code expects them.",[48,1499,1501],{"id":1500},"see-also","See also",[171,1503,1504,1509,1514,1519,1524],{},[174,1505,1506],{},[252,1507,1508],{"href":254},"os module overview",[174,1510,1511],{},[252,1512,1513],{"href":261},"os.getcwd() function explained",[174,1515,1516],{},[252,1517,1518],{"href":1291},"os.path.exists() function explained",[174,1520,1521],{},[252,1522,1523],{"href":775},"Working with file paths in Python",[174,1525,1526],{},[252,1527,1529],{"href":1528},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","How to check if a file exists in Python",[1531,1532,1533],"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 .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .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 .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 .srjyR, html code.shiki .srjyR{--shiki-light:#90A4AE;--shiki-light-font-style:inherit;--shiki-default:#B31D28;--shiki-default-font-style:italic;--shiki-dark:#FDAEB7;--shiki-dark-font-style:italic}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sQRbd, html code.shiki .sQRbd{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#DBEDFF}html pre.shiki code .sjYin, html code.shiki .sjYin{--shiki-light:#90A4AE;--shiki-light-font-weight:inherit;--shiki-default:#22863A;--shiki-default-font-weight:bold;--shiki-dark:#85E89D;--shiki-dark-font-weight:bold}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--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 .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}",{"title":58,"searchDepth":76,"depth":76,"links":1535},[1536,1537,1538,1539,1542,1546,1547,1552,1553,1554,1560],{"id":50,"depth":76,"text":51},{"id":157,"depth":76,"text":158},{"id":268,"depth":76,"text":269},{"id":363,"depth":76,"text":364,"children":1540},[1541],{"id":521,"depth":83,"text":522},{"id":552,"depth":76,"text":553,"children":1543},[1544,1545],{"id":562,"depth":83,"text":563},{"id":635,"depth":83,"text":636},{"id":779,"depth":76,"text":780},{"id":859,"depth":76,"text":860,"children":1548},[1549,1550,1551],{"id":866,"depth":83,"text":867},{"id":965,"depth":83,"text":966},{"id":1018,"depth":83,"text":1019},{"id":1068,"depth":76,"text":1069},{"id":1297,"depth":76,"text":1298},{"id":1462,"depth":76,"text":1463,"children":1555},[1556,1557,1558,1559],{"id":1466,"depth":83,"text":1467},{"id":1477,"depth":83,"text":1478},{"id":1486,"depth":83,"text":1487},{"id":1493,"depth":83,"text":1494},{"id":1500,"depth":76,"text":1501},"Master os.chdir function explained in our comprehensive Python beginner guide.","md",{},"\u002Fstandard-library\u002Fos.chdir-function-explained",{"title":5,"description":1561},"standard-library\u002Fos.chdir-function-explained","o-fwz80q92MH2hmSRWtxCtWfTJi2tFZ8YK91rv_QD48",1777585468838]