#๐Ÿ”’ Thread arguments wrong

11 messages ยท Page 1 of 1 (latest)

void bronze
#

Sorry for the horrible title, it's very hard to explain.
I have this code:

def get_all_zips(path):
    zips = []
    path_encode = os.fsencode(path)
    for file in os.listdir(path_encode):
        filename = os.fsdecode(file)
        if filename.endswith(".zip") or filename.endswith(".rar"):
            zips.append(os.path.join(path, filename))
            continue
    return zips

def process(zipfiles):
    print(zipfiles)
    for file in zipfiles:
        extract(file, destination)

# Definitely code I wrote and not copied from stack overflow verbatim.
def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

zipfiles_chunks = list(chunks(get_all_zips(staging_dir), math.ceil(len(get_all_zips(staging_dir)) / 2)))

thread1 = threading.Thread(target=process, args=zipfiles_chunks[0])

thread1.start()
thread1.join()

zipfiles_chunks is equal to: [['./staging/zip1.zip'], ['./staging/zip2.zip']]
So, zipfiles_chunks[0] should be a list that contains ./staging/zip1.zip as an element, which it is if I print it. But when I print it in the method process it prints out what is equivalent to zipfiles_chunks[0][0]. It's not a list but a string, what should be zipfiles[0] in the method. What gives?

twilit yarrowBOT
#

@void bronze

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

void bronze
#

To make it clear, I expect zipfiles of process to be a list containing the location of the zip file, but as it stands, I am given the string that is contained inside that list.

cloud pewter
#

i havent touched threads in forever but my assumption is that args expects a list of arguments, so when you pass it a list it tries to pass that in verbatim

void bronze
#

Hmm, not sure how I would pass in the first chunk then.

cloud pewter
#

if you only want to pass the first item of zipfiles_chunks[0], you can wrap it in a list like [zipfiles_chunks[0]] (note the extra square brackets on the outside)

void bronze
#

Yup. that fixes it.

#

Thank you so much!

#

!close

twilit yarrowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.