Hướng dẫn how do you pass arguments to a python program? - làm thế nào để bạn truyền các đối số cho một chương trình python?
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 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)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 #!/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 $ python test.py arg1 arg2 arg30 $ python test.py arg1 arg2 arg31 $ python test.py arg1 arg2 arg32 $ python test.py arg1 arg2 arg33
#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0____11 #!/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 $ python test.py arg1 arg2 arg36 $ python test.py arg1 arg2 arg37 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 $ python test.py arg1 arg2 arg39 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']0 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 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)5 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']6 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']7 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0____11 #!/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 $ python test.py arg1 arg2 arg36 $ python test.py arg1 arg2 arg37 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 $ python test.py arg1 arg2 arg39 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']0 #!/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, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i2 #!/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 -i3 getopt.getopt(args, options, [long_options])4 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']0 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;
#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)0____11 #!/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 $ python test.py arg1 arg2 arg36 $ python test.py arg1 arg2 arg37 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 $ python test.py arg1 arg2 arg39 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']0 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 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)5 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']6 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']7 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']8 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)4 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)1 $ python test.py arg1 arg2 arg39 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']0 getopt.getopt(args, options, [long_options])4 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 $ python test.py arg1 arg2 arg32 Number of arguments: 4 arguments. Argument List: ['test.py', 'arg1', 'arg2', 'arg3']8 getopt.getopt(args, options, [long_options])4 usage: test.py -i6 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)1 usage: test.py -i8 usage: test.py -i9 Mô -đun Python GetOpt tương tự như hàm getOpt () của C. Không giống như mô -đun SYS Module GetOpt Mở rộng phân tách chuỗi đầu vào bằng xác thực tham số. Nó cho phép cả hai tùy chọn ngắn và dài bao gồm một gán giá trị. Tuy nhiên, mô -đun này yêu cầu sử dụng mô -đun SYS để xử lý dữ liệu đầu vào đúng cách. Để sử dụng mô-đun GetOpt, bắt buộc phải xóa phần tử đầu tiên khỏi danh sách các đối số dòng lệnh. & Nbsp; & nbsp; Cú pháp: getOpt.getOpt (args, Tùy chọn, [long_options]) tham số: & nbsp; args: danh sách các đối số sẽ được truyền. & Nbsp; Tùy chọn: Chuỗi các chữ cái tùy chọn mà tập lệnh muốn nhận ra. Các tùy chọn yêu cầu một đối số phải được theo sau bởi một dấu hai chấm (:). & Nbsp; long_options: Danh sách chuỗi có tên của các tùy chọn dài. Các tùy chọn yêu cầu đối số phải được theo sau bởi một dấu hiệu bằng nhau (=). Loại trả về: Giá trị trả về bao gồm hai phần tử: Đầu tiên là danh sách các cặp (tùy chọn, giá trị). Thứ hai là danh sách các đối số chương trình còn lại sau khi danh sách tùy chọn bị tước. & NBSP; & NBSP;
#!/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 -i7 Output: Sử dụng mô -đun argparseMô -đun SYS cung cấp các chức năng và biến được sử dụng để thao tác các phần khác nhau của môi trường thời gian chạy Python. Mô -đun này cung cấp quyền truy cập vào một số biến được sử dụng hoặc duy trì bởi trình thông dịch và các chức năng tương tác mạnh mẽ với trình thông dịch. Một biến đó là sys.argv là cấu trúc danh sách đơn giản. Mục đích chính của nó là: 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)50 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)51 #!/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)53 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)54 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)50 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)51 #!/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)53 & 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)54
#!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)51 #!/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)62 #!/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)64 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)50 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)51 #!/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)53 & 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)57 #!/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)59 #!/usr/bin/python import sys print 'Number of arguments:', len(sys.argv), 'arguments.' print 'Argument List:', str(sys.argv)51 #!/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)62 #!/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)64 Đầu ra: & nbsp; Output: Làm thế nào để bạn vượt qua các đối số chương trình?Chúng ta cũng có thể chuyển các đối số dòng lệnh cho một chương trình trong Eclipse bằng cách sử dụng các cấu hình chạy ... Bước 1: Mở cài đặt cấu hình chạy lớp. Từ trình chỉnh sửa lớp, nhấp chuột phải và chọn cách chạy AS AS -> Cấu hình Run Run. .... Bước 2: Chỉ định các đối số chương trình trong tab Đối số. .... Bước 3: Nhấp vào nút chạy .. Làm thế nào để bạn có được lập luận trong Python?Để truy cập các đối số dòng lệnh từ trong chương trình Python, trước tiên hãy nhập gói SYS. Sau đó bạn có thể tham khảo toàn bộ các đối số dòng lệnh, bao gồm chính tên hàm, bằng cách tham khảo danh sách có tên Argv.Trong cả hai trường hợp, Argv đề cập đến một danh sách các đối số dòng lệnh, tất cả được lưu trữ dưới dạng chuỗi.first import the sys package. You can then refer to the full set of command-line arguments, including the function name itself, by referring to a list named argv. In either case, argv refers to a list of command-line arguments, all stored as strings.
Làm thế nào để bạn chuyển một giá trị vào một tập lệnh Python?Cách truyền thông tin đến tập lệnh Python bằng lệnh sys.argv bằng cách mã hóa các biến đầu vào trong sổ ghi chép Jupyter hoặc thông qua việc sử dụng hàm đầu vào () tương tác.using the sys. argv command by hard-coding the input variables in Jupyter Notebook or through the interactive use of the input() function.
Có bao nhiêu cách bạn có thể vượt qua một cuộc tranh luận trong Python?5 loại đối số trong định nghĩa chức năng Python:.. Đối số mặc định .. Đối số từ khóa .. Đối số vị trí .. Đối số vị trí tùy ý .. Đối số từ khóa tùy ý .. |