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