Hello I am new to python...I met with this simple problem

def search(path,filename,extension):
 name=' '
 listdir=os.listdir(path)
 print listdir
 flname=filename+'.'+extension
 i=0
 match=''
 while(i<len(listdir)):
  if(listdir[i]==flname):
   match='YES'
   print 'got the match'
   break
  i=i+1
 if(match=='YES'):
   assign(filename)
 else:
   name = filename+'.'+extension
   print 'the filename is',name
   return name


lets_text=search(path,filename,extension)
print 'the name we gt is',lets_text

The above problem has to return me a valid string. but instead of that it return None...I dont know why?...I am getting correct value for variable "name"....but when it returns it come up with None

def search(path,filename,extension):
    name=' '
    listdir=os.listdir(path)
    print listdir
    flname="".join([filename, '.', extension])
    i=0
    match=False
    for i in listdir:
        if i==flname:
            match=True
            print 'Got the match!'
            break
    if match:
        assign(filename)  #Not sure what this is but I will leave it.
    else:
        name = "".join([filename, '.', extension])
        print 'The filename is %s.' % name
        return name

lets_text = search(path,filename,extension)
print 'The name we got is %s.' % lets_text

This works for me.
String.join() is faster then adding onto a string.

The code provided by redyugi wont work too...

Your problem must be in the assign() function.

here is the complete code...

def assign(filename):
 int(counter+1)
 print 'the value of counter is',counter
 filename=filename+str(counter)
 int(counter)
 print 'sending',filename
 search(path,filename,extension)


def search(path,filename,extension):
 name=' '
 name1=''
 listdir=os.listdir(path)
 print listdir
 flname="".join([filename, '.', extension])
 i=0
 match=False
 for i in listdir:
  if i==flname:
   match=True
   print 'Got the match!'
   break
 if match:
  assign(filename) #Not sure what this is but I will leave it.
 else:
  name = "".join([filename, '.', extension])
  print 'The filename is %s.' % name
 return 'name'

data=search(path,filename,extension)
print 'the file name is',data
def assign(filename, counter, path, extension):
 print 'the value of counter is %s.'  % int(counter)+1
 filename="".join([filename, str(counter)])
 print 'Sending %s.' % filename
 search(path,filename,extension, counter)


def search(path,filename,extension, counter):
 name=' '
 listdir=os.listdir(path)
 print listdir
 flname="".join([filename, '.', extension])
 i=0
 match=False
 for i in listdir:
  if i==flname:
   match=True
   print 'Got the match!'
   break
 if match:
  assign(filename, counter, path, extension)
 else:
  name = "".join([filename, '.', extension])
  print 'The filename is %s.' % name
 return name

data=search(path,filename,extension)
print 'the file name is',data

You were missing a variable in your function. You need to learn how to pass variable's to other functions.
This should work in theory.

You might want to re-think/modify the following.

if match:
    assign(filename, counter, path, extension)
    counter += 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.