【发布时间】:2023-03-29 03:17:01
【问题描述】:
我开始学习 python,我决定从我认为是一个简单的脚本开始,但我想不是。我要做的就是返回我在 Mac 上使用的当前网络位置。每次我运行它时,它都会返回正确的位置,但是当我尝试更改它时,什么也没有发生。我不知道我做错了什么,所以这是我的代码。我真的希望有人能帮助我。这个脚本是 MACS 独有的。我知道“scselect”是内置的,但我想看看它是否可以用 Python 完成。我知道下面的代码很粗糙,但随着我的学习,我会做得更好。我正在使用 Python 2.7。
import subprocess
loc = "scselect"
srn = "scselect SRN"
home = "scselect HOME"
a = subprocess.check_output(loc, shell=True)
print "\n##### NETWORK SELECTION #####"
print "\nYour current location is set to \n\n%s" % a
choice = raw_input("Please select what location you want to change to: ")
b = subprocess.Popen(srn, shell=True, stdout=subprocess.PIPE)
c = subprocess.Popen(home, shell=True, stdout=subprocess.PIPE)
choice = int(choice)
if choice == 1:
print "\nSwitching to HOME...\n"
c
elif choice == 2:
print "\nSwitching to SRN...\n"
b
else:
print "\nShutting down...\n"
【问题讨论】:
-
"[...] 但是当我尝试更改它时,什么也没有发生。"当您说什么都没有发生时,您的意思是在您输入您的选择后,您不会从脚本中得到任何输出?
-
@user2495187:我也不明白那句话。我认为您的意思是
subprocess.check_output按预期工作,但subprocess.Popen行似乎并没有改变网络。我说的对吗? -
@KyleG。这是我得到输出的奇怪部分: Switching to HOME... SCPreferencesCommitChanges() failed
标签: python macos networking python-2.7