Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to call function
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


for i in range(len(lst_file_smry)):
    input_file= in_dir+"\\"+lst_file_smry[i]+".csv"
    def readxl():
        f= pd.read_csv(input_file)
    if __name__== "__main__":
    def write2csv():
    if __name__== "__main__":


NameError: name 'f' is not defined
may be my function definition or function calling is bad.
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to call/read function for all elements in my list in python johnny_sav1992 1 211 Jul-27-2020, 04:19 PM
Last Post: buran
  Python: Call function with variabele? Ending in error. efclem 5 349 Apr-22-2020, 02:35 PM
Last Post: buran
  How to mock an object that is created during function call? Schlangenversteher 0 345 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Is there a way to search for function call? mtran 2 335 Jan-14-2020, 02:07 AM
Last Post: mtran
  what would you call the input for the parameter(s) of a function you have defined? rix 3 444 Dec-16-2019, 12:04 AM
Last Post: rix
  Call to a print in a defined function leodavinci1990 1 377 Nov-20-2019, 01:45 AM
Last Post: ichabod801
  How can I call a c++ function using python HWDev 1 436 Oct-22-2019, 06:19 PM
Last Post: Larz60+
  I'm trying to figure out whether this is a method or function call 357mag 2 657 Jul-04-2019, 01:43 AM
Last Post: ichabod801
  Call a Function contained in a Tuple SamGer 7 862 Jun-14-2019, 05:00 PM
Last Post: ThomasL
  Python function call font color JeffR1992 1 1,044 Apr-17-2019, 06:38 AM
Last Post: perfringo

Forum Jump:

Users browsing this thread: 1 Guest(s)