Hướng dẫn can you pass arguments to python script? - bạn có thể chuyển đối số vào tập lệnh python không?
Python cung cấp một mô-đun GetOpt giúp bạn phân tích các tùy chọn và đối số dòng lệnh.getopt module that helps you parse command-line options and arguments. Show
$ python test.py arg1 arg2 arg3 Mô-đun Python SYS cung cấp quyền truy cập vào bất kỳ đối số dòng lệnh nào thông qua sys.argv. Điều này phục vụ hai mục đích -sys module provides access to any command-line arguments via the sys.argv. This serves two purposes −
Ở đây sys.argv [0] là chương trình tức là. Tên tập lệnh. Thí dụXem xét kiểm tra tập lệnh sau.py - #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv) Bây giờ chạy trên tập lệnh như sau - $ python test.py arg1 arg2 arg3 Sản phẩm này sau kết quả - Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3'] Lưu ý - Như đã đề cập ở trên, đối số đầu tiên luôn là tên tập lệnh và nó cũng được tính bằng số lượng đối số. − As mentioned above, first argument is always script name and it is also being counted in number of arguments. Phân tích đối số dòng lệnhPython cung cấp một mô-đun GetOpt giúp bạn phân tích các tùy chọn và đối số dòng lệnh. Mô -đun này cung cấp hai chức năng và một ngoại lệ để kích hoạt phân tích đối số dòng lệnh.getopt module that helps you parse command-line options and arguments. This module provides two functions and an exception to enable command line argument parsing. phương thức getOpt.getOptPhương thức này phân tích các tùy chọn dòng lệnh và danh sách tham số. Sau đây là cú pháp đơn giản cho phương pháp này - getopt.getopt(args, options, [long_options]) Dưới đây là chi tiết của các tham số -
Ngoại lệ getOpt.GetOpterRorĐiều này được nêu ra khi một tùy chọn không được công nhận được tìm thấy trong danh sách đối số hoặc khi một tùy chọn yêu cầu một đối số không được đưa ra. Đối số cho ngoại lệ là một chuỗi chỉ ra nguyên nhân của lỗi. Các thuộc tính MSG và OPT cung cấp thông báo lỗi và tùy chọn liên quan.msg and opt give the error message and related option. Thí dụHãy xem xét chúng tôi muốn truyền hai tên tệp thông qua dòng lệnh và chúng tôi cũng muốn đưa ra một tùy chọn để kiểm tra việc sử dụng tập lệnh. Việc sử dụng tập lệnh như sau - usage: test.py -i Đây là tập lệnh sau để kiểm tra.py - #!/usr/bin/python import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i Bây giờ, chạy trên tập lệnh như sau - $ test.py -h usage: test.py -i python_basic_syntax.htm Để thêm các đối số vào các tập lệnh Python, bạn sẽ phải sử dụng một mô-đun tích hợp có tên là Arg Argparse. Như tên cho thấy, nó phân tích các đối số dòng lệnh được sử dụng trong khi khởi chạy tập lệnh hoặc ứng dụng Python. Các đối số phân tích cú pháp này cũng được kiểm tra bởi mô -đun Arg Argparse để đảm bảo rằng chúng thuộc loại đúng kiểu.
Làm thế nào để bạn vượt qua các đối số trong một kịch bản? Các đối số có thể được chuyển đến tập lệnh khi nó được thực thi, bằng cách viết chúng dưới dạng danh sách được phân phối không gian theo tên tệp tập lệnh. Bên trong tập lệnh, biến $ 1 tham chiếu đối số đầu tiên trong dòng lệnh, $ 2 đối số thứ hai và vv. Biến $ 0 tham chiếu đến tập lệnh hiện tại. Các đối số được đưa ra theo tên của chương trình trong vỏ dòng lệnh của hệ điều hành được gọi là đối số dòng lệnh. Python cung cấp nhiều cách khác nhau để đối phó với các loại đối số này. Ba phổ biến nhất là: & nbsp;Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are:
Sử dụng sys.argvSử dụng mô -đun GetOpt
Len (sys.argv) cung cấp số lượng đối số dòng lệnh. Let’s suppose there is a Python script for adding two numbers and the numbers are passed as
command-line arguments. Python3sys.argv [0] là tên của tập lệnh Python hiện tại. & nbsp; & nbsp; Ví dụ: Hãy giả sử rằng có một tập lệnh Python để thêm hai số và các số được truyền dưới dạng đối số dòng lệnh. & NBSP; #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)8 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)9 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 $ python test.py arg1 arg2 arg32 $ python test.py arg1 arg2 arg33 $ python test.py arg1 arg2 arg34 $ python test.py arg1 arg2 arg35 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)2 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)4 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)5 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 $ python test.py arg1 arg2 arg38 $ python test.py arg1 arg2 arg39 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']1 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']2 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']4 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']5 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']8 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']9 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)2 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)4 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)5 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 $ python test.py arg1 arg2 arg38 $ python test.py arg1 arg2 arg39 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']1 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']2 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 #!/usr/bin/python import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i4 #!/usr/bin/python import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i5 getopt.getopt(args, options, [long_options])6 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']2 Output: Sử dụng mô -đun GetOptSử dụng mô -đun argparsegetopt module is similar to the
getopt() function of C. Unlike sys module getopt module extends the separation of the input string by parameter validation. It allows both short, and long options including a value assignment. However, this module requires the use of the sys module to process input data properly. To use getopt module, it is required to remove the first element from the list of command-line
arguments.
Example: Python3Nó là một danh sách các đối số dòng lệnh. Len (sys.argv) cung cấp số lượng đối số dòng lệnh. sys.argv [0] là tên của tập lệnh Python hiện tại. & nbsp; & nbsp; Ví dụ: Hãy giả sử rằng có một tập lệnh Python để thêm hai số và các số được truyền dưới dạng đối số dòng lệnh. & NBSP; 7 8#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)2 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)4 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)5 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 $ python test.py arg1 arg2 arg38 $ python test.py arg1 arg2 arg39 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']1 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']2 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']4 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']5 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']8 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']9 getopt.getopt(args, options, [long_options])0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 getopt.getopt(args, options, [long_options])2 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']1 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']2 getopt.getopt(args, options, [long_options])6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 $ python test.py arg1 arg2 arg34 8#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)13 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)00 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']5 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)33 #!/usr/bin/python import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i5 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)35 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)06 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)07 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)39 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)40 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)41 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)42 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)43 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)44 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)45 getopt.getopt(args, options, [long_options])0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)7 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)49 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)50 Output: Sử dụng mô -đun argparseSử dụng mô -đun ArgParse là một tùy chọn tốt hơn so với hai tùy chọn ở trên vì nó cung cấp rất nhiều tùy chọn như đối số vị trí, giá trị mặc định cho các đối số, thông báo trợ giúp, chỉ định loại dữ liệu của đối số, v.v. & NBSP; & NBSP; Lưu ý: Là một đối số tùy chọn mặc định, nó bao gồm -h, cùng với phiên bản dài của nó As a default optional argument, it includes -h, along with its long version –help. Ví dụ 1: Sử dụng cơ bản mô -đun Argparse. & NBSP; Basic use of argparse module. Python3#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)52 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)55 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)56 Output: & nbsp; & nbsp; Ví dụ 2: Thêm mô tả vào thông báo trợ giúp. & nbsp; Python3#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)52 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)55 & nbsp; & nbsp; Ví dụ 2: Thêm mô tả vào thông báo trợ giúp. & nbsp; #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)56
#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)64 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)66 Example 3: Defining optional value Python3#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)52 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)55 & nbsp; & nbsp; Ví dụ 2: Thêm mô tả vào thông báo trợ giúp. & nbsp; #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)59 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)61 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)64 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)3 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)66 Đầu ra: & nbsp; Output: Làm thế nào để bạn thêm các đối số vào tập lệnh Python?Để thêm các đối số vào các tập lệnh Python, bạn sẽ phải sử dụng một mô-đun tích hợp có tên là Arg Argparse. Như tên cho thấy, nó phân tích các đối số dòng lệnh được sử dụng trong khi khởi chạy tập lệnh hoặc ứng dụng Python. Các đối số phân tích cú pháp này cũng được kiểm tra bởi mô -đun Arg Argparse để đảm bảo rằng chúng thuộc loại đúng kiểu.use a built-in module named “argparse”. As the name suggests, it parses command line arguments used while launching a Python script or application. These parsed arguments are also checked by the “argparse” module to ensure that they are of proper “type”.
Làm thế nào để bạn vượt qua các đối số trong một kịch bản?Các đối số có thể được chuyển đến tập lệnh khi nó được thực thi, bằng cách viết chúng dưới dạng danh sách được phân phối không gian theo tên tệp tập lệnh.Bên trong tập lệnh, biến $ 1 tham chiếu đối số đầu tiên trong dòng lệnh, $ 2 đối số thứ hai và vv.Biến $ 0 tham chiếu đến tập lệnh hiện tại.by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
Danh sách nào trong Python chứa các đối số được chuyển cho một kịch bản?Mô -đun SYS hiển thị một mảng có tên Argv bao gồm các mục sau: Argv [0] chứa tên của chương trình Python hiện tại.Argv [1:], phần còn lại của danh sách, chứa bất kỳ và tất cả các đối số dòng lệnh Python được chuyển cho chương trình.argv[1:] , the rest of the list, contains any and all Python command line arguments passed to the program. |