使用 Python 查找目录中的所有 CSV 文件

发布于 2025-01-04 11:12:07 字数 37 浏览 1 评论 0原文

如何在Python中找到扩展名为.csv的目录中的所有文件?

How can I find all files in directory with the extension .csv in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

茶色山野 2025-01-11 11:12:07
import os
import glob

path = 'c:\\'
extension = 'csv'
os.chdir(path)
result = glob.glob('*.{}'.format(extension))
print(result)
import os
import glob

path = 'c:\\'
extension = 'csv'
os.chdir(path)
result = glob.glob('*.{}'.format(extension))
print(result)
染墨丶若流云 2025-01-11 11:12:07
from os import listdir

def find_csv_filenames( path_to_dir, suffix=".csv" ):
    filenames = listdir(path_to_dir)
    return [ filename for filename in filenames if filename.endswith( suffix ) ]

函数 find_csv_filenames() 以字符串形式返回文件名列表,这些文件名位于具有给定后缀(默认情况下为“.csv”)的目录 path_to_dir 中。

附录

如何打印文件名:

filenames = find_csv_filenames("my/directory")
for name in filenames:
  print name
from os import listdir

def find_csv_filenames( path_to_dir, suffix=".csv" ):
    filenames = listdir(path_to_dir)
    return [ filename for filename in filenames if filename.endswith( suffix ) ]

The function find_csv_filenames() returns a list of filenames as strings, that reside in the directory path_to_dir with the given suffix (by default, ".csv").

Addendum

How to print the filenames:

filenames = find_csv_filenames("my/directory")
for name in filenames:
  print name
骄傲 2025-01-11 11:12:07

通过使用过滤器和 lambda 的组合,您可以轻松过滤掉给定文件夹中的 csv 文件。

import os

all_files = os.listdir("/path-to-dir")    
csv_files = list(filter(lambda f: f.endswith('.csv'), all_files))

# lambda returns True if filename (within `all_files`) ends with .csv or else False
# and filter function uses the returned boolean value to filter .csv files from list files.

By using the combination of filters and lambda, you can easily filter out csv files in given folder.

import os

all_files = os.listdir("/path-to-dir")    
csv_files = list(filter(lambda f: f.endswith('.csv'), all_files))

# lambda returns True if filename (within `all_files`) ends with .csv or else False
# and filter function uses the returned boolean value to filter .csv files from list files.
城歌 2025-01-11 11:12:07

使用 Python OS 模块在目录中查找 csv 文件。

简单的例子在这里:

import os

# This is the path where you want to search
path = r'd:'

# this is the extension you want to detect
extension = '.csv'

for root, dirs_list, files_list in os.walk(path):
    for file_name in files_list:
        if os.path.splitext(file_name)[-1] == extension:
            file_name_path = os.path.join(root, file_name)
            print file_name
            print file_name_path   # This is the full path of the filter file

use Python OS module to find csv file in a directory.

the simple example is here :

import os

# This is the path where you want to search
path = r'd:'

# this is the extension you want to detect
extension = '.csv'

for root, dirs_list, files_list in os.walk(path):
    for file_name in files_list:
        if os.path.splitext(file_name)[-1] == extension:
            file_name_path = os.path.join(root, file_name)
            print file_name
            print file_name_path   # This is the full path of the filter file
亣腦蒛氧 2025-01-11 11:12:07

我必须获取子目录中的 csv 文件,因此,使用 tchlpr 的响应我修改它以最适合我的用例:

import os
import glob

os.chdir( '/path/to/main/dir' )
result = glob.glob( '*/**.csv' )
print( result )

I had to get csv files that were in subdirectories, therefore, using the response from tchlpr I modified it to work best for my use case:

import os
import glob

os.chdir( '/path/to/main/dir' )
result = glob.glob( '*/**.csv' )
print( result )
独自←快乐 2025-01-11 11:12:07
import os

path = 'C:/Users/Shashank/Desktop/'
os.chdir(path)

for p,n,f in os.walk(os.getcwd()):
    for a in f:
        a = str(a)
        if a.endswith('.csv'):
            print(a)
            print(p)

这将有助于识别这些 csv 文件的路径

import os

path = 'C:/Users/Shashank/Desktop/'
os.chdir(path)

for p,n,f in os.walk(os.getcwd()):
    for a in f:
        a = str(a)
        if a.endswith('.csv'):
            print(a)
            print(p)

This will help to identify path also of these csv files

多彩岁月 2025-01-11 11:12:07

使用 python glob 模块可以轻松列出我们需要的文件。

import glob
path_csv=glob.glob("../data/subfolrder/*.csv")

Use the python glob module to easily list out the files we need.

import glob
path_csv=glob.glob("../data/subfolrder/*.csv")
甜警司 2025-01-11 11:12:07

虽然 thclpr 给出的解决方案有效,但它仅扫描目录中的直接文件,而不扫描子目录中的文件(如果有)。虽然这不是要求,但以防万一有人希望扫描下面的子目录,代码使用 os.walk

import os
from glob import glob
PATH = "/home/someuser/projects/someproject"
EXT = "*.csv"
all_csv_files = [file
                 for path, subdir, files in os.walk(PATH)
                 for file in glob(os.path.join(path, EXT))]
print(all_csv_files)

