Python chạy chương trình bên ngoài với các đối số

Thêm khả năng xử lý đối số dòng lệnh Python cung cấp giao diện thân thiện với người dùng cho chương trình dòng lệnh dựa trên văn bản của bạn. Nó tương tự như giao diện người dùng đồ họa dành cho một ứng dụng trực quan được thao tác bởi các thành phần đồ họa hoặc tiện ích con

Python đưa ra một cơ chế để nắm bắt và trích xuất các đối số dòng lệnh Python của bạn. Những giá trị này có thể được sử dụng để sửa đổi hành vi của một chương trình. Ví dụ: nếu chương trình của bạn xử lý dữ liệu được đọc từ tệp, thì bạn có thể chuyển tên tệp cho chương trình của mình, thay vì mã hóa cứng giá trị trong mã nguồn của bạn

Đến cuối hướng dẫn này, bạn sẽ biết

  • Nguồn gốc của các đối số dòng lệnh Python
  • Hỗ trợ cơ bản cho các đối số dòng lệnh Python
  • Các tiêu chuẩn hướng dẫn thiết kế giao diện dòng lệnh
  • Khái niệm cơ bản để tùy chỉnh thủ công và xử lý các đối số dòng lệnh Python
  • Các thư viện có sẵn trong Python để dễ dàng phát triển giao diện dòng lệnh phức tạp

Nếu bạn muốn có một cách thân thiện với người dùng để cung cấp các đối số dòng lệnh Python cho chương trình của mình mà không cần nhập thư viện chuyên dụng hoặc nếu bạn muốn hiểu rõ hơn về cơ sở chung cho các thư viện hiện có dành riêng cho việc xây dựng giao diện dòng lệnh Python, thì hãy tiếp tục

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Giao diện dòng lệnh

Giao diện dòng lệnh [CLI] cung cấp cách để người dùng tương tác với chương trình đang chạy trong trình thông dịch shell dựa trên văn bản. Một số ví dụ về trình thông dịch shell là Bash trên Linux hoặc Command Prompt trên Windows. Giao diện dòng lệnh được kích hoạt bởi trình thông dịch shell hiển thị một. Nó có thể được đặc trưng bởi các yếu tố sau

  • Một lệnh hoặc chương trình
  • Không hoặc nhiều đối số dòng lệnh
  • Một đầu ra đại diện cho kết quả của lệnh
  • Tài liệu văn bản được gọi là sử dụng hoặc trợ giúp

Không phải mọi giao diện dòng lệnh đều có thể cung cấp tất cả các yếu tố này, nhưng danh sách này cũng chưa đầy đủ. Độ phức tạp của dòng lệnh dao động từ khả năng chuyển một đối số đến nhiều đối số và tùy chọn, giống như Ngôn ngữ dành riêng cho miền. Ví dụ: một số chương trình có thể khởi chạy tài liệu web từ dòng lệnh hoặc bắt đầu một Python tương tự

Hai ví dụ sau với lệnh Python minh họa mô tả giao diện dòng lệnh

$ python -c "print['Real Python']"
Real Python

Trong ví dụ đầu tiên này, trình thông dịch Python chọn tùy chọn

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
1 cho lệnh, cho biết thực thi các đối số dòng lệnh Python theo tùy chọn
$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
1 dưới dạng chương trình Python

Một ví dụ khác cho thấy cách gọi Python bằng

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
3 để hiển thị trợ giúp

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]

Hãy thử điều này trong thiết bị đầu cuối của bạn để xem tài liệu trợ giúp đầy đủ

Loại bỏ các quảng cáo

Di sản C

Đối số dòng lệnh Python kế thừa trực tiếp từ ngôn ngữ lập trình C. Như Guido Van Rossum đã viết trong Giới thiệu về Python dành cho Lập trình viên Unix/C vào năm 1993, C có ảnh hưởng mạnh mẽ đến Python. Guido đề cập đến các định nghĩa về chữ, định danh, toán tử và các câu lệnh như

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
4,
$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
5 hoặc
$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
6. Việc sử dụng các đối số dòng lệnh Python cũng bị ảnh hưởng mạnh mẽ bởi ngôn ngữ C

