-2

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

codeholic24
  • 955
  • 4
  • 22
  • 4
    The error says it all. You have a `return` outside of a function. That is meaningless and invalid. *returning* only has a meaning in the context of a function. If you are explicitly *asked* to return something, then this implicitly means you have to create a function. – trincot Jul 30 '21 at 13:57
  • 1
    It would help if you could explain what you expect `return var` to do. – BoarGules Jul 30 '21 at 13:59
  • Perhaps you want `print(var)` instead? `return` does not do what you seem to think it does. – chepner Jul 30 '21 at 14:00
  • 1
    Does this answer your question? [Python return statement error " 'return' outside function"](https://stackoverflow.com/questions/7842120/python-return-statement-error-return-outside-function) – trincot Jul 30 '21 at 14:00

3 Answers3

2

You can only return from a function, and you have no function there.

But, based on the code and description in your question, I would say the definition of "return" in your case is simply to print the value. That could be done with something like (after importing sys, of course):

print(var)
sys.exit()

However, you could also clean up the code a little as well, making it more concise:

import os
import glob

pathname='C:\\Users\\admin\\Desktop\\JUL\\'

if os.path.exists(pathname):
    var = glob.glob(f'{pathname}/log.*')
    try:
        _, name = os.path.split(var[0])
    except IndexError:
        name = 'abc.txt'

    print(name)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

The Python return word is used for returning a value from a function. You cannot use the return word if your code is not inside a function.

To make your code work, you could change it from this:

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

to this:

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
    except IndexError:
        var = 'abc.txt'

print(var) # will be 'abc.txt' if there is an index error
GreyHope
  • 102
  • 1
  • 11
0

Thanks for the solution , i was able to do it : @paxdiablo and @GreyHope

import os
import  glob

pathname='C:\\Users\\admin\\Desktop\\JUL\\'

def returnFile(pathname):
    if os.path.exists(pathname):
        var = glob.glob('{0}/abc.*'.format(pathname))
        try:
            fnme = var[0]
            head, tail = os.path.split(fnme)
            var = tail
        except IndexError as er:
            var='abc.txt'
    return var

getfile=returnFile(pathname)
print(getfile)
codeholic24
  • 955
  • 4
  • 22