TypeError: join() argument must be str or bytes, not 'list'

10,535

f_names is a list, you need to iterate over it to get each filename separately and use in os.path.join e.g.:

for root, d_names, f_names in dt:
    for filename in f_names:
        os.path.join(root, filename)
Share:
10,535
coder_3476
Author by

coder_3476

Updated on June 14, 2022

Comments

  • coder_3476
    coder_3476 about 2 years

    I am new to python and just learning the os.walk() and tarfile. I am trying to traverse a folder which has files and subfolders with files, and trying to add all of them to tar file. I keep getting the error "TypeError: join() argument must be str or bytes, not 'list'"

    Before I tried to add to the tar file, I have tried to just print the contents. Gives the same error. I can get through that by adding str to the parameters of the os.path.dirname but not sure if that is the right thing to do.

    import tarfile
    import os
    
    tnt = tarfile.open("sample.tar.gz", 'w:gz')
    
    dt = os.walk('C:\\users\\cap\\desktop\\test1')
    for root, d_names, f_names in dt:
       print(os.path.join((root), (f_names))) #error
       tnt.add(os.path.join(root, f_names) #error
    tnt.close()
    
    print(os.path.join((root), (f_names)))
    genericpath._check_arg_types('join', path, *paths)
    

    Output:

    TypeError: join() argument must be str or bytes, not 'list''''