Home > Software engineering >  Python Starting Thread From Function Inside Class TypeError takes 2 positional args but 3 were given
Python Starting Thread From Function Inside Class TypeError takes 2 positional args but 3 were given

Time:11-01

    def tfunc(this.args.dir, folder):
      for file in os.listdir(folder):
        this.executeFile(os.path.join(this.args.dir, file))

    def executeFiles(this):
      for folders1 in os.listdir(this.args.dir):
        for folders2 in os.listdir("downloaded/"   os.path.join(folders1)):
            t = threading.Thread(target=this.tfunc, args=(this, folders2))
            t.start()

Just like the title says, I've tried wtih a comma after last argument to no avail. What am I doing wrong? Please help.

Edit: Reproducible code. Sorry for not posting this directly.Now I can't add the code because it says that there is too much code and not enough text so I'm writing as much as possible here to bne able to post the code.

class MoodysParser :

def complete(this) :
    print(f"Complete")
    exit()

def executeFile(this, filename):
    print(filename)
    

def tfunc(dir, folder):
    for file in os.listdir(folder):
        this.executeFile(os.path.join(dir, file))

def executeFiles(this):
    for folders1 in os.listdir(this.args.dir):
        for folders2 in os.listdir("downloaded/"   os.path.join(folders1)):
            t = threading.Thread(target=this.tfunc, args=(this.args.dir, folders2))
            t.start()



def execute(this) :
    sys.setrecursionlimit(10000)
    this.timestamp = int(time.time());
    this.dir = os.path.dirname(__file__);
    this.out_fn = f"moodys-results-{this.timestamp}.csv";
    this.file_x = 1
    this.fields_template = {};
    this.fields_template["company"] = "";
    this.fields_template["telephone"] = "";
    this.fields_template["address"] = "";
    this.fields_template["telephone2"] = "";
    this.fields_template["link_id"] = "";
    this.fields_template["location_type"] = "";
    this.fields_template["industry"] = "";
    this.fields_template["sales_range"] = "";
    this.fields_template["employees"] = "";
    this.fields_template["alerts_risk_increase"] = "";
    this.fields_template["alerts_overall_payments"] = "";
    this.fields_template["alerts_peer_payments"] = "";
    this.fields_template["alerts_bankruptcy"] = "";
    this.fields_template["alerts_tax_liens"] = "";
    this.fields_template["alerts_financial_news"] = "";
    this.fields_template["alerts_ucc_filings"] = "";
    this.fields_template["number_reviews"] = "";
    this.fields_template["average_rating"] = "";
    this.fields_template["reviews"] = "";
    this.firstrun = not os.path.isfile(this.out_fn)
    this.out = open(this.out_fn, 'w', newline='', encoding="utf8");
    print(f"Opening {this.out_fn}");
    this.writer = csv.writer(this.out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL);
    this.out.write(b'\xEF\xBB\xBF'.decode())
    this.writer.writerow(this.fields_template.keys());
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir', type=str, required=True);
    parser.add_argument('--max', type=int);
    parser.add_argument('--debug', action=argparse.BooleanOptionalAction);
    this.args = parser.parse_args();
    if not os.path.isdir(this.args.dir):
        print(f"Input directory does not exist")
        exit()
    this.executeFiles()
    this.complete();

scraper = MoodysParser(); scraper.execute();

CodePudding user response:

You should not pass this to args. this would be passed to tfunc automatically.

t = threading.Thread(target=this.tfunc, args=(folders2, ))
  • Related