Python Forum
How to append one function1 results to function2 results
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append one function1 results to function2 results
#1
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]
Reply
#2
results1 =[0,0]
results2 = [0,1]
final = results1 + results2

print(final)
Output:
[0, 0, 0, 1]
Reply
#3
but result1 & result2 are the outputs of function1 & function2.
Reply
#4
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]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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)]
Reply
#6
#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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling methods and getting results out HerrAyas 4 275 Apr-02-2024, 09:37 PM
Last Post: deanhystad
  sort search results by similarity of characters jacksfrustration 5 430 Feb-16-2024, 11:59 PM
Last Post: deanhystad
  plotting based on the results of mathematical formulas Timur 1 333 Feb-08-2024, 07:22 PM
Last Post: Gribouillis
  Updating sharepoint excel file odd results cubangt 1 820 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 765 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 710 Jun-22-2023, 06:04 PM
Last Post: jefsummers
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,436 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Trying to send file to printer with no results. chob_thomas 2 3,361 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  How do I scrape profile information from Twitter People search results? asdad 0 728 Nov-29-2022, 10:25 AM
Last Post: asdad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,364 Sep-27-2022, 01:38 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020