python 尝试创建文件列表时出错

bqucvtff  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(148)

我有一个包含20个CSV文件的文件夹。每个文件大约有10列和数千行。csv文件如下所示:
| 基因|p值|xyz|
| --|--|--|
| 禁毒常务|0.05| 123 |
| MMP2|零点零二| 456 |
| MMP9|零点零七| 789 |
| nNOS|零点零九| 123 |
| GFAP| 0.01| 456 |
我编写了下面的脚本,目的是遍历每个文件,并仅根据我所指示的感兴趣的基因(在本例中为mmp 2和mmp 9)过滤行。

# the goal is to edit and save the csv files so they only contain the genes of interest

path = '/Users/adriana/Library/Documents/raw_data',
all_files = glob.glob(os.path.join(path, "*.csv")) #make list of file paths 
genes = ["mmp2", "mmp9"]
for file in all_files:
    path = '/Users/adriana/Library/Documents/raw_data'
    df = pd.read_csv(file,delimiter ='\t')
    cleaned = df[df['gene'].isin(genes)]
    cleaned.to_csv(file)

但是,我得到了以下与创建对象“all_files”相关的错误:
TypeError:应为str、bytes或os。PathLike对象,而不是元组
我以前无缝地使用过这个脚本,所以我不确定发生了什么。

ozxc1zmp

ozxc1zmp1#

试试这个:

import os
import glob
import pandas as pd


path = '/Users/adriana/Library/Documents/raw_data'  # Removed comma here
all_files = glob.glob(os.path.join(path, "*.csv"))  # make list of file paths 
genes = ["mmp2", "mmp9"]
for file in all_files:
    df = pd.read_csv(file, delimiter='\t')
    cleaned = df[df['gene'].isin(genes)]  
    cleaned.to_csv(file, index=False)

相关问题