Để minh họa những điểm tương đồng, hãy xem xét chương trình C sau đây

________số 8

Dòng 4 định nghĩa , là điểm vào của chương trình C. Lưu ý kỹ các thông số

  1. $ sha1sum --help
    Usage: sha1sum [OPTION].. [FILE]...
    Print or check SHA1 [160-bit] checksums.
    
    With no FILE, or when FILE is -, read standard input.
    
      -b, --binary         read in binary mode
      -c, --check          read SHA1 sums from the FILEs and check them
          --tag            create a BSD-style checksum
      -t, --text           read in text mode [default]
      -z, --zero           end each output line with NUL, not newline,
                           and disable file name escaping
    [ .. complete help text not shown .. ]
    
    8 là một số nguyên đại diện cho số đối số của chương trình
  2. $ sha1sum --help
    Usage: sha1sum [OPTION].. [FILE]...
    Print or check SHA1 [160-bit] checksums.
    
    With no FILE, or when FILE is -, read standard input.
    
      -b, --binary         read in binary mode
      -c, --check          read SHA1 sums from the FILEs and check them
          --tag            create a BSD-style checksum
      -t, --text           read in text mode [default]
      -z, --zero           end each output line with NUL, not newline,
                           and disable file name escaping
    [ .. complete help text not shown .. ]
    
    9 là một mảng con trỏ tới các ký tự chứa tên chương trình ở phần tử đầu tiên của mảng, tiếp theo là các đối số của chương trình nếu có ở các phần tử còn lại của mảng

Bạn có thể biên dịch mã ở trên trên Linux với

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
00, sau đó thực thi với
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
01 để có được thông tin sau

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
3

Trừ khi được thể hiện rõ ràng tại dòng lệnh với tùy chọn

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
02,
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
03 là tên mặc định của tệp thực thi được tạo bởi trình biên dịch gcc. Nó là viết tắt của đầu ra trình biên dịch chương trình và gợi nhớ đến các tệp thực thi được tạo trên các hệ thống UNIX cũ hơn. Quan sát rằng tên của tệp thực thi
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
01 là đối số duy nhất

Hãy thêm gia vị cho ví dụ này bằng cách chuyển một vài đối số dòng lệnh Python cho cùng một chương trình

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
7

Đầu ra cho thấy số lượng đối số là

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
05 và danh sách đối số bao gồm tên của chương trình,
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
06, theo sau là từng từ của cụm từ
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
07 mà bạn đã chuyển qua dòng lệnh

Ghi chú.

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
8 là viết tắt của số lượng đối số, trong khi
$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
9 là viết tắt của véc tơ đối số. Để tìm hiểu thêm, hãy xem A Little C Primer/C Command Line Arguments

Quá trình biên dịch

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
10 giả định rằng bạn đã sử dụng hệ thống Linux hoặc Mac OS. Trên Windows, bạn cũng có thể biên dịch chương trình C này bằng một trong các tùy chọn sau

  • Hệ thống con Windows cho Linux [WSL]. Nó có sẵn trong một số bản phân phối Linux, như Ubuntu, OpenSUSE và Debian, trong số những bản khác. Bạn có thể cài đặt nó từ Microsoft Store
  • Điều này bao gồm các công cụ xây dựng dòng lệnh của Windows, trình biên dịch Microsoft C/C++
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    11 và giao diện người dùng của trình biên dịch có tên là
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    12 cho C/C++
  • Microsoft Visual Studio. Đây là môi trường phát triển tích hợp [IDE] chính của Microsoft. Để tìm hiểu thêm về các IDE có thể được sử dụng cho cả Python và C trên các hệ điều hành khác nhau, bao gồm cả Windows, hãy xem Python IDE và Trình chỉnh sửa mã [Hướng dẫn]
  • dự án mingw-64. Điều này hỗ trợ trình biên dịch GCC trên Windows

