2016-04-29 88 views
0

(在單擊「複製」,沒有解決方案上任何其他的工作)NameError:全局名稱「的FileInput」沒有定義

我在Python 2.7.11工作,試圖轉換我的代碼來自Python 3.5。除了這個惱人的問題之外,我設法刪除了所有的語法錯誤。

NameError: global name 'fileinput' is not defined

這裏是我的代碼(全部):

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() 

(在你想知道,我轉換成能夠使用py2exe情況下,我無法與PyInstaller)

+2

你嘗試'進口fileinput',而不是'從進口的FileInput *'(這是無論如何勸阻)?然後它應該是'fileinput.FileInput('Schedule.txt',inplace = 1)' –

+0

只要'fileinput(「Schedule.txt」,inplace = 1)'而不是'fileinput.fileinput(...) 。 –

回答

1

當您使用語句

from <modulename> import <names> 

它不綁定th e模塊名稱,它只是綁定作爲本地名稱導入的各個名稱。所以你應該撥打fileinput(),而不是fileinput.fileinput()

如果要模塊名綁定,你應該使用

import fileinput 

`from ... import` vs `import .`

+0

我得到一個TypeError:'模塊'對象不可調用 – Robski

+0

固定。我在上面使用了一條評論,並且在我之前做了一個嘗試之後,它就起作用了。 – Robski

+0

如果您執行'import fileinput',然後嘗試調用'fileinput()'而不是'fileinput.fileinput()',您將會得到該錯誤。你瞭解綁定模塊名稱和函數名稱的區別嗎? – Barmar

相關問題