[{"data":1,"prerenderedAt":1953},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-delete-a-file-in-python":3},{"id":4,"title":5,"body":6,"description":1946,"extension":1947,"meta":1948,"navigation":76,"path":1949,"seo":1950,"stem":1951,"__hash__":1952},"content\u002Fhow-to\u002Fhow-to-delete-a-file-in-python.md","How to Delete a File in Python",{"type":7,"value":8,"toc":1906},"minimark",[9,13,22,25,38,44,49,197,205,209,223,229,238,286,291,311,314,333,336,344,348,351,449,453,466,482,486,489,493,496,581,585,588,654,658,661,688,691,717,730,737,743,849,856,870,876,879,891,894,898,901,906,909,920,923,957,960,1045,1052,1058,1061,1063,1097,1100,1111,1120,1126,1129,1163,1168,1177,1183,1186,1196,1348,1352,1355,1372,1374,1528,1532,1535,1552,1555,1570,1594,1628,1661,1687,1690,1707,1711,1715,1724,1728,1739,1804,1811,1818,1825,1828,1838,1847,1850,1863,1867,1902],[10,11,5],"h1",{"id":12},"how-to-delete-a-file-in-python",[14,15,16,17,21],"p",{},"If you want to delete a file in Python, the most common way is to use ",[18,19,20],"code",{},"os.remove()",".",[14,23,24],{},"For beginners, the safest approach is:",[26,27,28,32,35],"ul",{},[29,30,31],"li",{},"store the file path in a variable",[29,33,34],{},"check whether the file exists",[29,36,37],{},"delete it only if it is really there",[14,39,40,41,21],{},"This helps you avoid common errors like ",[18,42,43],{},"FileNotFoundError",[45,46,48],"h2",{"id":47},"quick-answer","Quick answer",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import os\n\nfile_path = \"notes.txt\"\n\nif os.path.exists(file_path):\n    os.remove(file_path)\n    print(\"File deleted\")\nelse:\n    print(\"File does not exist\")\n","python","",[18,57,58,71,78,99,104,135,153,172,181],{"__ignoreMap":55},[59,60,63,67],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sVHd0","import",[59,68,70],{"class":69},"su5hD"," os\n",[59,72,74],{"class":61,"line":73},2,[59,75,77],{"emptyLinePlaceholder":76},true,"\n",[59,79,81,84,88,92,96],{"class":61,"line":80},3,[59,82,83],{"class":69},"file_path ",[59,85,87],{"class":86},"smGrS","=",[59,89,91],{"class":90},"sjJ54"," \"",[59,93,95],{"class":94},"s_sjI","notes.txt",[59,97,98],{"class":90},"\"\n",[59,100,102],{"class":61,"line":101},4,[59,103,77],{"emptyLinePlaceholder":76},[59,105,107,110,113,116,120,122,126,129,132],{"class":61,"line":106},5,[59,108,109],{"class":65},"if",[59,111,112],{"class":69}," os",[59,114,21],{"class":115},"sP7_E",[59,117,119],{"class":118},"skxfh","path",[59,121,21],{"class":115},[59,123,125],{"class":124},"slqww","exists",[59,127,128],{"class":115},"(",[59,130,131],{"class":124},"file_path",[59,133,134],{"class":115},"):\n",[59,136,138,141,143,146,148,150],{"class":61,"line":137},6,[59,139,140],{"class":69},"    os",[59,142,21],{"class":115},[59,144,145],{"class":124},"remove",[59,147,128],{"class":115},[59,149,131],{"class":124},[59,151,152],{"class":115},")\n",[59,154,156,160,162,165,168,170],{"class":61,"line":155},7,[59,157,159],{"class":158},"sptTA","    print",[59,161,128],{"class":115},[59,163,164],{"class":90},"\"",[59,166,167],{"class":94},"File deleted",[59,169,164],{"class":90},[59,171,152],{"class":115},[59,173,175,178],{"class":61,"line":174},8,[59,176,177],{"class":65},"else",[59,179,180],{"class":115},":\n",[59,182,184,186,188,190,193,195],{"class":61,"line":183},9,[59,185,159],{"class":158},[59,187,128],{"class":115},[59,189,164],{"class":90},[59,191,192],{"class":94},"File does not exist",[59,194,164],{"class":90},[59,196,152],{"class":115},[14,198,199,200,202,203,21],{},"Use ",[18,201,20],{}," to delete a file. Check that the path exists first to avoid a ",[18,204,43],{},[45,206,208],{"id":207},"what-this-page-helps-you-do","What this page helps you do",[26,210,211,214,217,220],{},[29,212,213],{},"Delete a single file with Python",[29,215,216],{},"Use the correct function for files, not folders",[29,218,219],{},"Check whether a file exists before deleting it",[29,221,222],{},"Understand common errors you may see",[45,224,226,227],{"id":225},"delete-a-file-with-osremove","Delete a file with ",[18,228,20],{},[14,230,231,232,235,236,21],{},"To delete a file, import the ",[18,233,234],{},"os"," module and pass the file path to ",[18,237,20],{},[50,239,241],{"className":52,"code":240,"language":54,"meta":55,"style":55},"import os\n\nos.remove(\"notes.txt\")\nprint(\"File deleted\")\n",[18,242,243,249,253,271],{"__ignoreMap":55},[59,244,245,247],{"class":61,"line":62},[59,246,66],{"class":65},[59,248,70],{"class":69},[59,250,251],{"class":61,"line":73},[59,252,77],{"emptyLinePlaceholder":76},[59,254,255,257,259,261,263,265,267,269],{"class":61,"line":80},[59,256,234],{"class":69},[59,258,21],{"class":115},[59,260,145],{"class":124},[59,262,128],{"class":115},[59,264,164],{"class":90},[59,266,95],{"class":94},[59,268,164],{"class":90},[59,270,152],{"class":115},[59,272,273,276,278,280,282,284],{"class":61,"line":101},[59,274,275],{"class":158},"print",[59,277,128],{"class":115},[59,279,164],{"class":90},[59,281,167],{"class":94},[59,283,164],{"class":90},[59,285,152],{"class":115},[287,288,290],"h3",{"id":289},"what-this-code-does","What this code does",[26,292,293,299,305],{},[29,294,295,298],{},[18,296,297],{},"import os"," gives you access to file system tools",[29,300,301,304],{},[18,302,303],{},"os.remove(\"notes.txt\")"," deletes the file",[29,306,307,310],{},[18,308,309],{},"\"notes.txt\""," is the path to the file",[14,312,313],{},"You can use:",[26,315,316,321,327],{},[29,317,318,319],{},"a simple file name like ",[18,320,309],{},[29,322,323,324],{},"a relative path like ",[18,325,326],{},"\"data\u002Freport.csv\"",[29,328,329,330],{},"an absolute path like ",[18,331,332],{},"\"\u002FUsers\u002Fname\u002FDocuments\u002Fnotes.txt\"",[14,334,335],{},"This removes the file from the file system.",[14,337,338,339,21],{},"If you are new to file paths, see ",[340,341,343],"a",{"href":342},"\u002Flearn\u002Fworking-with-file-paths-in-python","working with file paths in Python",[45,345,347],{"id":346},"check-if-the-file-exists-first","Check if the file exists first",[14,349,350],{},"A beginner-friendly way to delete a file is to check for it first.",[50,352,353],{"className":52,"code":53,"language":54,"meta":55,"style":55},[18,354,355,361,365,377,381,401,415,429,435],{"__ignoreMap":55},[59,356,357,359],{"class":61,"line":62},[59,358,66],{"class":65},[59,360,70],{"class":69},[59,362,363],{"class":61,"line":73},[59,364,77],{"emptyLinePlaceholder":76},[59,366,367,369,371,373,375],{"class":61,"line":80},[59,368,83],{"class":69},[59,370,87],{"class":86},[59,372,91],{"class":90},[59,374,95],{"class":94},[59,376,98],{"class":90},[59,378,379],{"class":61,"line":101},[59,380,77],{"emptyLinePlaceholder":76},[59,382,383,385,387,389,391,393,395,397,399],{"class":61,"line":106},[59,384,109],{"class":65},[59,386,112],{"class":69},[59,388,21],{"class":115},[59,390,119],{"class":118},[59,392,21],{"class":115},[59,394,125],{"class":124},[59,396,128],{"class":115},[59,398,131],{"class":124},[59,400,134],{"class":115},[59,402,403,405,407,409,411,413],{"class":61,"line":137},[59,404,140],{"class":69},[59,406,21],{"class":115},[59,408,145],{"class":124},[59,410,128],{"class":115},[59,412,131],{"class":124},[59,414,152],{"class":115},[59,416,417,419,421,423,425,427],{"class":61,"line":155},[59,418,159],{"class":158},[59,420,128],{"class":115},[59,422,164],{"class":90},[59,424,167],{"class":94},[59,426,164],{"class":90},[59,428,152],{"class":115},[59,430,431,433],{"class":61,"line":174},[59,432,177],{"class":65},[59,434,180],{"class":115},[59,436,437,439,441,443,445,447],{"class":61,"line":183},[59,438,159],{"class":158},[59,440,128],{"class":115},[59,442,164],{"class":90},[59,444,192],{"class":94},[59,446,164],{"class":90},[59,448,152],{"class":115},[287,450,452],{"id":451},"why-this-helps","Why this helps",[26,454,455,460,463],{},[29,456,457,458],{},"It avoids ",[18,459,43],{},[29,461,462],{},"It makes your script easier to understand",[29,464,465],{},"It is useful when the file may or may not exist",[14,467,468,469,473,474,21],{},"If you want more detail, see ",[340,470,472],{"href":471},"\u002Fhow-to\u002Fhow-to-check-if-a-file-exists-in-python\u002F","how to check if a file exists in Python"," and ",[340,475,477,478,481],{"href":476},"\u002Fstandard-library\u002Fos.path.exists-function-explained","the ",[18,479,480],{},"os.path.exists()"," function explained",[45,483,485],{"id":484},"use-the-right-path","Use the right path",[14,487,488],{},"A wrong path is one of the most common reasons file deletion fails.",[287,490,492],{"id":491},"relative-path","Relative path",[14,494,495],{},"A relative path starts from the folder your script is currently using.",[50,497,499],{"className":52,"code":498,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"notes.txt\"\nprint(os.getcwd())  # shows the current working folder\n\nif os.path.exists(file_path):\n    os.remove(file_path)\n",[18,500,501,507,511,523,543,547,567],{"__ignoreMap":55},[59,502,503,505],{"class":61,"line":62},[59,504,66],{"class":65},[59,506,70],{"class":69},[59,508,509],{"class":61,"line":73},[59,510,77],{"emptyLinePlaceholder":76},[59,512,513,515,517,519,521],{"class":61,"line":80},[59,514,83],{"class":69},[59,516,87],{"class":86},[59,518,91],{"class":90},[59,520,95],{"class":94},[59,522,98],{"class":90},[59,524,525,527,529,531,533,536,539],{"class":61,"line":101},[59,526,275],{"class":158},[59,528,128],{"class":115},[59,530,234],{"class":124},[59,532,21],{"class":115},[59,534,535],{"class":124},"getcwd",[59,537,538],{"class":115},"())",[59,540,542],{"class":541},"sutJx","  # shows the current working folder\n",[59,544,545],{"class":61,"line":106},[59,546,77],{"emptyLinePlaceholder":76},[59,548,549,551,553,555,557,559,561,563,565],{"class":61,"line":137},[59,550,109],{"class":65},[59,552,112],{"class":69},[59,554,21],{"class":115},[59,556,119],{"class":118},[59,558,21],{"class":115},[59,560,125],{"class":124},[59,562,128],{"class":115},[59,564,131],{"class":124},[59,566,134],{"class":115},[59,568,569,571,573,575,577,579],{"class":61,"line":155},[59,570,140],{"class":69},[59,572,21],{"class":115},[59,574,145],{"class":124},[59,576,128],{"class":115},[59,578,131],{"class":124},[59,580,152],{"class":115},[287,582,584],{"id":583},"absolute-path","Absolute path",[14,586,587],{},"An absolute path gives the full location of the file.",[50,589,591],{"className":52,"code":590,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"\u002Fhome\u002Fuser\u002Fproject\u002Fnotes.txt\"\n\nif os.path.exists(file_path):\n    os.remove(file_path)\n",[18,592,593,599,603,616,620,640],{"__ignoreMap":55},[59,594,595,597],{"class":61,"line":62},[59,596,66],{"class":65},[59,598,70],{"class":69},[59,600,601],{"class":61,"line":73},[59,602,77],{"emptyLinePlaceholder":76},[59,604,605,607,609,611,614],{"class":61,"line":80},[59,606,83],{"class":69},[59,608,87],{"class":86},[59,610,91],{"class":90},[59,612,613],{"class":94},"\u002Fhome\u002Fuser\u002Fproject\u002Fnotes.txt",[59,615,98],{"class":90},[59,617,618],{"class":61,"line":101},[59,619,77],{"emptyLinePlaceholder":76},[59,621,622,624,626,628,630,632,634,636,638],{"class":61,"line":106},[59,623,109],{"class":65},[59,625,112],{"class":69},[59,627,21],{"class":115},[59,629,119],{"class":118},[59,631,21],{"class":115},[59,633,125],{"class":124},[59,635,128],{"class":115},[59,637,131],{"class":124},[59,639,134],{"class":115},[59,641,642,644,646,648,650,652],{"class":61,"line":137},[59,643,140],{"class":69},[59,645,21],{"class":115},[59,647,145],{"class":124},[59,649,128],{"class":115},[59,651,131],{"class":124},[59,653,152],{"class":115},[287,655,657],{"id":656},"if-you-are-not-sure-what-path-your-code-is-using","If you are not sure what path your code is using",[14,659,660],{},"Print the path before deleting:",[50,662,664],{"className":52,"code":663,"language":54,"meta":55,"style":55},"file_path = \"notes.txt\"\nprint(file_path)\n",[18,665,666,678],{"__ignoreMap":55},[59,667,668,670,672,674,676],{"class":61,"line":62},[59,669,83],{"class":69},[59,671,87],{"class":86},[59,673,91],{"class":90},[59,675,95],{"class":94},[59,677,98],{"class":90},[59,679,680,682,684,686],{"class":61,"line":73},[59,681,275],{"class":158},[59,683,128],{"class":115},[59,685,131],{"class":124},[59,687,152],{"class":115},[14,689,690],{},"You can also print the current working folder:",[50,692,694],{"className":52,"code":693,"language":54,"meta":55,"style":55},"import os\nprint(os.getcwd())\n",[18,695,696,702],{"__ignoreMap":55},[59,697,698,700],{"class":61,"line":62},[59,699,66],{"class":65},[59,701,70],{"class":69},[59,703,704,706,708,710,712,714],{"class":61,"line":73},[59,705,275],{"class":158},[59,707,128],{"class":115},[59,709,234],{"class":124},[59,711,21],{"class":115},[59,713,535],{"class":124},[59,715,716],{"class":115},"())\n",[14,718,719,720,473,723,21],{},"If your file path problems are causing errors, see ",[340,721,722],{"href":342},"how to understand file paths in Python",[340,724,726,727],{"href":725},"\u002Ferrors\u002Ffilenotfounderror-errno-2-no-such-file-or-directory-fix","how to fix ",[18,728,729],{},"FileNotFoundError: [Errno 2] No such file or directory",[45,731,733,734],{"id":732},"deleting-with-pathlib","Deleting with ",[18,735,736],{},"pathlib",[14,738,739,740,742],{},"You can also delete files with ",[18,741,736],{},", which many beginners find easier to read.",[50,744,746],{"className":52,"code":745,"language":54,"meta":55,"style":55},"from pathlib import Path\n\nfile_path = Path(\"notes.txt\")\n\nif file_path.exists():\n    file_path.unlink()\n    print(\"File deleted\")\nelse:\n    print(\"File does not exist\")\n",[18,747,748,761,765,784,788,802,815,829,835],{"__ignoreMap":55},[59,749,750,753,756,758],{"class":61,"line":62},[59,751,752],{"class":65},"from",[59,754,755],{"class":69}," pathlib ",[59,757,66],{"class":65},[59,759,760],{"class":69}," Path\n",[59,762,763],{"class":61,"line":73},[59,764,77],{"emptyLinePlaceholder":76},[59,766,767,769,771,774,776,778,780,782],{"class":61,"line":80},[59,768,83],{"class":69},[59,770,87],{"class":86},[59,772,773],{"class":124}," Path",[59,775,128],{"class":115},[59,777,164],{"class":90},[59,779,95],{"class":94},[59,781,164],{"class":90},[59,783,152],{"class":115},[59,785,786],{"class":61,"line":101},[59,787,77],{"emptyLinePlaceholder":76},[59,789,790,792,795,797,799],{"class":61,"line":106},[59,791,109],{"class":65},[59,793,794],{"class":69}," file_path",[59,796,21],{"class":115},[59,798,125],{"class":124},[59,800,801],{"class":115},"():\n",[59,803,804,807,809,812],{"class":61,"line":137},[59,805,806],{"class":69},"    file_path",[59,808,21],{"class":115},[59,810,811],{"class":124},"unlink",[59,813,814],{"class":115},"()\n",[59,816,817,819,821,823,825,827],{"class":61,"line":155},[59,818,159],{"class":158},[59,820,128],{"class":115},[59,822,164],{"class":90},[59,824,167],{"class":94},[59,826,164],{"class":90},[59,828,152],{"class":115},[59,830,831,833],{"class":61,"line":174},[59,832,177],{"class":65},[59,834,180],{"class":115},[59,836,837,839,841,843,845,847],{"class":61,"line":183},[59,838,159],{"class":158},[59,840,128],{"class":115},[59,842,164],{"class":90},[59,844,192],{"class":94},[59,846,164],{"class":90},[59,848,152],{"class":115},[287,850,852,853,855],{"id":851},"why-use-pathlib","Why use ",[18,854,736],{},"?",[26,857,858,861,867],{},[29,859,860],{},"The code often reads more clearly",[29,862,863,866],{},[18,864,865],{},"Path"," objects are convenient for file work",[29,868,869],{},"It works well when you do many path-related tasks in one script",[14,871,872,875],{},[18,873,874],{},"Path.unlink()"," is for files, not directories.",[14,877,878],{},"Both styles are fine:",[26,880,881,886],{},[29,882,883],{},[18,884,885],{},"os.remove(path)",[29,887,888],{},[18,889,890],{},"Path(path).unlink()",[14,892,893],{},"Choose one style and stay consistent in your script.",[45,895,897],{"id":896},"common-errors-when-deleting-files","Common errors when deleting files",[14,899,900],{},"Here are the main errors you may see.",[287,902,904],{"id":903},"filenotfounderror",[18,905,43],{},[14,907,908],{},"This usually means:",[26,910,911,914,917],{},[29,912,913],{},"the path is wrong",[29,915,916],{},"the file was already deleted",[29,918,919],{},"your script is running in a different folder than you expected",[14,921,922],{},"Example:",[50,924,926],{"className":52,"code":925,"language":54,"meta":55,"style":55},"import os\n\nos.remove(\"missing_file.txt\")\n",[18,927,928,934,938],{"__ignoreMap":55},[59,929,930,932],{"class":61,"line":62},[59,931,66],{"class":65},[59,933,70],{"class":69},[59,935,936],{"class":61,"line":73},[59,937,77],{"emptyLinePlaceholder":76},[59,939,940,942,944,946,948,950,953,955],{"class":61,"line":80},[59,941,234],{"class":69},[59,943,21],{"class":115},[59,945,145],{"class":124},[59,947,128],{"class":115},[59,949,164],{"class":90},[59,951,952],{"class":94},"missing_file.txt",[59,954,164],{"class":90},[59,956,152],{"class":115},[14,958,959],{},"Possible fix:",[50,961,963],{"className":52,"code":962,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"missing_file.txt\"\n\nif os.path.exists(file_path):\n    os.remove(file_path)\nelse:\n    print(\"File does not exist\")\n",[18,964,965,971,975,987,991,1011,1025,1031],{"__ignoreMap":55},[59,966,967,969],{"class":61,"line":62},[59,968,66],{"class":65},[59,970,70],{"class":69},[59,972,973],{"class":61,"line":73},[59,974,77],{"emptyLinePlaceholder":76},[59,976,977,979,981,983,985],{"class":61,"line":80},[59,978,83],{"class":69},[59,980,87],{"class":86},[59,982,91],{"class":90},[59,984,952],{"class":94},[59,986,98],{"class":90},[59,988,989],{"class":61,"line":101},[59,990,77],{"emptyLinePlaceholder":76},[59,992,993,995,997,999,1001,1003,1005,1007,1009],{"class":61,"line":106},[59,994,109],{"class":65},[59,996,112],{"class":69},[59,998,21],{"class":115},[59,1000,119],{"class":118},[59,1002,21],{"class":115},[59,1004,125],{"class":124},[59,1006,128],{"class":115},[59,1008,131],{"class":124},[59,1010,134],{"class":115},[59,1012,1013,1015,1017,1019,1021,1023],{"class":61,"line":137},[59,1014,140],{"class":69},[59,1016,21],{"class":115},[59,1018,145],{"class":124},[59,1020,128],{"class":115},[59,1022,131],{"class":124},[59,1024,152],{"class":115},[59,1026,1027,1029],{"class":61,"line":155},[59,1028,177],{"class":65},[59,1030,180],{"class":115},[59,1032,1033,1035,1037,1039,1041,1043],{"class":61,"line":174},[59,1034,159],{"class":158},[59,1036,128],{"class":115},[59,1038,164],{"class":90},[59,1040,192],{"class":94},[59,1042,164],{"class":90},[59,1044,152],{"class":115},[14,1046,1047,1048,21],{},"For a full fix guide, see ",[340,1049,1050],{"href":725},[18,1051,729],{},[287,1053,1055],{"id":1054},"permissionerror",[18,1056,1057],{},"PermissionError",[14,1059,1060],{},"This means your program does not have permission to delete the file.",[14,1062,922],{},[50,1064,1066],{"className":52,"code":1065,"language":54,"meta":55,"style":55},"import os\n\nos.remove(\"protected_file.txt\")\n",[18,1067,1068,1074,1078],{"__ignoreMap":55},[59,1069,1070,1072],{"class":61,"line":62},[59,1071,66],{"class":65},[59,1073,70],{"class":69},[59,1075,1076],{"class":61,"line":73},[59,1077,77],{"emptyLinePlaceholder":76},[59,1079,1080,1082,1084,1086,1088,1090,1093,1095],{"class":61,"line":80},[59,1081,234],{"class":69},[59,1083,21],{"class":115},[59,1085,145],{"class":124},[59,1087,128],{"class":115},[59,1089,164],{"class":90},[59,1091,1092],{"class":94},"protected_file.txt",[59,1094,164],{"class":90},[59,1096,152],{"class":115},[14,1098,1099],{},"This can happen if:",[26,1101,1102,1105,1108],{},[29,1103,1104],{},"the file belongs to another user",[29,1106,1107],{},"the file is protected by the operating system",[29,1109,1110],{},"another program is locking the file",[14,1112,1113,1114,21],{},"See ",[340,1115,726,1117],{"href":1116},"\u002Ferrors\u002Fpermissionerror-errno-13-permission-denied-fix",[18,1118,1119],{},"PermissionError: [Errno 13] Permission denied",[287,1121,1123],{"id":1122},"isadirectoryerror",[18,1124,1125],{},"IsADirectoryError",[14,1127,1128],{},"This happens when you try to delete a folder with a file function.",[50,1130,1132],{"className":52,"code":1131,"language":54,"meta":55,"style":55},"import os\n\nos.remove(\"my_folder\")\n",[18,1133,1134,1140,1144],{"__ignoreMap":55},[59,1135,1136,1138],{"class":61,"line":62},[59,1137,66],{"class":65},[59,1139,70],{"class":69},[59,1141,1142],{"class":61,"line":73},[59,1143,77],{"emptyLinePlaceholder":76},[59,1145,1146,1148,1150,1152,1154,1156,1159,1161],{"class":61,"line":80},[59,1147,234],{"class":69},[59,1149,21],{"class":115},[59,1151,145],{"class":124},[59,1153,128],{"class":115},[59,1155,164],{"class":90},[59,1157,1158],{"class":94},"my_folder",[59,1160,164],{"class":90},[59,1162,152],{"class":115},[14,1164,1165,1167],{},[18,1166,20],{}," is for files only.",[14,1169,1113,1170,1176],{},[340,1171,726,1173],{"href":1172},"\u002Ferrors\u002Fisadirectoryerror-errno-21-is-a-directory-fix",[18,1174,1175],{},"IsADirectoryError: [Errno 21] Is a directory"," if that happens.",[287,1178,1180],{"id":1179},"oserror",[18,1181,1182],{},"OSError",[14,1184,1185],{},"This is a broader file system error. It can happen for different system-level reasons.",[14,1187,1188,1189,473,1192,1195],{},"A simple way to handle deletion errors is to use ",[18,1190,1191],{},"try",[18,1193,1194],{},"except",":",[50,1197,1199],{"className":52,"code":1198,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"notes.txt\"\n\ntry:\n    os.remove(file_path)\n    print(\"File deleted\")\nexcept FileNotFoundError:\n    print(\"File does not exist\")\nexcept PermissionError:\n    print(\"You do not have permission to delete this file\")\nexcept OSError as error:\n    print(\"A file system error happened:\", error)\n",[18,1200,1201,1207,1211,1223,1227,1233,1247,1261,1271,1285,1295,1311,1327],{"__ignoreMap":55},[59,1202,1203,1205],{"class":61,"line":62},[59,1204,66],{"class":65},[59,1206,70],{"class":69},[59,1208,1209],{"class":61,"line":73},[59,1210,77],{"emptyLinePlaceholder":76},[59,1212,1213,1215,1217,1219,1221],{"class":61,"line":80},[59,1214,83],{"class":69},[59,1216,87],{"class":86},[59,1218,91],{"class":90},[59,1220,95],{"class":94},[59,1222,98],{"class":90},[59,1224,1225],{"class":61,"line":101},[59,1226,77],{"emptyLinePlaceholder":76},[59,1228,1229,1231],{"class":61,"line":106},[59,1230,1191],{"class":65},[59,1232,180],{"class":115},[59,1234,1235,1237,1239,1241,1243,1245],{"class":61,"line":137},[59,1236,140],{"class":69},[59,1238,21],{"class":115},[59,1240,145],{"class":124},[59,1242,128],{"class":115},[59,1244,131],{"class":124},[59,1246,152],{"class":115},[59,1248,1249,1251,1253,1255,1257,1259],{"class":61,"line":155},[59,1250,159],{"class":158},[59,1252,128],{"class":115},[59,1254,164],{"class":90},[59,1256,167],{"class":94},[59,1258,164],{"class":90},[59,1260,152],{"class":115},[59,1262,1263,1265,1269],{"class":61,"line":174},[59,1264,1194],{"class":65},[59,1266,1268],{"class":1267},"sZMiF"," FileNotFoundError",[59,1270,180],{"class":115},[59,1272,1273,1275,1277,1279,1281,1283],{"class":61,"line":183},[59,1274,159],{"class":158},[59,1276,128],{"class":115},[59,1278,164],{"class":90},[59,1280,192],{"class":94},[59,1282,164],{"class":90},[59,1284,152],{"class":115},[59,1286,1288,1290,1293],{"class":61,"line":1287},10,[59,1289,1194],{"class":65},[59,1291,1292],{"class":1267}," PermissionError",[59,1294,180],{"class":115},[59,1296,1298,1300,1302,1304,1307,1309],{"class":61,"line":1297},11,[59,1299,159],{"class":158},[59,1301,128],{"class":115},[59,1303,164],{"class":90},[59,1305,1306],{"class":94},"You do not have permission to delete this file",[59,1308,164],{"class":90},[59,1310,152],{"class":115},[59,1312,1314,1316,1319,1322,1325],{"class":61,"line":1313},12,[59,1315,1194],{"class":65},[59,1317,1318],{"class":1267}," OSError",[59,1320,1321],{"class":65}," as",[59,1323,1324],{"class":69}," error",[59,1326,180],{"class":115},[59,1328,1330,1332,1334,1336,1339,1341,1344,1346],{"class":61,"line":1329},13,[59,1331,159],{"class":158},[59,1333,128],{"class":115},[59,1335,164],{"class":90},[59,1337,1338],{"class":94},"A file system error happened:",[59,1340,164],{"class":90},[59,1342,1343],{"class":115},",",[59,1345,1324],{"class":124},[59,1347,152],{"class":115},[45,1349,1351],{"id":1350},"simple-safety-tips","Simple safety tips",[14,1353,1354],{},"When deleting files, a few small checks can prevent bigger mistakes.",[26,1356,1357,1360,1363,1366],{},[29,1358,1359],{},"Print the file path before deleting important files",[29,1361,1362],{},"Test your code with a sample file first",[29,1364,1365],{},"Do not delete files directly from unchecked user input",[29,1367,199,1368,1371],{},[18,1369,1370],{},"try-except"," if your script should keep running after an error",[14,1373,922],{},[50,1375,1377],{"className":52,"code":1376,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"test_file.txt\"\nprint(\"About to delete:\", file_path)\n\ntry:\n    if os.path.isfile(file_path):\n        os.remove(file_path)\n        print(\"File deleted\")\n    else:\n        print(\"That path is not a file\")\nexcept PermissionError:\n    print(\"Permission denied\")\n",[18,1378,1379,1385,1389,1402,1421,1425,1431,1453,1468,1483,1490,1505,1513],{"__ignoreMap":55},[59,1380,1381,1383],{"class":61,"line":62},[59,1382,66],{"class":65},[59,1384,70],{"class":69},[59,1386,1387],{"class":61,"line":73},[59,1388,77],{"emptyLinePlaceholder":76},[59,1390,1391,1393,1395,1397,1400],{"class":61,"line":80},[59,1392,83],{"class":69},[59,1394,87],{"class":86},[59,1396,91],{"class":90},[59,1398,1399],{"class":94},"test_file.txt",[59,1401,98],{"class":90},[59,1403,1404,1406,1408,1410,1413,1415,1417,1419],{"class":61,"line":101},[59,1405,275],{"class":158},[59,1407,128],{"class":115},[59,1409,164],{"class":90},[59,1411,1412],{"class":94},"About to delete:",[59,1414,164],{"class":90},[59,1416,1343],{"class":115},[59,1418,794],{"class":124},[59,1420,152],{"class":115},[59,1422,1423],{"class":61,"line":106},[59,1424,77],{"emptyLinePlaceholder":76},[59,1426,1427,1429],{"class":61,"line":137},[59,1428,1191],{"class":65},[59,1430,180],{"class":115},[59,1432,1433,1436,1438,1440,1442,1444,1447,1449,1451],{"class":61,"line":155},[59,1434,1435],{"class":65},"    if",[59,1437,112],{"class":69},[59,1439,21],{"class":115},[59,1441,119],{"class":118},[59,1443,21],{"class":115},[59,1445,1446],{"class":124},"isfile",[59,1448,128],{"class":115},[59,1450,131],{"class":124},[59,1452,134],{"class":115},[59,1454,1455,1458,1460,1462,1464,1466],{"class":61,"line":174},[59,1456,1457],{"class":69},"        os",[59,1459,21],{"class":115},[59,1461,145],{"class":124},[59,1463,128],{"class":115},[59,1465,131],{"class":124},[59,1467,152],{"class":115},[59,1469,1470,1473,1475,1477,1479,1481],{"class":61,"line":183},[59,1471,1472],{"class":158},"        print",[59,1474,128],{"class":115},[59,1476,164],{"class":90},[59,1478,167],{"class":94},[59,1480,164],{"class":90},[59,1482,152],{"class":115},[59,1484,1485,1488],{"class":61,"line":1287},[59,1486,1487],{"class":65},"    else",[59,1489,180],{"class":115},[59,1491,1492,1494,1496,1498,1501,1503],{"class":61,"line":1297},[59,1493,1472],{"class":158},[59,1495,128],{"class":115},[59,1497,164],{"class":90},[59,1499,1500],{"class":94},"That path is not a file",[59,1502,164],{"class":90},[59,1504,152],{"class":115},[59,1506,1507,1509,1511],{"class":61,"line":1313},[59,1508,1194],{"class":65},[59,1510,1292],{"class":1267},[59,1512,180],{"class":115},[59,1514,1515,1517,1519,1521,1524,1526],{"class":61,"line":1329},[59,1516,159],{"class":158},[59,1518,128],{"class":115},[59,1520,164],{"class":90},[59,1522,1523],{"class":94},"Permission denied",[59,1525,164],{"class":90},[59,1527,152],{"class":115},[45,1529,1531],{"id":1530},"common-mistakes","Common mistakes",[14,1533,1534],{},"These are common causes of problems when deleting files:",[26,1536,1537,1540,1543,1546,1549],{},[29,1538,1539],{},"Using the wrong file path",[29,1541,1542],{},"Trying to delete a folder instead of a file",[29,1544,1545],{},"Deleting a file that does not exist",[29,1547,1548],{},"Not having permission to delete the file",[29,1550,1551],{},"Confusing relative paths with absolute paths",[14,1553,1554],{},"Useful debugging checks:",[50,1556,1558],{"className":52,"code":1557,"language":54,"meta":55,"style":55},"print(file_path)\n",[18,1559,1560],{"__ignoreMap":55},[59,1561,1562,1564,1566,1568],{"class":61,"line":62},[59,1563,275],{"class":158},[59,1565,128],{"class":115},[59,1567,131],{"class":124},[59,1569,152],{"class":115},[50,1571,1572],{"className":52,"code":693,"language":54,"meta":55,"style":55},[18,1573,1574,1580],{"__ignoreMap":55},[59,1575,1576,1578],{"class":61,"line":62},[59,1577,66],{"class":65},[59,1579,70],{"class":69},[59,1581,1582,1584,1586,1588,1590,1592],{"class":61,"line":73},[59,1583,275],{"class":158},[59,1585,128],{"class":115},[59,1587,234],{"class":124},[59,1589,21],{"class":115},[59,1591,535],{"class":124},[59,1593,716],{"class":115},[50,1595,1597],{"className":52,"code":1596,"language":54,"meta":55,"style":55},"import os\nprint(os.path.exists(file_path))\n",[18,1598,1599,1605],{"__ignoreMap":55},[59,1600,1601,1603],{"class":61,"line":62},[59,1602,66],{"class":65},[59,1604,70],{"class":69},[59,1606,1607,1609,1611,1613,1615,1617,1619,1621,1623,1625],{"class":61,"line":73},[59,1608,275],{"class":158},[59,1610,128],{"class":115},[59,1612,234],{"class":124},[59,1614,21],{"class":115},[59,1616,119],{"class":118},[59,1618,21],{"class":115},[59,1620,125],{"class":124},[59,1622,128],{"class":115},[59,1624,131],{"class":124},[59,1626,1627],{"class":115},"))\n",[50,1629,1631],{"className":52,"code":1630,"language":54,"meta":55,"style":55},"import os\nprint(os.path.isfile(file_path))\n",[18,1632,1633,1639],{"__ignoreMap":55},[59,1634,1635,1637],{"class":61,"line":62},[59,1636,66],{"class":65},[59,1638,70],{"class":69},[59,1640,1641,1643,1645,1647,1649,1651,1653,1655,1657,1659],{"class":61,"line":73},[59,1642,275],{"class":158},[59,1644,128],{"class":115},[59,1646,234],{"class":124},[59,1648,21],{"class":115},[59,1650,119],{"class":118},[59,1652,21],{"class":115},[59,1654,1446],{"class":124},[59,1656,128],{"class":115},[59,1658,131],{"class":124},[59,1660,1627],{"class":115},[50,1662,1664],{"className":52,"code":1663,"language":54,"meta":55,"style":55},"import os\nprint(os.listdir())\n",[18,1665,1666,1672],{"__ignoreMap":55},[59,1667,1668,1670],{"class":61,"line":62},[59,1669,66],{"class":65},[59,1671,70],{"class":69},[59,1673,1674,1676,1678,1680,1682,1685],{"class":61,"line":73},[59,1675,275],{"class":158},[59,1677,128],{"class":115},[59,1679,234],{"class":124},[59,1681,21],{"class":115},[59,1683,1684],{"class":124},"listdir",[59,1686,716],{"class":115},[14,1688,1689],{},"These commands help you answer basic questions:",[26,1691,1692,1695,1698,1701,1704],{},[29,1693,1694],{},"What path is my code using?",[29,1696,1697],{},"Which folder is my script running in?",[29,1699,1700],{},"Does the path exist?",[29,1702,1703],{},"Is it really a file?",[29,1705,1706],{},"What files are in the current folder?",[45,1708,1710],{"id":1709},"faq","FAQ",[287,1712,1714],{"id":1713},"what-is-the-easiest-way-to-delete-a-file-in-python","What is the easiest way to delete a file in Python?",[14,1716,199,1717,1719,1720,1723],{},[18,1718,885],{},". For a safer beginner approach, check ",[18,1721,1722],{},"os.path.exists(path)"," first.",[287,1725,1727],{"id":1726},"how-do-i-delete-a-file-only-if-it-exists","How do I delete a file only if it exists?",[14,1729,1730,1731,1733,1734,1736,1737,21],{},"Use an ",[18,1732,109],{}," statement with ",[18,1735,1722],{},", then call ",[18,1738,885],{},[50,1740,1742],{"className":52,"code":1741,"language":54,"meta":55,"style":55},"import os\n\nfile_path = \"notes.txt\"\n\nif os.path.exists(file_path):\n    os.remove(file_path)\n",[18,1743,1744,1750,1754,1766,1770,1790],{"__ignoreMap":55},[59,1745,1746,1748],{"class":61,"line":62},[59,1747,66],{"class":65},[59,1749,70],{"class":69},[59,1751,1752],{"class":61,"line":73},[59,1753,77],{"emptyLinePlaceholder":76},[59,1755,1756,1758,1760,1762,1764],{"class":61,"line":80},[59,1757,83],{"class":69},[59,1759,87],{"class":86},[59,1761,91],{"class":90},[59,1763,95],{"class":94},[59,1765,98],{"class":90},[59,1767,1768],{"class":61,"line":101},[59,1769,77],{"emptyLinePlaceholder":76},[59,1771,1772,1774,1776,1778,1780,1782,1784,1786,1788],{"class":61,"line":106},[59,1773,109],{"class":65},[59,1775,112],{"class":69},[59,1777,21],{"class":115},[59,1779,119],{"class":118},[59,1781,21],{"class":115},[59,1783,125],{"class":124},[59,1785,128],{"class":115},[59,1787,131],{"class":124},[59,1789,134],{"class":115},[59,1791,1792,1794,1796,1798,1800,1802],{"class":61,"line":137},[59,1793,140],{"class":69},[59,1795,21],{"class":115},[59,1797,145],{"class":124},[59,1799,128],{"class":115},[59,1801,131],{"class":124},[59,1803,152],{"class":115},[287,1805,1807,1808,1810],{"id":1806},"can-osremove-delete-a-folder","Can ",[18,1809,20],{}," delete a folder?",[14,1812,1813,1814,21],{},"No. It is for files. Folders need a different function. If you need to create folders, see ",[340,1815,1817],{"href":1816},"\u002Fhow-to\u002Fhow-to-create-a-directory-in-python\u002F","how to create a directory in Python",[287,1819,1821,1822,1824],{"id":1820},"why-do-i-get-filenotfounderror-when-deleting-a-file","Why do I get ",[18,1823,43],{}," when deleting a file?",[14,1826,1827],{},"Usually:",[26,1829,1830,1833,1835],{},[29,1831,1832],{},"the file path is wrong",[29,1834,916],{},[29,1836,1837],{},"your script is running in a different folder than you expect",[287,1839,1841,1842,1844,1845,855],{"id":1840},"should-i-use-osremove-or-pathlib","Should I use ",[18,1843,20],{}," or ",[18,1846,736],{},[14,1848,1849],{},"Both work.",[26,1851,1852,1857],{},[29,1853,1854,1856],{},[18,1855,20],{}," is common and simple",[29,1858,1859,1862],{},[18,1860,1861],{},"pathlib.Path.unlink()"," is also clear and beginner-friendly",[45,1864,1866],{"id":1865},"see-also","See also",[26,1868,1869,1878,1883,1889,1897],{},[29,1870,1871],{},[340,1872,1874,1875,1877],{"href":1873},"\u002Fstandard-library\u002Fpython-os-module-overview","Python ",[18,1876,234],{}," module overview",[29,1879,1880],{},[340,1881,1882],{"href":471},"How to check if a file exists in Python",[29,1884,1885],{},[340,1886,1888],{"href":1887},"\u002Fhow-to\u002Fhow-to-rename-a-file-in-python\u002F","How to rename a file in Python",[29,1890,1891],{},[340,1892,1874,1894,481],{"href":1893},"\u002Freference\u002Fpython-open-function-explained",[18,1895,1896],{},"open()",[29,1898,1899],{},[340,1900,1901],{"href":342},"Working with file paths in Python",[1903,1904,1905],"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 .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 .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":55,"searchDepth":73,"depth":73,"links":1907},[1908,1909,1910,1914,1917,1922,1927,1933,1934,1935,1945],{"id":47,"depth":73,"text":48},{"id":207,"depth":73,"text":208},{"id":225,"depth":73,"text":1911,"children":1912},"Delete a file with os.remove()",[1913],{"id":289,"depth":80,"text":290},{"id":346,"depth":73,"text":347,"children":1915},[1916],{"id":451,"depth":80,"text":452},{"id":484,"depth":73,"text":485,"children":1918},[1919,1920,1921],{"id":491,"depth":80,"text":492},{"id":583,"depth":80,"text":584},{"id":656,"depth":80,"text":657},{"id":732,"depth":73,"text":1923,"children":1924},"Deleting with pathlib",[1925],{"id":851,"depth":80,"text":1926},"Why use pathlib?",{"id":896,"depth":73,"text":897,"children":1928},[1929,1930,1931,1932],{"id":903,"depth":80,"text":43},{"id":1054,"depth":80,"text":1057},{"id":1122,"depth":80,"text":1125},{"id":1179,"depth":80,"text":1182},{"id":1350,"depth":73,"text":1351},{"id":1530,"depth":73,"text":1531},{"id":1709,"depth":73,"text":1710,"children":1936},[1937,1938,1939,1941,1943],{"id":1713,"depth":80,"text":1714},{"id":1726,"depth":80,"text":1727},{"id":1806,"depth":80,"text":1940},"Can os.remove() delete a folder?",{"id":1820,"depth":80,"text":1942},"Why do I get FileNotFoundError when deleting a file?",{"id":1840,"depth":80,"text":1944},"Should I use os.remove() or pathlib?",{"id":1865,"depth":73,"text":1866},"Master how to delete a file in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-delete-a-file-in-python",{"title":5,"description":1946},"how-to\u002Fhow-to-delete-a-file-in-python","aP-pXt5KGRAGVlJ50LOk8v8oS8iqSRAtUiCI9JXtMAE",1777585483732]