您的位置 首页 > 娱乐休闲

获取文件夹中的文件列表

1. 现象

获取文件系统中某个目录下的所有文件列表

2. 原因分析

3. 问题解决

os.listdir函数来获取某个目录中的文件列表

import os prin()) # 'E:\code\PythonCookBook\chapter4' os.chdir('..') prin()) # 'E:\code\PythonCookBook' prin('./chapter4')) """ ['merged_file', ';, ';, ';, ';, ';, 'sorted_file_0', 'sorted_file_1', 'sorted_file_2', 'sorted_file_3', 'sorted_file_ 4', 'sorted_file_5'] """

os.listdir函数会返回目录中所有文件列表,包括所有文件,子目录,符号链接等等

如果需要通过某种方式过滤数据,可以考虑结合 os.path 库中的一些函数来使用列表推导

names = [name for name in os.listdir('./chapter4') if os.('./chapter4', name))] dir_names = [name for name in os.listdir('./chapter4') if os.('./chapter4', name))]

字符串的 startswith方法和 endswith方法对于过滤一个目录的内容也是很有用的

py_names = [name for name in os.listdir('./chapter4') if name.endswith('py')] """ [';, ';, ';, ';, ';] """

对于文件名的匹配,你可能会考虑使用 glob 或 fnmatch 模块

glob模块匹配特定模式的路径名称,使用Unix通配符规则

import glob # 相对路径 py_pattern = glob.glob('./chapter4/*.py') # py_pattern 与 py_files 一致 # 绝对路径 py_ab_pattern = glob.glob('E:\code\PythonCookBook\chapter4\*.py') # py_ab_pattern 与 py_ab_files 一致 py_files = [name for name in py_pattern] """ ['./chapter4\\;, './chapter4\\;, './chapter4\\;, './chapter4\\;, './chapter4\\;] """ py_ab_files = [name for name in py_ab_pattern] """ ['E:\\code\\PythonCookBook\\chapter4\\;, 'E:\\code\\PythonCookBook\\chapter4\\;, 'E:\\code\\PythonCookBook\\chapter4\\;, 'E:\\code\\PythonCookBook\\chapter4\\;, 'E:\\code\\PythonCookBook\\chapter4\\;] """

fnmatch模块支持Unix shell格式的通配符

import fnmatch py_files = [name for name in os.listdir('./chapter4') if (name, '*.py')] """ <class 'list'>: [';, ';, ';, ';, ';] """

获取目录中的列表是很容易的,但是其返回结果只是目录中实体名列表

如果需要获取其他的元信息,比如文件大小,修改时间等等,还需要使用到os.path模块中的函数或者os.stat函数来收集数据

import glob import time import os py_pattern = glob.glob('./chapter4/*.py') py_meta = [(name, os.(name), os.(name)) for name in py_pattern] for name, size, mtime in py_meta: print(name, size, (mtime)) """ ./chapter4\ 1460 Sat Oct 9 22:14:52 2021 ./chapter4\ 1663 Sun Oct 10 17:01:04 2021 ./chapter4\ 1276 Mon Oct 11 22:07:16 2021 ./chapter4\ 1902 Sat Oct 23 17:03:45 2021 ./chapter4\ 2733 Sat Oct 23 21:42:16 2021 """ print('####') file_meta = [(name, os.stat(name)) for name in py_pattern] for name, meta in file_meta: print(name, me, me) """ ./chapter4\ 1460 1633788892.536979 ./chapter4\ 1663 1633856464.6744094 ./chapter4\ 1276 1633961236.9767005 ./chapter4\ 1902 1634979825.0937288 ./chapter4\ 2709 1634996395.330934 """

最后还有一点要注意的就是,有时候在处理文件名编码问题时候可能会出现一些问题。通常来讲,函数os.listdir函数返回的实体列表会根据系统默认的文件名编码来解码。有时候会碰到一些不能正常解码的文件名,需要我们进行特殊处理。

import os import sys import locale os.chdir('..') print(type('py\x;)) # '<class 'str'>' prin()) # 'utf-8' prin()) # 'cp936' # encoding 默认编码 cp936 with open('py\x;, 'w') as f_obj: ('Python!') prin('.')) """ ['.idea', 'chapter2', 'chapter3', 'chapter4', 'Chapter_1', 'data', 'main.py', 'models', 'notebooks', 'pyòx.txt', 'README.md', 'requiremen;] """ prin(b'.')) """ [b'.idea', b'chapter2', b'chapter3', b'chapter4', b'Chapter_1', b'data', b'main.py', b'models', b'notebooks', b'py\xc3\xb2x.txt', b'README.md', b'requiremen;] """ with open(b'py\xc3\xb2x.txt', 'r') as f_obj: prin()) """ Python! """

4. 错误经历

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“如何列出某个目录下的所有文件”边界阅读