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