Python 名称错误:全局名称';文件输入';没有定义
(在单击“复制”之前,其他任何解决方案均无效) 我正在使用Python2.7.11,试图从Python3.5转换代码。我设法消除了所有的语法错误,除了这个恼人的错误 NameError:未定义全局名称“fileinput” 这是我的代码(全部):Python 名称错误:全局名称';文件输入';没有定义,python,python-2.7,file-io,global,Python,Python 2.7,File Io,Global,(在单击“复制”之前,其他任何解决方案均无效) 我正在使用Python2.7.11,试图从Python3.5转换代码。我设法消除了所有的语法错误,除了这个恼人的错误 NameError:未定义全局名称“fileinput” 这是我的代码(全部): from fileinput import * from sys import * callingstops=[] def Main_Menu (): print'--------------------------------------
from fileinput import *
from sys import *
callingstops=[]
def Main_Menu ():
print'----------------------------------------------------------------------------------------------------------------------------'
print'RAILWAY SCHEDULE AND DISPATCHING SYSTEM V1.1 - PROVIDED BY ROBSKI'
print'\n'
print'Please choose one of the options:'
print'1) Check Schedule'
print'2) Add schedule'
print'3) Enter Delays Report'
print'4) Delete Schedule'
print''
print'0) EXIT'
Option = int(raw_input("==> "))
if Option == 1:
checkschedule()
if Option == 2:
addschedule()
if Option == 3:
delays()
if Option == 4:
delete()
else:
sys.exit
def checkschedule ():
file = open("Schedule.txt", "r")
Scheduledetails = file.read()
file.close()
print'--------------------------------------------------------------------------------------------------------------------------------'
print(Scheduledetails)
Main_Menu()
def addschedule ():
user1 = raw_input('Enter time in xxxx format: ')
dest = raw_input('Enter Destination of train: ')
op = raw_input('Enter NR Operator Abbreviaton: ')
long = raw_input('Enter number of carriages: ')
notes = raw_input('Remarks (Running days etc.): ')
callingno = int(raw_input('How much calling points are there? (NOT CHANGEABLE): '))
for i in range(0,callingno):
callingstops=[]
callingstops.append(raw_input('Enter the name of stop '+str(i+1)+' (If your stop is a request, put "(r)" at the end): '))
file = open("Schedule.txt", "a")
file.write ("\n\n | Time: "+user1+"\n | Destination: "+dest+"\n | Operator Abbreviation: "+op+"\n | No. of carriages: "+long+"\n | Remarks: "+notes)
for item in callingstops:
file.write("\n | Calling at: "+item)
file.close()
Main_Menu()
def delays ():
delaytitle = raw_input("Enter title of delay (Will be saved in seperate doc, overrides any other file with same name): ")
delaydesc = raw_input("Enter description of delay in detail: ")
file = open(delaytitle+".txt", "w")
file.write("Delay report:\n"+delaydesc)
file.close()
def delete ():
train = raw_input("Enter the train time that you want to remove (You MUST have the file closed): ")
train = train.strip()
train_found_on = None # Rememberes which line the train time is located
for i, line in enumerate(fileinput.fileinput('Schedule.txt', inplace=1)): # Open the file for in-place writing
if 'Time:' in line and train in line: # Don't transfer the lines which match input
train_found_on = i
continue
if train_found_on and i < (train_found_on + 7): # Neither the lines after that
continue
sys.stdout.write(line) # But write the others
Main_Menu()
Main_Menu()
从文件输入导入*
从系统导入*
呼叫停止=[]
def主菜单():
打印“----------------------------------------------------------------------------------------------------------------”
打印“铁路时刻表和调度系统V1.1-由ROBSKI提供”
打印“\n”
打印“请选择一个选项:”
打印“1)检查计划”
打印“2)添加计划”
打印“3)输入延迟报告”
打印“4)删除计划”
打印“
打印“0)退出”
选项=int(原始输入(“=>”)
如果选项==1:
核对附表()
如果选项==2:
addschedule()
如果选项==3:
延迟()
如果选项==4:
删除()
其他:
系统出口
def checkschedule():
文件=打开(“Schedule.txt”、“r”)
Scheduledetails=file.read()
file.close()文件
打印“----------------------------------------------------------------------------------------------------------------”
打印(时间表详细信息)
主菜单()
def addschedule():
user1=原始输入('以xxxx格式输入时间:')
dest=原始输入(“输入列车目的地:”)
op=原始输入('输入NR运算符缩写:')
long=原始输入(“输入车厢数量:”)
备注=原始输入('备注(运行天数等):')
callingno=int(原始输入('有多少呼叫点?(不可更改):'))
对于范围内的i(0,调用编号):
呼叫停止=[]
callingstops.append(原始输入('输入stop'+str(i+1)+'(如果您的stop是一个请求,请在末尾加上“(r)”))
文件=打开(“Schedule.txt”、“a”)
file.write(“\n\n | Time:+user1+”\n | dest:+dest+”\n |运算符缩写:“+op+”\n |车厢号:“+long+”\n |备注:+notes)
对于callingstops中的项目:
file.write(“\n|调用:”+项)
file.close()文件
主菜单()
def延迟():
delaytitle=原始输入(“输入延迟的标题(将保存在单独的文档中,覆盖具有相同名称的任何其他文件):”)
delaydesc=原始输入(“详细输入延迟说明:”)
文件=打开(delaytitle+“.txt”,“w”)
write(“延迟报告:\n”+delaydesc)
file.close()文件
def delete():
train=原始输入(“输入要删除的列车时间(必须关闭文件):”)
火车=train.strip()
列车运行时间=无#记住列车运行时间所在的线路
对于i,枚举中的行(fileinput.fileinput('Schedule.txt',inplace=1)):#打开文件进行就地写入
如果“时间:”在线路上,列车在线路上:#不要传输与输入匹配的线路
列车运行时间=i
持续
如果train_found_on和i<(train_found_on+7):#之后的两行都没有
持续
sys.stdout.write(line)#但是写其他的
主菜单()
主菜单()
(如果您想知道,我正在转换为能够使用py2exe。我使用PyInstaller失败)当您使用该语句时
from <modulename> import <names>
使用该语句时,请参见
from <modulename> import <names>
请参见您是否尝试从fileinput导入而不是从fileinput导入(无论如何都不鼓励这样做)导入fileinput?然后它应该是
fileinput.fileinput('Schedule.txt',inplace=1)
只要做fileinput('Schedule.txt',inplace=1)
而不是fileinput.fileinput(…)
。你试过从fileinput导入*导入文件输入吗?然后它应该是fileinput.fileinput('Schedule.txt',inplace=1)
只需执行fileinput('Schedule.txt',inplace=1)
而不是fileinput.fileinput(…)
。我得到一个类型错误:“module”对象未被调用。我在上面使用了一条注释,在我之前的一次尝试后,它起了作用。如果您执行import fileinput
,然后尝试调用fileinput()
,而不是fileinput.fileinput()
,就会出现错误。您了解绑定模块名和函数名之间的区别吗?我得到一个TypeError:“module”对象不是callableFIXED。我在上面使用了一条注释,在我之前的一次尝试后,它起了作用。如果您执行import fileinput
,然后尝试调用fileinput()
,而不是fileinput.fileinput()
,就会出现错误。您了解绑定模块名和函数名之间的区别吗?