Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否仅列出目录中的文件?_Python - Fatal编程技术网

Python 是否仅列出目录中的文件?

Python 是否仅列出目录中的文件?,python,Python,有没有办法用Python列出目录中的文件(而不是目录)?我知道我可以使用os.listdir和一个os.path.isfile()s循环,但是如果有更简单的东西(比如函数os.path.listfilesindir或其他东西),可能会更好。这是: 或者,如果发电机更适合您,您也可以使其发挥作用: def files(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)

有没有办法用Python列出目录中的文件(而不是目录)?我知道我可以使用
os.listdir
和一个
os.path.isfile()
s循环,但是如果有更简单的东西(比如函数
os.path.listfilesindir
或其他东西),可能会更好。

这是:

或者,如果发电机更适合您,您也可以使其发挥作用:

def files(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield file
然后简单地说:

for file in files(path):
    ...
你可以试试,里面还有很多其他有用的东西

Pathlib是一个面向对象的库,用于与文件系统路径交互。要获取当前目录中的文件,可以执行以下操作:

from pathlib import *
files = (x for x in Path(".") if x.is_file())
for file in files:
    print(str(file), "is a file!")
在我看来,这比使用
os.path
更具python风格


另请参见:.

在Windows中使用pathlib,如下所示:

files=(如果x.is_file(),则x代表路径中的x(“您的_路径”)

生成错误:

TypeError:“WindowsPath”对象不可编辑

你应该使用


使用
pathlib
,仅列出文件的最短方法是:

[x for x in Path("your_path").iterdir() if x.is_file()]

如果需要,可以使用深度支持。

对于处理当前目录中文件的特殊情况,您可以将其作为简单的一行列表:

[f for f in os.listdir(os.curdir) if os.path.isfile(f)]
否则,在更一般的情况下,必须连接目录路径和文件名:

dirpath = '~/path_to_dir_of_interest'
files = [f for f in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, f))]

由于Python3.6,您可以使用带有递归选项“**”的glob。请注意,glob将为您提供所有文件和目录,因此您只能保留属于文件的文件和目录

files = glob.glob(join(in_path, "**/*"), recursive=True)
files = [f for f in files if os.path.isfile(f)]

如果使用Python3,则可以使用

但是,您必须知道,如果使用
is_dir()
方法:

from pathlib import *

#p is directory path
#files is list of files in the form of path type

files=[x for x in p.iterdir() if x.is_file()]
.iterdir()将跳过空文件

我找到的解决方案是:

from pathlib import *

#p is directory path

#listing all directory's content, even empty files
contents=list(p.glob("*"))

#if element in contents isn't a folder, it's a file
#is_dir() even works for empty folders...!

files=[x for x in contents if not x.is_dir()]

一个常见的建议是避免这种进口。也就是说,只需导入您使用的内容,在您的情况下,只需从pathlib import Path导入
。虽然
pathlib
很好,但我认为这取决于手边的脚本-如果它只处理几次文件,根据
pathlib
的不同,可能有点过头了。您忘记在dirname和filename@ayvango这只是一种不同的行为-在这个问题中没有给出完整路径的要求,这是以一种定义良好的方式工作的。os.listdir返回文件的基本名称,只有在当前目录与给定目录匹配的情况下,才可以直接按basename统计它们path@ayvango我明白你的意思-我考虑的是返回值,而不是检查-你确实是对的。我会更新它。根据上面的答案,你可以做:
files=list(filter(lambda x:os.path.isfile(x),os.listdir(path))
如何指定深度?下一步是什么?单线回答引起了我的注意,但我无法理解。也许下一个(os.walk(os.getcwd())[2]更好?
files = glob.glob(join(in_path, "**/*"), recursive=True)
files = [f for f in files if os.path.isfile(f)]
from pathlib import *

#p is directory path
#files is list of files in the form of path type

files=[x for x in p.iterdir() if x.is_file()]
from pathlib import *

#p is directory path

#listing all directory's content, even empty files
contents=list(p.glob("*"))

#if element in contents isn't a folder, it's a file
#is_dir() even works for empty folders...!

files=[x for x in contents if not x.is_dir()]