Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call function
#1
Hi,
I defined two function in a forloop, and on each iteration both function should trigger. I use below code, and I am not getting any output.

import pandas as pd
input=open(r'D:\PythonCodes\InputCSV.csv','r')
output=open(r'D:\PythonCodes\OutCSV.csv','w',newline="")

lst_file_smry=['InputCSV','InputCSV2']
in_dir='D:\\Mekala_Backupdata\PythonCodes'
out_dir='D:\\Mekala_Backupdata\PythonCodes'

file_name=in_dir+"\\"+lst_file_smry[1]
len_file_lst=len(lst_file_smry)
for i in range(len(lst_file_smry)):
    input_file= in_dir+"\\"+lst_file_smry[i]+".csv"
    output_file=out_dir+"\\"+lst_file_smry[i]+"out.csv"
    print("infile:",input_file)
    print("outfile:",output_file)
    def readxl():
        f= pd.read_csv(input_file)
    if __name__== "__main__":
        readxl()
    def write2csv():
        f.to_csv(output_file)
    if __name__== "__main__":
        write2csv()
error:

NameError: name 'f' is not defined
may be my function definition or function calling is bad.
Reply
#2
Variables defined in a function are only available inside that function. You need to return the value from readxl with a return statement, and then pass it to write2csv with a parameter. See the function tutorial on how to do that. Additionally, if you defined both of those functions to take parameters for the file paths, you could define them once rather than redefining them every time you go through the loop.

Also, you use of if __name__ == '__main__': is very odd. That is usually done once at the end of the program. As it is, you are running a bunch of code whether you import the code or run it as the primary program. I would put all of the code in one function, and then use a single if __name__ at the end to call that function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,419 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,163 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,863 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,160 Oct-06-2020, 03:58 PM
Last Post: snippsat
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,041 Jul-27-2020, 04:19 PM
Last Post: buran
  Python: Call function with variabele? Ending in error. efclem 5 2,881 Apr-22-2020, 02:35 PM
Last Post: buran
  How to mock an object that is created during function call? Schlangenversteher 0 1,949 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Is there a way to search for function call? mtran 2 2,223 Jan-14-2020, 02:07 AM
Last Post: mtran

Forum Jump:

User Panel Messages

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