(Sep-23-2019, 01:06 AM)ichabod801 Wrote: First of all, that is not the correct code or you would be getting an indentation error. Second, the if __name__ goes at the end of the file, and is only for code that should not be run when the file is imported. If you stick it in the middle of a function, the function won't do anything when it is imported.
Sorry about the late reply but Ive been a way for the last 2 weeks.
When I pasted the previous code it didnt display the way I wanted. The actual code I used is below:
import os import globalVariables as gv def listCWD(): if __name__ == '__main__': try: gv.directoryList = os.listdir(gv.directoryIn) print('directoryList =\n',gv.directoryList) except FileNotFoundError: print('FileNotFoundError:\na List for the Directory gv.directoryIn cannnot be created\nbecause the Global Variable gv.directoryIn is an empty string.\n' 'To correct this either:\na)run the returnCWD function in this Module to assign a directory to gv.directoryIn or\nb)open the globalVariables.py file and setgv.directoryIn to = None') except TypeError: print('TypeError:\na List for the Directory gv.directoryIn cannnot be created\nbecause the Global Variable gv.directoryIn has been assigned to equal either:\n' 'a)List\nb)Set\nc)Tuple\n' 'To correct this either:\na)run the returnCWD function in this M odule to assign a directory to gv.directoryIn or\nb)open the globalVariables.py file and setgv.directoryIn to = None') except NameError: print('NameError:\nbecause of the Syntax Error on import a NameError now occurs in the functions code\n' 'To correct the SyntaxError above open the globalVariables.py file and do either of the solutions below\n' 'a)make directoryIn = an empty string\nb)make directoryIn = None') listCWD()You say that the if main code should go at the end of the file but I dont have anything to put after the if main piece of code. The file listCWD.py only contains a listCWD function and if I dont use If main the function runs on Import and I do not want it to run on import.