Comment
Author: Admin | 2025-04-27
Is not specified, ValueError will be thouwn:Traceback (most recent call last): File "test.py", line 3, in _, arg1, arg2, arg3, *_ = sys.argv + [None] * 2ValueError: not enough values to unpack (expected at least 4, got 3) answered Dec 4, 2020 at 13:20 import sys# Command line arguments are stored into sys.argv# print(sys.argv[1:])# I used the slice [1:] to print all the elements except the first# This because the first element of sys.argv is the program name# So the first argument is sys.argv[1], the second is sys.argv[2] eccprint("File name: " + sys.argv[0])print("Arguments:")for i in sys.argv[1:]: print(i)Let's name this file command_line.py and let's run it:C:\Users\simone> python command_line.py arg1 arg2 arg3 eccFile name: command_line.pyArguments:arg1arg2arg3eccNow let's write a simple program, sum.py:import systry: print(sum(map(float, sys.argv[1:])))except: print("An error has occurred")Result:C:\Users\simone> python sum.py 10 4 6 323 answered Apr 29, 2022 at 13:55 NtLakeNtLake1313 bronze badges My solution is entrypoint2. Example:from entrypoint2 import entrypoint@entrypointdef add(file, quiet=True): ''' This function writes report. :param file: write report to FILE :param quiet: don't print status messages to stdout ''' print file,quiethelp text:usage: report.py [-h] [-q] [--debug] fileThis function writes report.positional arguments: file write report to FILEoptional arguments: -h, --help show this help message and exit -q, --quiet don't print status messages to stdout --debug set logging level to DEBUG answered Oct 24, 2011 at 12:48 pontyponty6048 silver badges8 bronze badges This handles simple switches, value switches with optional alternative flags.import sys# [IN] argv - array of args# [IN] switch - switch to seek# [IN] val - expecting value# [IN] alt - switch alternative# returns value or True if val not expecteddef parse_cmd(argv,switch,val=None,alt=None): for idx, x in enumerate(argv): if x == switch or x == alt: if val: if len(argv) > (idx+1): if not argv[idx+1].startswith('-'): return argv[idx+1] else: return True//expecting a value for -ii = parse_cmd(sys.argv[1:],"-i", True, "--input")//no value needed for -pp = parse_cmd(sys.argv[1:],"-p") answered May 26, 2022 at 9:23 O WigleyO Wigley2373 silver badges5 bronze badges Several of our biotechnology clients have posed these two questions recently:How can we execute a Python script as a command?How can we pass input values to a Python script when it is executed as a command?I have included a Python script below which I believe answers both questions. Let's assume the following Python script is saved in the file test.py:##----------------------------------------------------------------------## file name: test.py## input values: data - location of data to be processed# date - date data were delivered for processing# study - name of the study where data originated# logs - location where log files should be written ## macOS usage: ## python3 test.py "/Users/lawrence/data" "20220518" "XYZ123" "/Users/lawrence/logs"## Windows usage: ## python test.py "D:\data" "20220518" "XYZ123" "D:\logs"##----------------------------------------------------------------------## import needed modules...#import sysimport datetimedef main(argv): # # print message that process is starting...
Add Comment