Nếu bạn đã cài đặt Microsoft Visual Studio hoặc Windows Build Tools, thì bạn có thể biên dịch

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
10 như sau

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
7

Bạn sẽ nhận được một tệp thực thi có tên

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
14 mà bạn có thể bắt đầu với

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
9

Bạn có thể triển khai chương trình Python,

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
15, tương đương với chương trình C,
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
10, bạn đã thấy ở trên

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
2

Bạn không thấy biến

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
8 như trong ví dụ mã C. Nó không tồn tại trong Python vì
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 là đủ. Bạn có thể phân tích cú pháp các đối số dòng lệnh Python trong
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 mà không cần biết độ dài của danh sách và bạn có thể gọi
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
20 tích hợp sẵn nếu chương trình của bạn cần số lượng đối số

Ngoài ra, lưu ý rằng

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
21, khi được áp dụng cho một lần lặp, trả về một đối tượng ________ 422 có thể phát ra các cặp liên kết chỉ mục của một phần tử trong ________ 423 với giá trị tương ứng của nó. Điều này cho phép lặp qua nội dung của
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 mà không phải duy trì bộ đếm cho chỉ mục trong danh sách

Thực thi

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
15 như sau

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
2

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 chứa thông tin giống như trong chương trình C

  • Tên của chương trình
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    15 là mục đầu tiên của danh sách
  • Các đối số
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    28,
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    29,
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    30 và
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    31 là các phần tử còn lại trong danh sách

Với phần giới thiệu ngắn này về một số khía cạnh phức tạp của ngôn ngữ C, giờ đây bạn đã được trang bị một số kiến ​​thức quý giá để nắm bắt sâu hơn các đối số dòng lệnh Python

Loại bỏ các quảng cáo

Hai tiện ích từ thế giới Unix

Để sử dụng đối số dòng lệnh Python trong hướng dẫn này, bạn sẽ triển khai một số tính năng của hai tiện ích từ hệ sinh thái Unix

  1. sha1sum
  2. tiếp theo

Bạn sẽ làm quen với các công cụ Unix này trong các phần sau

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32 tính toán hàm băm SHA-1 và nó thường được sử dụng để xác minh tính toàn vẹn của tệp. Đối với một đầu vào nhất định, a luôn trả về cùng một giá trị. Mọi thay đổi nhỏ trong đầu vào sẽ dẫn đến một giá trị băm khác. Trước khi bạn sử dụng tiện ích với các thông số cụ thể, bạn có thể thử hiển thị phần trợ giúp

$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]

Hiển thị trợ giúp của chương trình dòng lệnh là một tính năng phổ biến được hiển thị trong giao diện dòng lệnh

Để tính giá trị băm SHA-1 của nội dung file, bạn tiến hành như sau

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
0

Kết quả hiển thị giá trị băm SHA-1 là trường đầu tiên và tên của tệp là trường thứ hai. Lệnh có thể lấy nhiều hơn một tệp làm đối số

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
1

Nhờ tính năng mở rộng ký tự đại diện của thiết bị đầu cuối Unix, nó cũng có thể cung cấp các đối số dòng lệnh Python với các ký tự đại diện. Một ký tự như vậy là dấu hoa thị hoặc ngôi sao [

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
34]

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
2

Shell chuyển đổi

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
35 thành
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
10 và
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
15, là hai tệp khớp với mẫu
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
35 trong thư mục hiện tại và chuyển chúng tới
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32. Chương trình tính toán hàm băm SHA1 của từng tệp trong danh sách đối số. Bạn sẽ thấy rằng, trên Windows, hành vi sẽ khác. Windows không có phần mở rộng ký tự đại diện, vì vậy chương trình có thể phải đáp ứng cho điều đó. Việc triển khai của bạn có thể cần mở rộng ký tự đại diện trong nội bộ

