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