prvrlx
prvrlx

Reputation: 101

how to make file path as a global variable in python?

I've been getting this error NameError: name 'filep' is not defined i need to make filep as a file path. but every time i run my code i always get this error.

i need to make the filep as a module variable instead of a parameter as well as menulist.

import csv

filep= # filepath
menulist = [] #global scope


def menu_List():

    global menulist 
    menulist = []  # store items
    try:
        with open(filep) as f:  # read file
            reader = f.readlines()
            next(reader, None) #skip the header

            for row in reader:
                row[2] = int(row[2].strip()) #convert string to int
                row[1] = float(row[1].strip()) #convert string to float
                if row[2] > 100 and row[2] < 200:
                    menulist.append(row)
    except NameError:
        raise ValueError("Variable not set")

    menulist.sort(key=lambda x: x[-1])

menu_List()

Upvotes: 0

Views: 3132

Answers (2)

okie
okie

Reputation: 881

The answer above (which use args for fliep) is the best solution, but if you determined to not use args:

filep= 'file path you want'# filepath
menulist = [] #global scope


def menu_List():
    global filep #just add one more global will make it work
    global menulist 
    menulist = []  # store items
    try:
        with open(filep) as f:  # read file
            reader = f.readlines()
            next(reader, None) #skip the header

            for row in reader:
                row[2] = int(row[2].strip()) #convert string to int
                row[1] = float(row[1].strip()) #convert string to float
                if row[2] > 100 and row[2] < 200:
                    menulist.append(row)
    except NameError:
        raise ValueError("Variable not set")

    menulist.sort(key=lambda x: x[-1])

menu_List()

Extra little tip: Try prevent using global var if you can, global var slow down program's speed and eats memory, also sometime causing messy var naming.

Upvotes: 2

Guimoute
Guimoute

Reputation: 4629

You do not need global variables here, your function should accept the path as an argument and return the menu list.

import csv

def menu_List(filep):

    menulist = []  # store items
    try:
        with open(filep) as f:  # read file
            reader = f.readlines()
            next(reader, None) #skip the header

            for row in reader:
                row[2] = int(row[2].strip()) #convert string to int
                row[1] = float(row[1].strip()) #convert string to float
                if row[2] > 100 and row[2] < 200:
                    menulist.append(row)
    except NameError:
        raise ValueError("Variable not set")

    menulist.sort(key=lambda x: x[-1])
    return menulist

menulist = menu_List("a/path/goes/here")

Unrelated to your question, you can skip the header like you did or like this:

reader = f.readlines()
for row in reader[1:]: # skip the first line.
    ...

Upvotes: 3

Related Questions