Python 循环遍历文件夹中的所有 CSV 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14262405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:54:09  来源:igfitidea点击:

Loop through all CSV files in a folder

python

提问by Sam Creamer

I'm trying to loop through only the csv files in a folder that contains many kinds of files and many folders, I just want it to list all of the .csv files in this folder.

我试图只遍历包含多种文件和许多文件夹的文件夹中的 csv 文件,我只想列出该文件夹中的所有 .csv 文件。

Here's what I mean:

这就是我的意思:

import os, sys

path = "path/to/dir"
dirs = os.listdir(path)

for file in dirs:
    if file == '*.csv':
        print file

I know there is no wildcard variable in python, but is there a way of doing this?

我知道 python 中没有通配符变量,但是有没有办法做到这一点?

采纳答案by dm03514

Python provides globwhich should do this

Python 提供了glob哪些应该这样做

>>> import glob
>>> glob.glob('/path/to/dir/*.csv')

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

返回与 pathname 匹配的可能为空的路径名列表,该列表必须是包含路径规范的字符串。路径名可以是绝对的(如 /usr/src/Python-1.5/Makefile)或相对的(如 ../../Tools/ /.gif),并且可以包含 shell 样式的通配符。结果中包含损坏的符号链接(如在 shell 中)。

回答by Dan Hooper

Use the glob module: http://docs.python.org/2/library/glob.html

使用 glob 模块:http: //docs.python.org/2/library/glob.html

import glob
path = "path/to/dir/*.csv"
for fname in glob.glob(path):
    print(fname)

回答by Masail Guliyev

I was trying to loop through the folder containing cvs files and print the number and the name of the columns. Following code worked for me

我试图遍历包含 cvs 文件的文件夹并打印列的编号和名称。以下代码对我有用

import pandas as pd
import glob
path = r"C:\Users\gumnwe\OneDrive - BP\Desktop\Personal\eiLink\Skin Project\Skin_Project_Data_2020\*.csv"
for fname in glob.glob(path):
   df=pd.read_csv(fname)
   my_list=list(df.columns)
   print(len(my_list),my_list)