如何使用变量在python中使用net user添加全名

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

我一直在用 python 编写一个脚本来创建带有输入的新用户。我专注于这一行,并尝试获取最后 2 个变量进行描述和全名以进行继承。

subprocess.run(["net", "user", user, password, "/add", "/comment: %description%", "/fullname: %full_name%"], check=True)

正如所写,它创建了具有正确用户名和密码的用户,但描述(又名注释)为 %description%,全名为 %full_name%。这些都是我的程序代码中已经定义的变量,并且工作正常。

我一直在尝试各种事情,我最近才发现我应该在添加命令后添加注释和全名。我只想将这两个变量的值传递给 comment 和 fullname 命令。

这是完整的代码,以防有帮助

import subprocess, os, sys
from easygui import passwordbox
print("""Hello, I am C-3PO. I understant you'd like to add some new users to your system today
I'll be happy to assist you with this task.""")

Count = int(input("How many users are we creating today?"))

Total = 0

GroupName = input("""We want to be sure all of the new users can find each other; comradery is so important you know.
Let’s create a group for them. What would you like the group to be called?""")
subprocess.run(["net", "localgroup", GroupName, "/add"], check=True)

total = 0

while total != Count:
    first_name = input("What is the first name of the user you'd like to create? ")
    middle_name = input("What is their middle name, if they don't have one kindly press enter. ")
    last_name = input("Also, may I know their last name? ")
    username = (first_name[0:1] + last_name)
    full_name = f"{first_name} {middle_name} {last_name}"
    password = passwordbox("What password would you like to give " + full_name + "? ")
    description = input("Would you like to tell me a little about " + full_name + "? If not simply press enter. ")
    user=username
    group = GroupName

    subprocess.run(["net", "user", user, password, "/add", "/comment: %description%", "/fullname: %full_name%"], check=True)
#    subprocess.run(["net", "user", user, "/Comment:"description, "/fullname:", full_name, "/add"], check=True)
    subprocess.run(["net", "localgroup", GroupName, user,"/add"], check=True)
    total += 1

print("\nIt's been a pleasure in helping you add these {count} users today.")
print("I do hope you'll inform Master James of my performance.")

忽略注释掉的行,因为它将被删除,我只是用它来保存变量名称。

python python-3.x subprocess
1个回答
0
投票

您应该使用 F 弦。您现在正在尝试使用 %,但您应该使用以下内容:

f"/comment: {description}", f"/fullname: {full_name}"

顺便说一句,如果您需要命令中的“%”,请使用以下命令:

f"/comment: %{description}%", f"/fullname: %{full_name}%"
© www.soinside.com 2019 - 2024. All rights reserved.