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