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