如何在Python中列出目录的所有文件并将其添加到 list
?
如何在Python中列出目录的所有文件并将其添加到 list
?
os.listdir()
将为您提供目录中的所有内容 - 文件和目录。
如果你想 只是 文件,你可以使用过滤掉它 os.path
:
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
或者你可以使用 os.walk()
这将为它访问的每个目录产生两个列表 - 为您分割成文件和目录。如果你只想要顶级目录,你可以在它第一次产生时中断
from os import walk
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
最后,正如该示例所示,将一个列表添加到另一个列表中即可使用 .extend()
要么
>>> q = [1, 2, 3]
>>> w = [4, 5, 6]
>>> q = q + w
>>> q
[1, 2, 3, 4, 5, 6]
就个人而言,我更喜欢 .extend()
我更喜欢使用 glob
模块,因为它模式匹配和扩展。
import glob
print(glob.glob("/home/adam/*.txt"))
它将返回包含查询文件的列表:
['/home/adam/file1.txt', '/home/adam/file2.txt', .... ]
import os
os.listdir("somedirectory")
将返回“somedirectory”中所有文件和目录的列表。
我也在这里做了一个简短的视频: Python:如何获取目录中的文件列表
os.listdir()
或者.....热获取当前目录中的所有文件(和目录)(Python 3)
在Python 3中将文件放在当前目录中的最简单方法是这样。这很简单;使用 os
模块和listdir()函数,你将拥有该目录中的文件(以及目录中的最终文件夹,但你不会在子目录中有文件,因为你可以使用walk - 我会谈论它后来)。
>>> import os
>>> arr = os.listdir()
>>> arr
['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
使用glob
我发现glob更容易选择相同类型的文件或共同的东西。请看以下示例:
import glob
txtfiles = []
for file in glob.glob("*.txt"):
txtfiles.append(file)
使用列表理解
import glob
mylist = [f for f in glob.glob("*.txt")]
如您所知,您在上面的代码中没有文件的完整路径。如果你需要有绝对路径,你可以使用另一个函数 os.path
模块叫 _getfullpathname
,把你得到的文件 os.listdir()
作为一个论点。还有其他方法可以获得完整路径,我们稍后会检查(我更换了,如mexmex所建议,_getfullpathname with abspath
)。
>>> import os
>>> files_path = [os.path.abspath(x) for x in os.listdir()]
>>> files_path
['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt']
我发现这对于在许多目录中查找内容非常有用,它帮助我找到了一个我不记得名字的文件:
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 ".docx" in file:
print(os.path.join(r, file))
os.listdir():获取当前目录中的文件(Python 2)
在Python 2中,如果您想要当前目录中的文件列表,则必须将参数设置为“。”。或os.listdir方法中的os.getcwd()。
>>> import os
>>> arr = os.listdir('.')
>>> arr
['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
>>> # Method 1
>>> x = os.listdir('..')
# Method 2
>>> x= os.listdir('/')
>>> import os
>>> arr = os.listdir('F:\\python')
>>> arr
['$RECYCLE.BIN', 'work.txt', '3ebooks.txt', 'documents']
import os
x = os.listdir("./content")
>>> import os
>>> arr = next(os.walk('.'))[2]
>>> arr
['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt']
import glob
print(glob.glob("*"))
out:['content', 'start.py']
>>> 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)
>output
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]
>>>x
['F:\\_python\\dict.py', 'F:\\_python\\progr.txt', 'F:\\_python\\readl.py']
>>> arr_txt = [x for x in os.listdir() if x.endswith(".txt")]
>>> print(arr_txt)
['work.txt', '3ebooks.txt']
>>> import glob
>>> x = glob.glob("*.txt")
>>> x
['ale.txt', 'alunni2015.txt', 'assenze.text.txt', 'text2.txt', 'untitled.txt']
如果我需要文件的绝对路径:
>>> from path import path
>>> from glob import glob
>>> x = [path(f).abspath() for f in glob("F:\*.txt")]
>>> for f in x:
... print(f)
...
F:\acquistionline.txt
F:\acquisti_2018.txt
F:\bootstrap_jquery_ecc.txt
如果我想要目录中的所有文件:
>>> x = glob.glob("*")
import os.path
listOfFiles = [f for f in os.listdir() if os.path.isfile(f)]
print(listOfFiles)
> output
['a simple game.py', 'data.txt', 'decorator.py']
import pathlib
>>> flist = []
>>> for p in pathlib.Path('.').iterdir():
... if p.is_file():
... print(p)
... flist.append(p)
...
error.PNG
exemaker.bat
guiprova.mp3
setup.py
speak_gui2.py
thumb.PNG
如果你想使用列表理解
>>> flist = [p for p in pathlib.Path('.').iterdir() if p.is_file()]
import os
x = [i[2] for i in os.walk('.')]
y=[]
for t in x:
for f in t:
y.append(f)
>>> y
['append_to_list.py', 'data.txt', 'data1.txt', 'data2.txt', 'data_180617', 'os_walk.py', 'READ2.py', 'read_data.py', 'somma_defaltdic.py', 'substitute_words.py', 'sum_data.py', 'data.txt', 'data1.txt', 'data_180617']
>>> import os
>>> x = next(os.walk('F://python'))[2]
>>> x
['calculator.bat','calculator.py']
>>> import os
>>> next(os.walk('F://python'))[1] # for the current dir use ('.')
['python3','others']
>>> for r,d,f in os.walk("F:\_python"):
... for dirs in d:
... print(dirs)
...
.vscode
pyexcel
pyschool.py
subtitles
_metaprogramming
.ipynb_checkpoints
>>> import os
>>> x = [f.name for f in os.scandir() if f.is_file()]
>>> x
['calculator.bat','calculator.py']
# 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)
...
ebookmaker.py
error.PNG
exemaker.bat
guiprova.mp3
setup.py
speakgui4.py
speak_gui2.py
speak_gui3.py
thumb.PNG
>>>
在此示例中,我们查找包含在所有目录及其子目录中的文件数。
import os
def count(dir, counter=0):
"returns number of files in dir and subdirs"
for pack in os.walk(dir):
for f in pack[2]:
counter += 1
return dir + " : " + str(counter) + "files"
print(count("F:\\python"))
> output
>'F:\\\python' : 12057 files'
一个脚本,用于在计算机中查找所有类型的文件(默认值:pptx)并将其复制到新文件夹中。
import os
import shutil
from path import path
destination = "F:\\file_copied"
# os.makedirs(destination)
def copyfile(dir, filetype='pptx', counter=0):
"Searches for pptx (or other - pptx is the default) files and copies them"
for pack in os.walk(dir):
for f in pack[2]:
if f.endswith(filetype):
fullpath = pack[0] + "\\" + f
print(fullpath)
shutil.copy(fullpath, destination)
counter += 1
if counter > 0:
print("------------------------")
print("\t==> Found in: `" + dir + "` : " + str(counter) + " files\n")
for dir in os.listdir():
"searches for folders that starts with `_`"
if dir[0] == '_':
# copyfile(dir, filetype='pdf')
copyfile(dir, filetype='txt')
> Output
_compiti18\Compito Contabilità 1\conti.txt
_compiti18\Compito Contabilità 1\modula4.txt
_compiti18\Compito Contabilità 1\moduloa4.txt
------------------------
==> Found in: `_compiti18` : 3 files
如果您要创建包含所有文件名的txt文件:
import os
mylist = ""
with open("filelist.txt", "w", encoding="utf-8") as file:
for eachfile in os.listdir():
mylist += eachfile + "\n"
file.write(mylist)
得到的一线解决方案 只有文件列表 (没有子目录):
filenames = next(os.walk(path))[2]
或绝对路径名:
paths = [os.path.join(path,fn) for fn in next(os.walk(path))[2]]
从目录及其所有子目录获取完整文件路径
import os
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
return file_paths # Self-explanatory.
# Run the above function and store its results in a variable.
full_file_paths = get_filepaths("/Users/johnny/Desktop/TEST")
print full_file_paths
这将打印列表:
['/Users/johnny/Desktop/TEST/file1.txt', '/Users/johnny/Desktop/TEST/file2.txt', '/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat']
如果您愿意,可以打开并阅读内容,或只关注扩展名为“.dat”的文件,如下面的代码所示:
for f in full_file_paths:
if f.endswith(".dat"):
print f
/Users/johnny/Desktop/TEST/SUBFOLDER/file3.dat
从版本3.4开始就有内置版本 迭代器 对于这个比它更有效率 os.listdir()
:
pathlib
: 版本3.4中的新功能。
>>> import pathlib
>>> [p for p in pathlib.Path('.').iterdir() if p.is_file()]
根据 PEP 428,目的 pathlib
library是提供一个简单的类层次结构来处理文件系统路径以及用户对它们执行的常见操作。
os.scandir()
: 版本3.5中的新功能。
>>> import os
>>> [entry for entry in os.scandir('.') if entry.is_file()]
注意 os.walk()
使用 os.scandir()
代替 os.listdir()
从版本3.5开始,它的速度提高了2到20倍 PEP 471。
我还建议您阅读下面的ShadowRanger评论。
我非常喜欢 adamk的回答,建议你使用 glob()
,来自同名模块。这允许您与模式匹配 *
秒。
但正如其他人在评论中指出的那样, glob()
可以通过不一致的斜线方向绊倒。为了帮助你,我建议你使用 join()
和 expanduser()
功能 os.path
模块,也许是 getcwd()
功能在 os
模块,以及。
例如:
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库模块中可以找到的一些函数的强大功能。
def list_files(path):
# returns a list of names (with extension, without full path) of all files
# in folder path
files = []
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)):
files.append(name)
return files
你应该用 os
用于列出目录内容的模块。os.listdir(".")
返回目录的所有内容。我们迭代结果并附加到列表中。
import os
content_list = []
for content in os.listdir("."): # "." means current directory
content_list.append(content)
print content_list