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