0

(Before you click "Duplicate", none of the solutions on ANY of the others work)

I am working in Python 2.7.11, trying to convert my code from Python 3.5. I managed to remove all the syntax errors, apart from this annoying one.

NameError: global name 'fileinput' is not defined

Here is my code (The ENTIRETY):

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

(In case you are wondering, I am converting to be able to use py2exe. I failed with PyInstaller)

Robski
  • 19
  • 1
  • 4
  • 2
    Did you try `import fileinput` instead of `from fileinput import *` (which is discouraged anyway)? And then it should be `fileinput.FileInput('Schedule.txt', inplace=1)` – Byte Commander Apr 29 '16 at 20:40
  • Just do `fileinput("Schedule.txt", inplace=1)` instead of `fileinput.fileinput(...)`. – Sergei Lebedev Apr 29 '16 at 20:43

1 Answers1

3

When you use the statement

from <modulename> import <names>

It doesn't bind the module name, it just binds the individual names that you imported as local names. So you should just call fileinput(), not fileinput.fileinput().

If you want to bind the module name, you should use

import fileinput

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

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I get a TypeError: 'module' object is not callable – Robski Apr 30 '16 at 06:19
  • FIXED. I used a comment above, and it worked after a try that I did earlier. – Robski Apr 30 '16 at 06:22
  • You'll get that error if you do `import fileinput` and then try to call `fileinput()` instead of `fileinput.fileinput()`. Do you understand the difference between binding the module name and function name? – Barmar Apr 30 '16 at 18:15