- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
os.path.getsize(path) 和 os.stat 之间有什么不同?哪一个最适合在 python 3 中使用?我们什么时候使用它们?为什么我们有两个相同的解决方案?我发现this回答但我无法理解这句话的意思:
From this, it seems pretty clear that there is no reason to expect the two approaches to behave differently (except perhaps due to the different structures of the loops in your code)
具体来说,为什么我们有两种方法,有什么不同?
最佳答案
stat
是一个 POSIX 系统调用(可在 Linux、Unix 甚至 Windows 上使用),它返回一堆信息(大小、类型、保护位...)
Python 必须在某个时刻调用它来获取大小 ( and it does ),但没有系统调用来仅获取大小。
所以它们在性能方面是相同的(使用stat
可能更快,但这只是多了1个函数调用,所以与I/O无关)。就是这样os.path.getsize
写起来比较简单。
也就是说,能够调用 os.path.getsize
您必须确保该路径实际上是一个文件。当在目录上调用时,getsize
返回一些值(在 Windows 上测试),该值可能与节点的大小有关,因此您必须使用 os.path.isfile
第一:再次调用 os.stat
.
最后,如果你想最大化性能,你必须使用 os.stat
,检查 infos 看看 path 是否是一个文件,然后使用 st_size
信息。这样您就可以调用 stat
仅一次。
如果您使用os.walk
扫描目录,您会看到更多隐藏的 stat
来电,所以查看os.scandir
(Python 3.5)。
相关:
关于python - os.path.getsize(path) 或 os.stat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46145221/
我刚刚遇到了书中的示例教程,但我不太明白。 所以这是代码 import java.awt.Color; import java.awt.Font; import java.awt.Graphics;
我需要能够在事件函数中更改 JPanel 的大小,然后再次获取大小。似乎 JPanel 在函数调用完成之前不会更新。我怎样才能得到真正的尺寸? 这是一个 SSCCE: import java.awt.
当我实现时 WindowManager wm = ((WindowManager)context.getSystemService(context.WINDOW_SERVICE)); Display
我有麻烦了。我想知道如何解决它大约两个小时,但我仍然一无所知。你们能帮我吗?我再次编写了这段代码,但它没有改变任何东西,仍然不知道我错在哪里。 public class Historia { publ
我正在尝试从导出函数的通用 js 文件调用 Image.getSize,但出现错误 Cannot read property 'getSize' of undefined 似乎图像未定义。我如何定义它
为了学习我自己的 Direct2D,我正在关注 this example来自 MSDN。 但是我有一个问题。调用 D2D1_SIZE_F rtSize = m_pRenderTarget->GetSi
我有以下代码来查找屏幕的大小: WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
最简单地说,我试图使用组件的宽度来设置其父容器的大小。但是,为组件返回的宽度偏移了大约 4 个像素。 更多详情:我有一个对话框,其中的子面板在中间水平分布。对话框应始终与此子面板列表一样宽,并在列表太
我有一个有趣的情况,我间歇性地从调用 JLabel 的方法 getSize() 得到负值。我想调整 JLabel 的大小以获取尽可能大的空间,但我经常把它倒过来,就像在 this question 中
本文整理了Java中org.bukkit.WorldBorder.getSize()方法的一些代码示例,展示了WorldBorder.getSize()的具体用法。这些代码示例主要来源于Github/
本文整理了Java中android.webkit.WebBackForwardList.getSize()方法的一些代码示例,展示了WebBackForwardList.getSize()的具体用法。
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在尝试计算等宽字体(我希望保持一致)中字形的宽度和高度,但是 ImageFont.getsize()给定不同的单字符串返回不同的值: >>> from PIL import ImageFont >
我想使用 Image.getSize ( https://facebook.github.io/react-native/docs/image.html ) 来获取图像的大小,但第一个参数要求图像源位
List myObjs = myThreads[threadIndex].getMyObjList(); int initialSize = Collections.synchronizedList(
有没有办法判断 getSize() 是否包含状态栏的高度?从对几部不同手机的测试来看,它似乎在某些手机上有效,但在其他手机上无效,并且在尝试使布局中的所有内容正确对齐时令人沮丧。 在这个问题上花了一周
这是我第一次尝试使用 BufferStrategy,非常感谢您提供一些提示。 1) 为什么在下面的代码中,getSize() 在调整窗口大小之前返回 0 的尺寸?如何立即检测窗口的大小? 2) 为什么
Java zipEntry.getSize() 返回实际文件的大小,有时返回 -1(尽管文件大小大于 0)。 Java API 文档说“返回条目数据的未压缩大小,如果不知道则返回 -1。” 不确定在什
我使用以下两种方法为.ttf 字体文件生成文本预览图像 PIL方法: def make_preview(text, fontfile, imagefile, fontsize=30): try
def size_of_dir(dirname): print("Size of directory: ") print(os.path.getsize(dirname)) 是有问题的
我是一名优秀的程序员,十分优秀!