首页 > 解决方案 > How to return a data path using class in python

问题描述

I want to read in multiple files into python using a glob package and class in python. I want my function to take two parameters. Namely, a path and a parameter that the function that returns.

Here is my attempt.

import glob as g
converterPath = r"C:\Users\MyfilePath"

class collect_datafiles:
    def __init__(self,a,b):
        try:
            self.a = a
            self.b = g.glob(a)
        except:
            print("Error - While collecting InputFiles")
    def file_list(self):
        return self.b

collect_datafiles(converterPath,0)

Where a is an argument that holds a path of the data files and b is an argument that I want the function to return.

But the above code doesn't seem to work and throwing TypeError: collect_datafiles() takes no arguments. Can anyone help me with how to solve this problem?

标签: pythonfunctionclassobjectglob

解决方案


如果您将一个类与一个函数混淆,那么您的主要问题是。如果您想将您的 collect_datafiles 保留为一个类,您可以设置变量ab使用

file_holder = collect_datafiles() file_holder.a = converterPath


推荐阅读