如何在 Python 3 中传递命令行参数
Posted
技术标签:
【中文标题】如何在 Python 3 中传递命令行参数【英文标题】:How to pass Command Line Arguments in Python 3 【发布时间】:2020-08-27 17:08:33 【问题描述】:如何正确传递命令行参数以将数字分配给变量。下面是我创建的一个简单的密码生成器,但在通过终端传递参数时无法使它们起作用:
#!/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()
【问题讨论】:
【参考方案1】:如果有人想知道如何进行命令行争论,结果很简单,这是解决方案:
#!/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")
【讨论】:
以上是关于如何在 Python 3 中传递命令行参数的主要内容,如果未能解决你的问题,请参考以下文章