[英]Python Windows cmd command format
So I'm trying to make a simple python script to create a user in Windows but I'm having issues getting Python to format the command in cmd. 因此,我试图制作一个简单的Python脚本在Windows中创建用户,但在让Python在cmd中格式化命令时遇到了问题。 Let's say the user is test and the password is 12345 the output with the code I have below would be net usertest12345 /add instead of net user test 12345 /add. 假设用户是测试用户,密码为12345,则我下面的代码输出为net usertest12345 / add而不是net user test 12345 / add。 Any help would be appreciated! 任何帮助,将不胜感激!
import getpass, os
user = input("Please enter a username: ")
password = getpass.getpass()
add = ' net user'
add2 = ' /add'
add3 = add + user + password + add2
os.popen(add3)
Or even better, use python string formatting: 甚至更好,请使用python字符串格式:
import getpass, os
user = input("Please enter a username: ")
password = getpass.getpass()
addUser = "net user {user} {pw} /add".format(user=user, pw=password)
print(addUser)
Resulting in the following output: 结果如下:
Please enter a username: test
Warning: Password input may be echoed.
Password: 12345
net user test 12345 /add
That being said, you probably also want to handle exceptions ... 话虽如此,您可能还想处理异常...
Just add some spaces in between? 只是在两者之间添加一些空格?
add3 = add + ' ' + user + ' ' + password + add2
print(add3) # --> net user test 12345 /add
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.