Không có bất kỳ đối số nào,

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32 đọc từ đầu vào tiêu chuẩn. Bạn có thể cung cấp dữ liệu cho chương trình bằng cách gõ các ký tự trên bàn phím. Đầu vào có thể kết hợp bất kỳ ký tự nào, kể cả dấu xuống dòng Enter . Để kết thúc đầu vào, bạn phải báo hiệu kết thúc tệp bằng Enter , theo sau là trình tự Ctrl +D:

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
3

Trước tiên, bạn nhập tên của chương trình,

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32, tiếp theo là Enter , sau đó là
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
42 và
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
28, mỗi tên cũng được theo sau bởi Enter. To close the input stream, you type Ctrl + D . Kết quả là giá trị của hàm băm SHA1 được tạo cho văn bản
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
44. Tên của tệp là
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
45. Đây là một quy ước để chỉ ra đầu vào tiêu chuẩn. Giá trị băm giống nhau khi bạn thực hiện các lệnh sau.

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
4

Tiếp theo, bạn sẽ đọc một đoạn mô tả ngắn về

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46

Loại bỏ các quảng cáo

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46 tạo ra một dãy số. Ở dạng cơ bản nhất, như tạo chuỗi từ 1 đến 5, bạn có thể thực hiện như sau

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
5

Để có cái nhìn tổng quan về các khả năng mà

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46 đưa ra, bạn có thể hiển thị trợ giúp tại dòng lệnh

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
6

Đối với hướng dẫn này, bạn sẽ viết một vài biến thể đơn giản hóa của

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32 và
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46. Trong mỗi ví dụ, bạn sẽ tìm hiểu một khía cạnh hoặc sự kết hợp các tính năng khác nhau về đối số dòng lệnh Python

Trên Mac OS và Linux,

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32 và
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46 phải được cài đặt sẵn, mặc dù các tính năng và thông tin trợ giúp đôi khi có thể khác một chút giữa các hệ thống hoặc bản phân phối. Nếu bạn đang sử dụng Windows 10, thì phương pháp thuận tiện nhất là chạy
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
32 và
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
46 trong môi trường Linux được cài đặt trên WSL. Nếu bạn không có quyền truy cập vào thiết bị đầu cuối hiển thị các tiện ích Unix tiêu chuẩn, thì bạn có thể có quyền truy cập vào thiết bị đầu cuối trực tuyến

  • Tạo một tài khoản miễn phí trên PythonAnywhere và khởi động Bash Console
  • Tạo một thiết bị đầu cuối Bash tạm thời khi thay thế. nó

Đây là hai ví dụ và bạn có thể tìm thấy những ví dụ khác

Mảng
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18

Trước khi khám phá một số quy ước được chấp nhận và khám phá cách xử lý các đối số dòng lệnh Python, bạn cần biết rằng hỗ trợ cơ bản cho tất cả các đối số dòng lệnh Python được cung cấp bởi. Các ví dụ trong các phần sau đây cho bạn thấy cách xử lý các đối số dòng lệnh Python được lưu trữ trong

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 và cách khắc phục các sự cố điển hình xảy ra khi bạn cố gắng truy cập chúng. Bạn sẽ học

  • Cách truy cập nội dung của
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18
  • Làm thế nào để giảm thiểu tác dụng phụ của bản chất toàn cầu của
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18
  • Cách xử lý khoảng trắng trong đối số dòng lệnh Python
  • Cách xử lý lỗi khi truy cập đối số dòng lệnh Python
  • Cách nhập định dạng ban đầu của các đối số dòng lệnh Python được truyền theo byte

Bắt đầu nào

Hiển thị đối số

