不能在 Python 中使用列表作为函数参数

NK_

我有一个这样的功能。对于 *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])

我在哪里做错了?

里奥·R

您可以使用 splat 运算符执行此操作:

some_func(*params)

这会导致函数将每个列表项作为单独的参数接收。这里有一个描述:http : //docs.python.org/tutorial/controlflow.html#unpacking-argument-lists

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我不能在 Python 中使用变量作为参数来调用这个函数?

Python:为什么不能在方法中使用self.variable作为可选参数?

为什么不能在函数参数列表中使用匿名对象作为参数?C ++

为什么我不能在Python中使用列表作为字典键?

为什么我不能在Python中使用列表作为字典键?

不能在函数中使用类作为参数(JScript)

我不能在 python 中正确使用

不能在Python 3.1中使用unichr

python不能在处理程序中使用异常字段

直方图条不能在python中使用matplotlib堆叠

为什么我不能在Crontab中使用Python 3?

(Python 3.6)不能在模块中使用变量

为什么我不能在Python中使用“ +”合并字典?

不能在 C Python 扩展中使用定义的类型

Python不能在函数定义之外使用**

为什么不能在函数参数类型中使用模板参数包作为其模板参数列表,所以不能明确指定

为什么列表不能在python中使用'+'运算符与str串联?

为什么<some-name>不能在python列表中使用?

比较大列表中的字符串但不能在 Python 中使用 set

为什么我不能在python3子进程中使用导出的bash函数?

为什么我的while函数不能在Python上运行?

为什么我的函数不能在 python 代码中执行?

c ++不能在构造函数中使用父类型作为参数

我不能在将istream作为参数的函数中使用ifstream吗?

为什么不能在函数中使用模板别名作为参数并自动推导?

不能在Go中使用函数作为类型

为什么alloca不能在函数参数列表中使用?

为什么我不能在列表中使用匿名函数?

不能在R的pmap函数中使用列表