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