为什么写“from os import path.isfile, path.isdir, scandir”无效?

埃斯特利亚

除了像这样在两行单独的行中编写之外,还有其他方法可以编写它吗?

from os.path import isfile, isdir
from os import scandir # or import os.scandir
一个

TL; DR你不能。如果您from os.path import <stuff>想从os.path.


当您只使用导入东西时,import您只能导入模块(模块是包含 python 对象的 python 文件的术语)。要导入模块内部的对象,您from可以遍历该模块,然后import <whateverObject>从该文件中导入该对象(任何东西都是python中的对象,只要它在.py文件中)。您使用的.是(通常)遍历包内的目录(“子”包?)。

解释器如何知道要在包中包含哪个目录,或者如何将给定目录识别为包?__init__.py它在里面寻找一个文件。如果它找到一个,它就是一个包,因此你可以导入它。

import仅限于访问directories,并且modules最多,如果单独使用。使用时from <module_or_directory> import <objects>,访问目录和/或模块的任务交给 afterfrom子句,而后子句import接管在模块内部查找 python 对象的任务。你看,这是两件不同的事情 - 1)访问文件系统下的文件/目录,以及 2)访问文件的内容,(特别是.py文件),它在 python 的域下 - 在from-import进口东西的风格。


os模块的情况下,os.path是另一个.pyi文件(在 Windows 上),在文件ntpath.pyi中别名为. 由于它是一个模块,因此它位于 in 之后的子句而是模块中的一个函数,因此它在 in 之后的子句os.pypathfromfrom os.path import isfile, isdirscandirosimportfrom os import scandir

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么要同时使用os.path.abspath和os.path.realpath?

python os.path.isfile()对于标量返回true

python os.path.isfile()对于某些整数返回True

os.path.exists与os.path.isdir之间的优缺点

我应该使用`import os.path`或`import os`吗?

os.path.isfile()不起作用。为什么?

使用os.path.isdir()时,“ ./”和“ ../”之间有什么区别?

os.path.isdir()在无法访问但现有目录上返回false

os.path.isdir()和os.path.isfile()都在脚本中返回False,但在Python控制台中返回True(就像应该那样)

Python os.path.isdir为点返回true

路径不是当前目录时,os.path.isdir()无法识别目录

如何使用从YAML文件检索到的utf-8文件名用于os.path.isdir()?

os.path.isfile()对于linux smb网络驱动器上的文件返回false

os.path.isdir()向现有文件夹返回false

使用pytest,如何与os.listdir一起模拟pathlib的Path.isdir()函数

os.path.isfile只识别某些文件,但不能识别所有文件

os.path.isfile()仅在文件位于目录中时才返回true

奇怪的行为os.path.isdir(x)

为什么目录中的os.path.join()给出了无效的目录名称错误?

为什么LD_LIBRARY_PATH无效?

python 2.7 os.path.isdir(unicode abspath字符串)什么都不返回

如果通过传递用户输入变量os.path.isdir存在一个位置

当file_path是相对路径时,os.path.isfile(file_path)返回false,为什么?

无法从paramiko连接中获取命令print os.path.isfile(filename)的返回值

os.path.isfile()对于网络驱动器上的文件返回false

Python:os.path.isfile 没有检测到文件

断言 os.path.isfile 仅适用于完整路径

对于现有的文件系统对象,os.path.isfile 是否总是与 os.path.isdir 相反?

使用 os.path.isfile() 删除目录