首页 > 解决方案 > TypeError:join() 参数必须是 str、bytes 或 os.PathLike 对象,而不是“元组”

问题描述

我想处理我的所有文件path。如果它是一个 zip 文件,我想将其内容提取为img.

from pathlib import Path
import pandas as pd
import zipfile
import os
import sys
import pathlib

path = "./CODEX/input/"

for filename in os.walk(os.getcwd()):
    with open(os.path.join(os.getcwd(), filename), 'r') as f:
        # Antibody information
        ab = pd.read_excel("HUBMAP B004 codex antibodies metadata.xlsx")
        # TODO: Rename antibody_name column so that it matches channelNames.txt
        
        # CODEX images
        if filename.endswith(".zip"):
            img = zipfile.ZipFile(os.path.abspath(filename)).extractall(path)
            print(img)
> --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call
> last) /tmp/ipykernel_18751/2066110210.py in <module>
>       1 for filename in os.walk(os.getcwd()):
> ----> 2     with open(os.path.join(os.getcwd(), filename), 'r') as f:
>       3         # Antibody information
>       4         ab = pd.read_excel("HUBMAP B004 codex antibodies metadata.xlsx")
>       5         # TODO: Rename antibody_name column so that it matches channelNames.txt
> 
> /usr/lib/python3.8/posixpath.py in join(a, *p)
>      88                 path += sep + b
>      89     except (TypeError, AttributeError, BytesWarning):
> ---> 90         genericpath._check_arg_types('join', a, *p)
>      91         raise
>      92     return path
> 
> /usr/lib/python3.8/genericpath.py in _check_arg_types(funcname, *args)
>     150             hasbytes = True
>     151         else:
> --> 152             raise TypeError(f'{funcname}() argument must be str, bytes, or '
>     153                             f'os.PathLike object, not {s.__class__.__name__!r}') from None
>     154     if hasstr and hasbytes:
> 
> TypeError: join() argument must be str, bytes, or os.PathLike object,
> not 'tuple'

标签: pythonfilezipzipfilepathlib

解决方案


您可以使用 os.listdir() 代替 os.walk()

for filename in os.listdir(path):
    pass

推荐阅读