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