Mô-đun

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
61 hiển thị một mảng có tên
$ sha1sum --help
Usage: sha1sum [OPTION].. [FILE]...
Print or check SHA1 [160-bit] checksums.

With no FILE, or when FILE is -, read standard input.

  -b, --binary         read in binary mode
  -c, --check          read SHA1 sums from the FILEs and check them
      --tag            create a BSD-style checksum
  -t, --text           read in text mode [default]
  -z, --zero           end each output line with NUL, not newline,
                       and disable file name escaping
[ .. complete help text not shown .. ]
9 bao gồm các phần sau

  1. $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    63 chứa tên của chương trình Python hiện tại
  2. $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    64, 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 truyền cho chương trình

Ví dụ sau minh họa nội dung của

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
7

Đây là cách mã này hoạt động

  • Dòng 2 nhập mô-đun Python nội bộ
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    61
  • Dòng 4 trích xuất tên của chương trình bằng cách truy cập phần tử đầu tiên của danh sách
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18
  • Dòng 5 hiển thị các đối số dòng lệnh Python bằng cách tìm nạp tất cả các phần tử còn lại của danh sách
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18

Ghi chú. Cú pháp chuỗi f được sử dụng trong

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
69 tận dụng trình xác định gỡ lỗi mới trong Python 3. 8. Để đọc thêm về tính năng chuỗi f mới này và các tính năng khác, hãy xem Các tính năng mới thú vị trong Python 3. 8

Nếu phiên bản Python của bạn nhỏ hơn 3. 8, sau đó chỉ cần xóa dấu bằng [______470] trong cả hai chuỗi f để cho phép chương trình thực thi thành công. Đầu ra sẽ chỉ hiển thị giá trị của các biến, không hiển thị tên của chúng

Thực thi đoạn script

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
69 ở trên với danh sách các đối số tùy ý như sau

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
8

Đầu ra xác nhận rằng nội dung của

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
72 là tập lệnh Python
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
69 và các phần tử còn lại của danh sách
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 chứa các đối số của tập lệnh,
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
75

Tóm lại,

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 chứa tất cả các đối số dòng lệnh của
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
69 Python. Khi trình thông dịch Python thực thi một chương trình Python, nó sẽ phân tích cú pháp dòng lệnh và điền vào
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18 các đối số

Loại bỏ các quảng cáo

Đảo ngược đối số đầu tiên

Bây giờ bạn đã có đủ thông tin cơ bản về

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
18, bạn sẽ thao tác trên các đối số được truyền tại dòng lệnh. Ví dụ
$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
80 đảo ngược đối số đầu tiên được truyền tại dòng lệnh

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
9

Trong

$ python -h
usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
Options and arguments [and corresponding environment variables]:
-b     : issue warnings about str[bytes_instance], str[bytearray_instance]
         and comparing bytes/bytearray with str. [-bb: issue errors]
[ .. complete help text not shown .. ]
80, quá trình đảo ngược đối số đầu tiên được thực hiện theo các bước sau

  • Dòng 5 lấy đối số đầu tiên của chương trình được lưu trữ tại chỉ mục
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    82 của
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18. Hãy nhớ rằng tên chương trình được lưu trữ tại chỉ mục
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    84 của
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    18
  • Dòng 6 in chuỗi đảo ngược.
    $ python -h
    usage: python3 [option] .. [-c cmd | -m mod | file | -] [arg] ...
    Options and arguments [and corresponding environment variables]:
    -b     : issue warnings about str[bytes_instance], str[bytearray_instance]
             and comparing bytes/bytearray with str. [-bb: issue errors]
    [ .. complete help text not shown .. ]
    
    86 là một cách Pythonic để sử dụng thao tác lát để

Bạn thực thi đoạn script như sau

 1// main.c
 2#include 
 3
 4int main[int argc, char *argv[]] {
 5    printf["Arguments count: %d\n", argc];
 6    for [int i = 0; i 

Chủ Đề