python中获取⽂件⼤⼩_如何在Python中获取⽂件⼤⼩python中获取⽂件⼤⼩
We can get file size in Python using the .
我们可以使⽤在Python中获取⽂件⼤⼩。
Python中的⽂件⼤⼩ (File Size in Python)
The python os module has stat() function where we can pass the file name as argument. This function returns a tuple structure that contains the file information. We can then get its st_size property to get the file size in bytes.
python os模块具有stat()函数,我们可以在其中传递⽂件名作为参数。 该函数返回⼀个包含⽂件信息的元组结构。 然后,我们可以获取其st_size属性以获取⽂件⼤⼩(以字节为单位)。
Here is a simple program to print the file size in bytes and megabytes.
这是⼀个简单的程序,⽤于打印⽂件⼤⼩(以字节和兆字节为单位)。
# get file size in python
import os
file_name = "/Users/"
file_stats = os.stat(file_name)
print(file_stats)
print(f'File Size in Bytes is {file_stats.st_size}')
print(f'File Size in MegaBytes is {file_stats.st_size / (1024 * 1024)}')
Output:
输出 :
File Size In Python
File Size In Python
Python中的⽂件⼤⼩
If you look at the stat() function, we can pass two more arguments: dir_fd and follow_symlinks. However, they are not implemented for Mac OS.
如果您查看stat()函数,我们可以再传递两个参数:dir_fd和follow_symlinks。 但是,它们未在Mac OS中实现。
Here is an updated program where I am trying to use the relative path but it’s throwing NotImplementedError.
这是⼀个更新的程序,我尝试使⽤相对路径,但是会抛出NotImplementedError。
# get file size in pythonpython怎么读取py文件
import os
file_name = ""
relative_path = os.open("/Users/pankaj", os.O_RDONLY)
file_stats = os.stat(file_name, dir_fd=relative_path)
Output:
输出:
Traceback (most recent call last):
File "/Users/pankaj/.../get_file_size.py", line 7, in
file_stats = os.stat(file_name, dir_fd=relative_path)
NotImplementedError: dir_fd unavailable on this platform
Python File Size Relative Path NotImplementedError
Python⽂件⼤⼩相对路径NotImplementedError 翻译⾃:
python中获取⽂件⼤⼩
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论