如何输入固定的用户名和密码,而不需要python中的输入字段?

如何输入固定的用户名和密码,而不需要python中的输入字段?,python,input,passwords,Python,Input,Passwords,我有一个python脚本,提示输入用户名和密码。这很好,但我想知道我是否可以在文件中预设一个固定的用户名和密码。有没有办法轻松地将下面的密码提示编辑为用户名和密码的固定条目? username = input("Enter the username: ") password = input("Enter the password: ") 我认为最好的方法听起来像是函数的默认参数。比如: def log_me_in(username=None, password=None):

我有一个python脚本,提示输入用户名和密码。这很好,但我想知道我是否可以在文件中预设一个固定的用户名和密码。有没有办法轻松地将下面的密码提示编辑为用户名和密码的固定条目?

    username = input("Enter the username: ")
    password = input("Enter the password: ")

我认为最好的方法听起来像是函数的默认参数。比如:

def log_me_in(username=None, password=None):
    if username is None:
        username = input("Enter the username:\n>>> ")
    if password is None:
        password = input("Enter the password:\n>>> ")
    print("Logging in user {} with password {}".format(username, password))
这允许您灵活地运行
登录(“David”,“$omething$ecret”)
,而不必每次都输入这些参数。或者,您可以省略这些参数,并使用默认的
None
值(强制您在程序执行期间键入输入)

示例:

登录()

登录(“Dave”)

登录(“Dave”,“密码”)

登录(password=“A secret”)


您只需硬编码
username=“foo”
password=“keepthiscoret”
。不需要
input()
。如果它在python文件中应用,那么关于它的常见警告实际上并不是秘密。Username=“David Boykin”如果这是脚本想要的唯一输入,另一个选项是创建一个文件,将用户名和密码作为前两行,并在命令行中导入
myscript.py
@tdelaney-谢谢!这很有效。
Enter the username:
>>>Dave
Enter the password:
>>>pa$$word
Logging in user Dave with password pa$$word
Enter the password:
>>> My Password
Logging in user Dave with password My Password
Logging in user Dave with password Password
Enter the username:
>>> Dave
Logging in user Dave with password A secret