0

I keep getting the following error

g:\python_test>py3 lib\libtest.py p = g:\python_test\SqlLogon.txt Traceback (most recent call last): File "lib\libtest.py", line 43, in

i.printit('c:\mylog.log')

File "lib\libtest.py", line 32, in printit

s = wos()

NameError: name 'wos' is not defined

If i remove the call to os and let s = 'WINDOWS' everything works fine. Why can I not call that function?? following is the code giving the issue!

import sys
import platform

import os

import subprocess
class utl:

    def __init__(self,path):
        self.path = path
        print ('p = ' + self.path)

    def usr(self):

        lc = open(self.path,'r')

        up = lc.readlines()
        u = up[0]
        u = u.strip('\n')
        p = up[1]
        p = p.strip('\n')

        return u,p

    def wos(self):
        p = platform.system()        
        print ('type = ' + str(type(p)))

        return p.upper()

    def printit(self,filetoprt):
        s = wos()   # undefined wos
        #s = "WINDOWS"
        print ('os1 = ' + s  + ' ' + filetoprt)
        if s == ('WINDOWS'):
            os.startfile(filetoprt,'print')

#x = LogonSql.usr('g:\python_test\lib\jcd.txt')
i= utl('g:\python_test\SqlLogon.txt')

p = i.usr()
#t = i.wos()
i.printit('c:\mylog.log')
  • 1
    Possible duplicate of [Python call function within class](https://stackoverflow.com/questions/5615648/python-call-function-within-class) – hlg Mar 14 '19 at 02:37

2 Answers2

0

wos is your class member, you should call it with

self.wos()
Lena Oxton
  • 96
  • 5
  • Thanks that worked!! I am just trying to learn Python and am wondering if anybody has some tips for how best to cope with the indenting in python. Using uedit32??? – user3158464 Mar 14 '19 at 06:38
  • uedit32 is quite out-dated, try some modernized code editors, like sublime, atom,MS code, etc.. – Lena Oxton Mar 14 '19 at 06:59
0

wos() is a method bound to instances of your utl class, so you need to refer to them as such.

Replace s = wos() with s = self.wos(). Otherwise, your code tries to access a function wos() on the global scope (outside of the class), which you haven't defined.

jfaccioni
  • 7,099
  • 1
  • 9
  • 25