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