如何在Python中列出目录中的所有文件并将其添加到列表中?


当前回答

我真的很喜欢adamk的回答,建议您使用来自同名模块的glob()。这允许您使用*s进行模式匹配。

但正如其他人在评论中指出的,glob()可能会被不一致的斜线方向绊倒。为了帮助实现这一点,我建议您在os.path模块中使用join()和expanduser()函数,也可以在os模块中使用getcwd()函数。

例如:

from glob import glob

# Return everything under C:\Users\admin that contains a folder called wlp.
glob('C:\Users\admin\*\wlp')

上面的情况很糟糕-路径已被硬编码,并且只能在Windows上在驱动器名称和硬编码到路径之间工作。

from glob    import glob
from os.path import join

# Return everything under Users, admin, that contains a folder called wlp.
glob(join('Users', 'admin', '*', 'wlp'))

上面的方法效果更好,但它依赖于文件夹名Users,该文件夹名在Windows中常见,而在其他操作系统中不常见。它还依赖于具有特定名称admin的用户。

from glob    import glob
from os.path import expanduser, join

# Return everything under the user directory that contains a folder called wlp.
glob(join(expanduser('~'), '*', 'wlp'))

这在所有平台上都非常有效。

另一个很好的例子,它可以在不同的平台上完美运行,并且做了一些不同的事情:

from glob    import glob
from os      import getcwd
from os.path import join

# Return everything under the current directory that contains a folder called wlp.
glob(join(getcwd(), '*', 'wlp'))

希望这些示例能帮助您了解在标准Python库模块中可以找到的一些函数的功能。

其他回答

为了获得更好的结果,您可以使用os模块的listdir()方法和生成器(生成器是一个强大的迭代器,可以保持其状态,记得吗?)。以下代码适用于两个版本:Python 2和Python 3。

这里有一个代码:

import os

def files(path):  
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield file

for file in files("."):  
    print (file)

listdir()方法返回给定目录的条目列表。如果给定的条目是文件,则os.path.isfile()方法返回True。yield运算符退出func,但保持其当前状态,并且只返回作为文件检测到的条目的名称。以上所有内容都允许我们对生成器函数进行循环。

import os
os.listdir("somedirectory")

将返回“somedirectory”中所有文件和目录的列表。

当前目录中的列表

使用os模块中的listdir,可以获得当前目录中的文件和文件夹

import os

arr = os.listdir()

查找目录

arr = os.listdir('c:\\files')

使用glob,可以指定要列出的文件类型,如下所示

import glob

txtfiles = []
for file in glob.glob("*.txt"):
    txtfiles.append(file)

or

mylist = [f for f in glob.glob("*.txt")]

仅获取当前目录中文件的完整路径

import os
from os import listdir
from os.path import isfile, join

cwd = os.getcwd()
onlyfiles = [os.path.join(cwd, f) for f in os.listdir(cwd) if 
os.path.isfile(os.path.join(cwd, f))]
print(onlyfiles) 

['G:\\getfilesname\\getfilesname.py', 'G:\\getfilesname\\example.txt']

使用os.path.abspath获取完整路径名

你得到了完整的路径作为回报

 import os
 files_path = [os.path.abspath(x) for x in os.listdir()]
 print(files_path)
 
 ['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt']

漫游:遍历子目录

os.walk返回根目录、目录列表和文件列表,这就是为什么我在for循环中在r、d、f中解压缩它们;然后,它在根目录的子文件夹中查找其他文件和目录,依此类推,直到没有子文件夹为止。

import os

# Getting the current work directory (cwd)
thisdir = os.getcwd()

# r=root, d=directories, f = files
for r, d, f in os.walk(thisdir):
    for file in f:
        if file.endswith(".docx"):
            print(os.path.join(r, file))

在目录树中向上

# Method 1
x = os.listdir('..')

# Method 2
x= os.listdir('/')

使用os.listdir()获取特定子目录的文件

import os

x = os.listdir("./content")

os.walk('.')-当前目录

 import os
 arr = next(os.walk('.'))[2]
 print(arr)
 
 >>> ['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt']

next(os.walk('.'))和os.path.join('dir','file')

 import os
 arr = []
 for d,r,f in next(os.walk("F:\\_python")):
     for file in f:
         arr.append(os.path.join(r,file))

 for f in arr:
     print(files)

>>> F:\\_python\\dict_class.py
>>> F:\\_python\\programmi.txt

下一个步行

 [os.path.join(r,file) for r,d,f in next(os.walk("F:\\_python")) for file in f]
 
 >>> ['F:\\_python\\dict_class.py', 'F:\\_python\\programmi.txt']

os.walk

x = [os.path.join(r,file) for r,d,f in os.walk("F:\\_python") for file in f]
print(x)

>>> ['F:\\_python\\dict.py', 'F:\\_python\\progr.txt', 'F:\\_python\\readl.py']

os.listdir()-仅获取txt文件

 arr_txt = [x for x in os.listdir() if x.endswith(".txt")]
 

使用glob获取文件的完整路径

from path import path
from glob import glob

x = [path(f).abspath() for f in glob("F:\\*.txt")]

使用os.path.isfile避免列表中的目录

import os.path
listOfFiles = [f for f in os.listdir() if os.path.isfile(f)]

使用Python 3.4中的pathlib

import pathlib

flist = []
for p in pathlib.Path('.').iterdir():
    if p.is_file():
        print(p)
        flist.append(p)

通过列表理解:

flist = [p for p in pathlib.Path('.').iterdir() if p.is_file()]

在pathlib.Path()中使用glob方法

import pathlib

py = pathlib.Path().glob("*.py")

使用os.walk获取所有文件:仅检查返回的第三个元素,即文件列表

import os
x = [i[2] for i in os.walk('.')]
y=[]
for t in x:
    for f in t:
        y.append(f)

仅获取目录中带有next的文件:仅返回根文件夹中的文件

 import os
 x = next(os.walk('F://python'))[2]

使用next只获取目录并在目录中行走,因为在[1]元素中只有文件夹

 import os
 next(os.walk('F://python'))[1] # for the current dir use ('.')
 
 >>> ['python3','others']

使用walk获取所有分区名称

for r,d,f in os.walk("F:\\_python"):
    for dirs in d:
        print(dirs)

Python 3.5及更高版本的os.scandir()

import os
x = [f.name for f in os.scandir() if f.is_file()]

# Another example with `scandir` (a little variation from docs.python.org)
# This one is more efficient than `os.listdir`.
# In this case, it shows the files only in the current directory
# where the script is executed.

import os
with os.scandir() as i:
    for entry in i:
        if entry.is_file():
            print(entry.name)

仅获取文件列表(无子目录)的单行解决方案:

filenames = next(os.walk(path))[2]

或绝对路径名:

paths = [os.path.join(path, fn) for fn in next(os.walk(path))[2]]

如果您正在寻找find的Python实现,这是我经常使用的方法:

from findtools.find_files import (find_files, Match)

# Recursively find all *.sh files in **/usr/bin**
sh_files_pattern = Match(filetype='f', name='*.sh')
found_files = find_files(path='/usr/bin', match=sh_files_pattern)

for found_file in found_files:
    print found_file

所以我用它制作了一个PyPI包,还有一个GitHub存储库。我希望有人发现它对这段代码可能有用。