我有一个这样的功能。对于 *args,我想使用不同 Excel 表格的列表。
def my_function(*args):
for x in args:
# some code
我用它来获取表格的完整路径并列出它们。我想使用的文件夹中只有表。
cust_tables = os.listdir(r"my_folder")
cust_path = [r"my_folder" + "\\" + x for x in cust_tables]
这将创建一个如下列表:
['C:\\some\\path\\to\\my\\table_1.xlsx', 'C:\\some\\path\\to\\my\\table_2.xlsx']
当我尝试调用该函数时my_function(cust_path)
,出现此错误:
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
我可以在使用时调用该函数my_function(cust_path[0],cust_path[1])
。
我在哪里做错了?
您可以使用 splat 运算符执行此操作:
some_func(*params)
这会导致函数将每个列表项作为单独的参数接收。这里有一个描述:http : //docs.python.org/tutorial/controlflow.html#unpacking-argument-lists
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句