我使用
我需要使用哪个函数才能实现?
我还查看了
os.listdir
,它有效地工作,但是列表中也包含子目录,这不是我想要的:我只需要文件。我需要使用哪个函数才能实现?
我还查看了
os.walk
,它似乎是我想要的东西,但我不确定它是如何工作的。os.listdir
,它有效地工作,但是列表中也包含子目录,这不是我想要的:我只需要文件。os.walk
,它似乎是我想要的东西,但我不确定它是如何工作的。你需要过滤掉目录;os.listdir()
列出给定路径中的所有名称。你可以使用os.path.isdir()
来实现:
basepath = '/path/to/directory'
for fname in os.listdir(basepath):
path = os.path.join(basepath, fname)
if os.path.isdir(path):
# skip directories
continue
请注意,这只会在跟踪符号链接后过滤掉目录。 fname
不一定是一个普通文件,它也可能是指向文件的符号链接。如果您还需要过滤掉符号链接,则需要先使用not os.path.islink()
。
在现代 Python 版本(3.5 或更高版本),甚至更好的选择是使用os.scandir()
函数; 这将产生DirEntry()
实例。在常见情况下,这比先前加载所有信息来确定条目是否为目录快得多:
basepath = '/path/to/directory'
for entry in os.scandir(basepath):
if entry.is_dir():
# skip directories
continue
# use entry.path to get the full path of this entry, or use
# entry.name for the base filename
如果只需要普通文件(而不是符号链接),则可以使用 entry.is_file(follow_symlinks=False)
。
os.walk()
在幕后执行相同的工作;除非需要递归遍历子目录,否则不需要在此处使用 os.walk()
。
这是一个很好的一行代码,采用列表推导式的形式:
[f for f in os.listdir(your_directory) if os.path.isfile(os.path.join(your_directory, f))]
这将返回
指定your_directory
中的文件名列表。
filter(os.path.isfile, os.listdir(你的目录))
- Valentin Lorentzf
只是文件名,而不是完整路径。你需要在那里使用 os.path.isfile(os.path.join(your_directory, f))
。 - Martijn Pietersand f.endswith('.txt')
。 - Nikhil VJimport os
directoryOfChoice = "C:\\" # Replace with a directory of choice!!!
filter(os.path.isfile, os.listdir(directoryOfChoice))
P.S: os.getcwd() 返回当前目录。
for fname in os.listdir('.'):
if os.path.isdir(fname):
pass # do your stuff here for directory
else:
pass # do your stuff here for regular file
使用os.walk()的解决方案如下:
for r, d, f in os.walk('path/to/dir'):
for files in f:
# This will list all files given in a particular directory
from os import scandir
folder = '/home/myfolder/'
for entry in scandir(folder):
if entry.is_dir():
# do code or skip
continue
myfile = folder + entry.name
#do something with myfile
def get_list_of_files_in_dir(directory: str, file_types: str ='*') -> list:
return [f for f in Path(directory).glob(file_types) if f.is_file()]
根据您的示例,您可以像这样使用它:
mypath = '/path/to/directory'
files = get_list_of_files_in_dir(mypath)
files = get_list_of_files_in_dir(mypath, '*.csv')
is_dir
是正确的方法名称。 - mdmjsh