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