Python Forum

Full Version: How to append one function1 results to function2 results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have two functions and I want to append the results of function1 and function2.

#import difflib
import os
my_dir = 'D:\Backupdata\PythonCodes'
post_fix1 = ['\InputFiles\InputCSV1.csv','\InputFiles\InputCSV2.csv']
post_fix2 = ['\InputFiles\InputCSV1.csv','\InputFiles\InputCSV23.csv']

def myfun1(my_dir,post_fix1):
    result = ''
    for i in range (len(post_fix1)):
        #result = ''
        try:
            full_file = my_dir + str(post_fix1[i])
            #print(full_file)
            if (os.path.exists(full_file)) == True:
                print("File_existpostfix1")
                result = result + '0' + ','
                #print('file exist form postfix1')
            else:
                print('file does not exists')
                result = result + '1' +','
        except FileNotFoundError:
            result = result + '-1' + ','
            print('abormal')
    result = result.strip(',')
    print(result)
def my_fun2(my_dir,post_fix2):
    result1 = ''
    for i in range (len(post_fix1)):
        try:
            full_file = my_dir + str(post_fix2[i])
            if (os.path.exists(full_file)) == True:
                result1 = result1 + '0' + ','
                #print(full_file)
                print("File_existpostfix1",full_file)
            else:
                print('file does not exists2:', full_file)
                result1 = result1 + '1' + ','
        except FileNotFoundError:
            print('abormal2')
            result1 = result1 + '-1' + ','
    result1 = result1.strip(',')
    print(result1)
    
        
myfun1(my_dir,post_fix1)
my_fun2(my_dir,post_fix2)
#final_result = result1.append(result)
#
function1 results =[0,0]
function2 result = [0,1]
I want to final result = [0,0
0,1]
results1 =[0,0]
results2 = [0,1]
final = results1 + results2

print(final)
Output:
[0, 0, 0, 1]
but result1 & result2 are the outputs of function1 & function2.
There are no outputs of function1 and function2, except for the default output of None. You need to use the return statement to get something out of those functions, then you can append.

def foo():
    return [0, 1]
def bar():
    return 2
data = foo()
data.append(bar())
print(data)         # [0, 1, 2]
(Jan-01-2020, 11:31 PM)SriRajesh Wrote: [ -> ]but result1 & result2 are the outputs of function1 & function2.

Use a return statement in your function.

def function1(a, b, c):
    stuff
    stuff
    stuff
    return result

def function2(a, b, c):
    stuff
    stuff
    stuff
    return result

final = [funtion1(a, b, c), funtion2(a, b, c)]
#import difflib
import os
my_dir = 'D:\Backupdata\PythonCodes'
post_fix1 = ['\InputFiles\InputCSV1.csv','\InputFiles\InputCSV2.csv']
post_fix2 = ['\InputFiles\InputCSV1.csv','\InputFiles\InputCSV23.csv']
 
def myfun1(my_dir,post_fix1):
    result = ''                     <----- These are local var. Is accessible only inside this function. Thats why the var name can be the same.
    for i in range (len(post_fix1)):
        #result = ''
        try:
            full_file = my_dir + str(post_fix1[i])
            #print(full_file)
            if (os.path.exists(full_file)) == True:
                print("File_existpostfix1")
                result = result + '0' + ','
                #print('file exist form postfix1')
            else:
                print('file does not exists')
                result = result + '1' +','
        except FileNotFoundError:
            result = result + '-1' + ','
            print('abormal')
    result = result.strip(',')
    return result
def my_fun2(my_dir,post_fix2):
    result = ''                     <----- These are local var. Is accessible only inside this function. Thats why the var name can be the same.
    for i in range (len(post_fix1)):
        try:
            full_file = my_dir + str(post_fix2[i])
            if (os.path.exists(full_file)) == True:
                result = result + '0' + ','
                #print(full_file)
                print("File_existpostfix1",full_file)
            else:
                print('file does not exists2:', full_file)
                result = result + '1' + ','
        except FileNotFoundError:
            print('abormal2')
            result = result + '-1' + ','
    result = result.strip(',')
    return result
     
        
final_result = myfun1(my_dir,post_fix1) + my_fun2(my_dir,post_fix2)
print(final_result)