复制自 此博客。

While solution given by thclpr works it scans only immediate files in the directory and not files in the sub directories if any. Although this is not the requirement but just in case someone wishes to scan sub directories too below is the code that uses os.walk

import os
from glob import glob
PATH = "/home/someuser/projects/someproject"
EXT = "*.csv"
all_csv_files = [file
                 for path, subdir, files in os.walk(PATH)
                 for file in glob(os.path.join(path, EXT))]
print(all_csv_files)

Copied from this blog.

相思碎 2025-01-11 11:12:07

您可以使用 glob使用recursive = true,模式**将匹配任何文件以及零个或多个目录、子目录以及目录的符号链接。

import glob, os

os.chdir("C:\\Users\\username\\Desktop\\MAIN_DIRECTORY")

for file in glob.glob("*/.csv", recursive = true):
    print(file)

You could just use glob with recursive = true, the pattern ** will match any files and zero or more directories, subdirectories and symbolic links to directories.

import glob, os

os.chdir("C:\\Users\\username\\Desktop\\MAIN_DIRECTORY")

for file in glob.glob("*/.csv", recursive = true):
    print(file)
夜光 2025-01-11 11:12:07

该解决方案使用 python 函数过滤器。此函数创建一个函数返回 true 的元素列表。在这种情况下,使用的匿名函数是通过 os.listdir('the path i Want to Look in') 获得的目录文件列表的每个元素部分匹配“.csv”

import os

filepath= 'filepath_to_my_CSVs'  # for example: './my_data/'

list(filter(lambda x: '.csv' in x, os.listdir('filepath_to_my_CSVs')))

This solution uses the python function filter. This function creates a list of elements for which a function returns true. In this case, the anonymous function used is partial matching '.csv' on every element of the directory files list obtained with os.listdir('the path i want to look in')

import os

filepath= 'filepath_to_my_CSVs'  # for example: './my_data/'

list(filter(lambda x: '.csv' in x, os.listdir('filepath_to_my_CSVs')))
开始看清了 2025-01-11 11:12:07

许多(链接的)答案使用 os.chdir() 更改工作目录。但你不必这样做。

递归打印 /home/project/ 目录中的所有 CSV 文件:

pathname = "/home/project/**/*.csv"

for file in glob.iglob(pathname, recursive=True):
    print(file)

需要 python 3.5+。来自文档 [1]:

  • pathname 可以是绝对路径(如 /usr/src/Python-1.5/Makefile)或相对路径(如 ../.. /Tools/*/*.gif)
  • pathname 可以包含 shell 样式的通配符。
  • 结果是否排序取决于文件系统。
  • 如果 recursive 为 true,则模式 ** 将匹配任何文件以及零个或多个目录、子目录以及目录的符号链接

[1] https://docs.python.org/3/library/glob.html#glob.glob

Many (linked) answers change working directory with os.chdir(). But you don't have to.

Recursively print all CSV files in /home/project/ directory:

pathname = "/home/project/**/*.csv"

for file in glob.iglob(pathname, recursive=True):
    print(file)

Requires python 3.5+. From docs [1]:

  • pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif)
  • pathname can contain shell-style wildcards.
  • Whether or not the results are sorted depends on the file system.
  • If recursive is true, the pattern ** will match any files and zero or more directories, subdirectories and symbolic links to directories

[1] https://docs.python.org/3/library/glob.html#glob.glob

放肆 2025-01-11 11:12:07

您可以使用 glob使用recursive = True,模式**将匹配任何文件以及零个或多个目录、子目录以及目录的符号链接。

import glob, os

os.chdir("C:\\Users\\username\\Desktop\\MAIN_DIRECTORY")

for file in glob.glob("*/*.csv", recursive = True):
    print(file)

You could just use glob with recursive = True, the pattern ** will match any files and zero or more directories, subdirectories and symbolic links to directories.

import glob, os

os.chdir("C:\\Users\\username\\Desktop\\MAIN_DIRECTORY")

for file in glob.glob("*/*.csv", recursive = True):
    print(file)
来日方长 2025-01-11 11:12:07

简单单行解决方案:

import os
[file for file in os.listdir() if '.csv' in file ]

针对当前目录或子文件夹的

import os
[file for file in os.listdir('subfolder') if '.csv' in file ]

A simple one-line solution for your current directory:

import os
[file for file in os.listdir() if '.csv' in file ]

or for a subfolder:

import os
[file for file in os.listdir('subfolder') if '.csv' in file ]
痴情换悲伤 2025-01-11 11:12:07

请使用这个经过测试的工作代码。此函数将返回指定路径中具有绝对 CSV 文件路径的所有 CSV 文件的列表。

import os
from glob import glob

def get_csv_files(dir_path, ext):
    os.chdir(dir_path)
    return list(map(lambda x: os.path.join(dir_path, x), glob(f'*.{ext}')))

print(get_csv_files("E:\\input\\dir\\path", "csv"))

Please use this tested working code. This function will return a list of all the CSV files with absolute CSV file paths in your specified path.

import os
from glob import glob

def get_csv_files(dir_path, ext):
    os.chdir(dir_path)
    return list(map(lambda x: os.path.join(dir_path, x), glob(f'*.{ext}')))

print(get_csv_files("E:\\input\\dir\\path", "csv"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文