2016-04-29 17 views
0

(Bevor Sie auf „Duplizieren“, keine der Lösungen auf alle anderen arbeiten)Nameerror: global name ‚fileinput‘ ist nicht definiert

ich in Python arbeite 2.7.11, zu konvertieren versuchen, Mein Code von Python 3.5. Es gelang mir, alle Syntaxfehler zu entfernen, abgesehen von diesem nervigen.

NameError: global name 'fileinput' is not defined

Hier ist mein Code (The TEILEN):

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

(. Falls Sie sich fragen, ich bin Umwandlung der Lage sein, py2exe verwenden ich mit PyInstaller nicht)

+2

Haben Sie versucht, 'Import fileinput' statt' von fileinput import * '(die ohnehin abgeraten)? Und dann sollte es 'fileinput.FileInput ('Schedule.txt', inplace = 1)' –

+0

Tun Sie einfach 'fileinput (" Schedule.txt ", inplace = 1)' anstelle von 'fileinput.fileinput (...)' . –

Antwort

1

Wenn Sie die Anweisung

from <modulename> import <names> 

Es ist nicht th nicht bindet Der Modulname bindet nur die einzelnen Namen, die Sie als lokale Namen importiert haben. Sie sollten also einfach fileinput() anrufen, nicht fileinput.fileinput().

Wenn Sie den Modulnamen binden möchten, sollten Sie

import fileinput 

verwenden Siehe `from ... import` vs `import .`

+0

Ich bekomme einen TypeError: 'Modul' Objekt ist nicht aufrufbar – Robski

+0

FIXED. Ich habe oben einen Kommentar verwendet, und es hat nach einem Versuch funktioniert, den ich vorher gemacht habe. – Robski

+0

Sie erhalten diesen Fehler, wenn Sie 'fileinput importieren 'und dann versuchen,' fileinput() 'anstelle von' fileinput.fileinput() 'aufzurufen. Verstehen Sie den Unterschied zwischen der Bindung des Modulnamens und des Funktionsnamens? – Barmar