ファイルへのパスを指定すると、によって提供されるいくつかの方法を使用して、ファイルに関する詳細情報を取得できます。os
モジュール:
os.path.getsize()
ファイルのサイズを返しますos.path.getmtime()
ファイルの最終変更日を返しますos.path.getctime()
ファイルの作成日を返します(macOSなどのUnixシステムの最終変更日と同じです)
次に例を示します。
import os
filename = ‘/Users/flavio/test.txt’
print(os.path.getsize(filename))
print(os.path.getmtime(filename))
print(os.path.getctime(filename))
os.stat()
必要なすべての情報を簡潔に返します。
import os
filename = ‘/Users/flavio/test.txt’
print(os.stat(filename))
それはos.stat_result
オブジェクト:
os.stat_result(st_mode=33252, st_ino=34409711, st_dev=16777224, st_nlink=1, st_uid=501, st_gid=20, st_size=189, st_atime=1605428774, st_mtime=1605428773, st_ctime=1605428773)We have a lot of information here, including:
st_mode
the file type and permissions
st_ino
the inode number
st_dev
the device id
st_uid
the file owner id
st_gid
the file group id
st_size
the file size
and you can reach for individual properties:
import os
filename = ‘/Users/flavio/test.txt’
stats = os.stat(filename)
print(stats.st_size)
print(stats.st_mtime)
More python tutorials:
- Introduction to Python
- Installing Python 3 on macOS
- Running Python programs
- Python 2 vs Python 3
- The basics of working with Python
- Python Data Types
- Python Operators
- Python Strings
- Python Booleans
- Python Numbers
- Python, Accepting Input
- Python Control Statements
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python Functions
- Python Objects
- Python Loops
- Python Modules
- Python Classes
- The Python Standard Library
- Debugging Python
- Python variables scope
- Python, accept arguments from command line
- Python Recursion
- Python Nested Functions
- Python Lambda Functions
- Python Closures
- Python Virtual Environments
- Use a GoPro as a remote webcam using Python
- Python, how to create a list from a string
- Python Decorators
- Python Docstrings
- Python Introspection
- Python Annotations
- Python, how to list files and folders in a directory
- Python, how to check if a number is odd or even
- Python, how to get the details of a file
- Python, how to check if a file or directory exists
- Python Exceptions
- Python, how to create a directory
- Python, how to create an empty file
- Python, the `with` statement
- Python, create a network request
- Python, installing 3rd party packages using `pip`
- Python, read the content of a file