Cách sử dụng sys.argv trong python.
sys.argv là môt danh sách [list] trong python, nó được sư dụng khi bạn chạy một lệnh command-line nào đó trên hệ thống.
Và argument này được đẩy vào script python để thực thi khi chạy câu lệnh.
Ví dụ: python sys.argv arg1 arg2
Trước tiên bạn phải import mô đun sys trong script.
import sys print "This is the name of the script: ", sys.argv[0] print "Number of arguments: ", len(sys.argv) print "The arguments are: " , str(sys.argv)
Tên của script này : sysargv.py
Số lượng arg là : 1
Arg là : ['sysargv.py']
python test1020.py 111
This is the name of the script: test1020.py
Number of arguments: 2
The arguments are: ['test1020.py', '111']
Nhận xét
Đăng nhận xét