0

I have a function like this. For *args I want to use a list of different Excel tables.

def my_function(*args):
    for x in args:
        # some code

I use this to get the full paths to the tables and make a list of them. There are only tables in the folder I want to use.

cust_tables = os.listdir(r"my_folder")
cust_path = [r"my_folder" + "\\" + x for x in cust_tables]

This creates a list like this:

['C:\\some\\path\\to\\my\\table_1.xlsx', 'C:\\some\\path\\to\\my\\table_2.xlsx']

When I try to call the function my_function(cust_path), I get this error:

File "C:\Users\my.name\AppData\Local\Continuum\anaconda3\lib\genericpath.py", line 30, in isfile
    st = os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not list

I can call the function when using my_function(cust_path[0],cust_path[1]).

Where did I make a mistake?

NK_
  • 361
  • 1
  • 4
  • 11

1 Answers1

3

You need to unpack the list elements when passing as arguments. You can use the splat (*) operator:

my_function(*cust_path)

otherwise, when you do my_function(cust_path), it is passing the whole list as argument; so, args will be a one element tuple containing the list i.e. args will be ([<cust_path_elements ...>],).

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Thank you! I have to admit it was a rather dumb question forgetting about unpacking. – NK_ Feb 26 '18 at 09:40