I am bit stuck with this where i need to return value if file found
if not found will throw error Index out of list
will need to catch and return the value assigned a var variable in exception and return that variable value
Error :
C:\Users\admin\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/admin/PycharmProjects/pythonProject3/search.py
File "C:\Users\admin\PycharmProjects\pythonProject3\search.py", line 13
return var
^
SyntaxError: 'return' outside function
My code :
import os
import glob
pathname='C:\\Users\\admin\\Desktop\\JUL\\'
if os.path.exists(pathname):
var=glob.glob('{0}/log.*'.format(pathname))
try :
fnme=var[0]
head , tail = os.path.split(fnme)
var=tail
return var
except IndexError as Ier:
var='abc.txt'
return var
Expected Output:
if file is found then return var => log.txt
if does not return any value from fnme=var[0]
then it leads to Index Out of List , where in that case return var => abc.txt