Python脚本中的重定向错误

时间:2010-10-01 08:24:21

标签: python redirect

我有2个python脚本,分别是main_menu.py和inputip.py。

当我的功能在iputip.py中完成时按“enter”重定向到main_menu.py时出现问题。该脚本不允许我重定向到main_menu.py,而是在Windows命令提示符下显示此错误:

Traceback (most recent call last):
  File "C:\python\main_menu.py", line 32, in ?
    execfile('C:\python\Inputip.py')
  File "C:\python\Inputip.py", line 11, in ?
    input ("\nSelect enter to proceed back to Main Menu\n")
  File "<string>", line 0

^ SyntaxError:解析时意外的EOF

以下是我的代码(main_menu.py):

def menu():
#print what options you have
print "Welcome to Simple Network Program"
print " "
print "Please enter a following option to proceed"
print " "
print "2) View Personal IP Address"
print " "
return input ("Select an Option here: ")
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
    execfile('Inputip.py')
elif choice == 5:
    loop = 0
print "Thank you for using the Simple Network Program!"

代码(inputip.py):

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.
import socket
import os
print ("\n\n"+socket.gethostbyname(socket.gethostname()))
input ("\nSelect enter to proceed back to Main Menu\n")

execfile('C:\python\main_menu.py')

错误似乎指向execfile。关于代码的一些建议会很棒。谢谢!

2 个答案:

答案 0 :(得分:1)

除非您使用的是python 3.x(但您的问题未标记为此类),请不要使用输入。请使用raw_input。它将返回字符串,因此首先将它们转换为int,或者进行字符串比较。 E.g。

x = raw_input("Choice")
if x == '1': 
    do_this()

答案 1 :(得分:0)

我认为execfile()不是好习惯。请尝试使用'import'命令:

if choice == 1:
    from Inputip import some_func
    some_func()

当然,Inputip应该包含一些你可以导入的方法。