如何在Python 3中传递命令行参数

问题描述 投票:0回答:1

如何正确传递命令行参数来为变量分配数字。下面是我创建的一个简单的密码生成器,但是在通过终端传递参数时却无法使它们起作用:

#!/usr/bin/env python3
# This script creates a secure password using all available key combinations.

# System arguments aka command line arguments

import secrets , string, os, sys
from sys import argv

chars = string.ascii_letters+string.punctuation+string.digits

argv = one, two

print()
#pwd_length = int(input('Enter the length of the desired password: '))
pwd_length = two
print()
print('[+] ' + 'Your secure password is:')
print()

for n in range(1):
    output = ""
    for i in range(pwd_length):
        next_index = secrets.SystemRandom().randrange(len(chars))
        output = output + chars[next_index]
    print(output)
print()
python-3.x arguments parameter-passing command-line-arguments
1个回答
0
投票

[如果有人想知道如何进行命令行辩论,那么结果很简单,这是解决方案:

#!/usr/bin/env python3
# This script creates a secure password using all available key combinations.

import secrets , string, os, sys
chars = string.ascii_letters+string.punctuation+string.digits
pwd_length = int(sys.argv[1])

print("\n",'[+] ' + 'Your secure password is:',"\n")

for n in range(1):
    output = ""
    for i in range(pwd_length):
        next_index = secrets.SystemRandom().randrange(len(chars))
        output = output + chars[next_index]
    print(output,"\n")
© www.soinside.com 2019 - 2024. All rights reserved.