@@ -2839,18 +2839,75 @@ features:
2839
2839
``follow_symlinks=False `` had been specified instead of raising an error.
2840
2840
2841
2841
2842
+ .. function :: statx(path, mask, *, dir_fd=None, follow_symlinks=True, flags=0)
2843
+
2844
+ Get selected fields of the status of a file or a file descriptor. Perform the
2845
+ equivalent of a :c:func: `statx ` system call on the given path. *path * may be
2846
+ specified as either a string or bytes -- directly or indirectly through the
2847
+ :class: `PathLike ` interface -- or as an open file descriptor. Return a
2848
+ :class: `stat_result ` object.
2849
+
2850
+ *mask * is a bitwise combination of the ``STATX_* `` attributes in the
2851
+ :mod: `stat ` module, indicating which fields the caller intends to use. Note
2852
+ that the set of fields returned may differ from what's requested, if the
2853
+ operating system or file system does not support the metadata, or if it can
2854
+ provide additional fields with no extra effort.
2855
+
2856
+ This function normally follows symlinks; to stat a symlink add the argument
2857
+ ``follow_symlinks=False ``.
2858
+
2859
+ This function can support :ref: `specifying a file descriptor <path_fd >` and
2860
+ :ref: `not following symlinks <follow_symlinks >`.
2861
+
2862
+ On Windows, passing ``follow_symlinks=False `` will disable following all
2863
+ name-surrogate reparse points, which includes symlinks and directory
2864
+ junctions. Other types of reparse points that do not resemble links or that
2865
+ the operating system is unable to follow will be opened directly. When
2866
+ following a chain of multiple links, this may result in the original link
2867
+ being returned instead of the non-link that prevented full traversal. To
2868
+ obtain stat results for the final path in this case, use the
2869
+ :func: `os.path.realpath ` function to resolve the path name as far as
2870
+ possible and call :func: `lstat ` on the result. This does not apply to
2871
+ dangling symlinks or junction points, which will raise the usual exceptions.
2872
+
2873
+ .. index :: module: stat
2874
+
2875
+ Example::
2876
+
2877
+ >>> import os, stat
2878
+ >>> statinfo = os.statx('somefile.txt', stat.STATX_SIZE)
2879
+ >>> statinfo
2880
+ os.stat_result(st_mode=33188, st_ino=0, st_dev=0,
2881
+ st_nlink=1, st_uid=0, st_gid=0, st_size=264, st_atime=0,
2882
+ st_mtime=0, st_ctime=0)
2883
+ >>> statinfo.stx_mask & stat.STATX_SIZE
2884
+ 512
2885
+ >>> statinfo.st_size
2886
+ 264
2887
+
2888
+ .. seealso ::
2889
+
2890
+ :func: `stat `, :func: `fstat ` and :func: `lstat ` functions.
2891
+
2892
+ .. versionadded :: 3.12
2893
+ Added ``statx `` function.
2894
+
2895
+
2842
2896
.. class :: stat_result
2843
2897
2844
2898
Object whose attributes correspond roughly to the members of the
2845
2899
:c:type: `stat ` structure. It is used for the result of :func: `os.stat `,
2846
- :func: `os.fstat ` and :func: `os.lstat `.
2900
+ :func: `os.fstat `, :func: ` os.lstat ` and :func: `os.statx `.
2847
2901
2848
2902
Attributes:
2849
2903
2850
2904
.. attribute :: st_mode
2851
2905
2852
2906
File mode: file type and file mode bits (permissions).
2853
2907
2908
+ This field is set with the :data: `stat.STATX_TYPE ` and/or
2909
+ :data: `stat.STATX_MODE ` flags.
2910
+
2854
2911
.. attribute :: st_ino
2855
2912
2856
2913
Platform dependent, but if non-zero, uniquely identifies the
@@ -2861,70 +2918,111 @@ features:
2861
2918
<https://msdn.microsoft.com/en-us/library/aa363788> `_ on
2862
2919
Windows
2863
2920
2921
+ This field is set with :data: `stat.STATX_INO `.
2922
+
2864
2923
.. attribute :: st_dev
2865
2924
2866
2925
Identifier of the device on which this file resides.
2867
2926
2927
+ On Windows, this field is set with :data: `stat.STATX_INO `.
2928
+
2868
2929
.. attribute :: st_nlink
2869
2930
2870
2931
Number of hard links.
2871
2932
2933
+ This field is set with :data: `stat.STATX_NLINK `.
2934
+
2872
2935
.. attribute :: st_uid
2873
2936
2874
2937
User identifier of the file owner.
2875
2938
2939
+ This field is set with :data: `stat.STATX_UID `.
2940
+
2876
2941
.. attribute :: st_gid
2877
2942
2878
2943
Group identifier of the file owner.
2879
2944
2945
+ This field is set with :data: `stat.STATX_GID `.
2946
+
2880
2947
.. attribute :: st_size
2881
2948
2882
2949
Size of the file in bytes, if it is a regular file or a symbolic link.
2883
2950
The size of a symbolic link is the length of the pathname it contains,
2884
2951
without a terminating null byte.
2885
2952
2953
+ This field is set with :data: `stat.STATX_SIZE `.
2954
+
2955
+ .. attribute :: stx_mask
2956
+
2957
+ Flags indicating which values were set. :func`os.statx` allows specifying
2958
+ a mask, though the result may include more or less than requested. Other
2959
+ ``stat `` functions set a default value representing the information they
2960
+ return.
2961
+
2886
2962
Timestamps:
2887
2963
2888
2964
.. attribute :: st_atime
2889
2965
2890
2966
Time of most recent access expressed in seconds.
2891
2967
2968
+ This field is set with :data: `stat.STATX_ATIME `.
2969
+
2892
2970
.. attribute :: st_mtime
2893
2971
2894
2972
Time of most recent content modification expressed in seconds.
2895
2973
2974
+ This field is set with :data: `stat.STATX_MTIME `.
2975
+
2896
2976
.. attribute :: st_ctime
2897
2977
2898
2978
Platform dependent:
2899
2979
2900
2980
* the time of most recent metadata change on Unix,
2901
- * the time of creation on Windows, expressed in seconds.
2981
+ * the time of creation on Windows, expressed in seconds, except
2982
+ when :data: `stat.STATX_CTIME ` is in :attr: `stx_mask `, in which
2983
+ case this is the time of the most recent metadata change
2984
+
2985
+ This field is set with :data: `stat.STATX_CTIME `.
2902
2986
2903
2987
.. attribute :: st_atime_ns
2904
2988
2905
2989
Time of most recent access expressed in nanoseconds as an integer.
2906
2990
2991
+ This field is set with :data: `stat.STATX_ATIME `.
2992
+
2907
2993
.. attribute :: st_mtime_ns
2908
2994
2909
2995
Time of most recent content modification expressed in nanoseconds as an
2910
2996
integer.
2911
2997
2998
+ This field is set with :data: `stat.STATX_MTIME `.
2999
+
2912
3000
.. attribute :: st_ctime_ns
2913
3001
2914
3002
Platform dependent:
2915
3003
2916
3004
* the time of most recent metadata change on Unix,
2917
3005
* the time of creation on Windows, expressed in nanoseconds as an
2918
- integer.
3006
+ integer, except when :data: `stat.STATX_CTIME ` is in :attr: `stx_mask `,
3007
+ in which case this is the time of the most recent metadata change
3008
+
3009
+ This field is set with :data: `stat.STATX_CTIME `.
3010
+
3011
+ .. attribute :: st_birthtime
3012
+
3013
+ Time of file creation, if available. The attribute may not be present if
3014
+ your operating system does not support the field.
3015
+
3016
+ This field is set with :data: `stat.STATX_BTIME `.
2919
3017
2920
3018
.. note ::
2921
3019
2922
3020
The exact meaning and resolution of the :attr: `st_atime `,
2923
- :attr: `st_mtime `, and :attr: `st_ctime ` attributes depend on the operating
2924
- system and the file system. For example, on Windows systems using the FAT
2925
- or FAT32 file systems, :attr: `st_mtime ` has 2-second resolution, and
2926
- :attr: `st_atime ` has only 1-day resolution. See your operating system
2927
- documentation for details.
3021
+ :attr: `st_mtime `, :attr: `st_ctime ` and :attr: ` st_birthtime ` attributes
3022
+ depend on the operating system and the file system. For example, on
3023
+ Windows systems using the FAT or FAT32 file systems, :attr: `st_mtime ` has
3024
+ 2-second resolution, and :attr: `st_atime ` has only 1-day resolution.
3025
+ See your operating system documentation for details.
2928
3026
2929
3027
Similarly, although :attr: `st_atime_ns `, :attr: `st_mtime_ns `,
2930
3028
and :attr: `st_ctime_ns ` are always expressed in nanoseconds, many
@@ -2943,11 +3041,15 @@ features:
2943
3041
Number of 512-byte blocks allocated for file.
2944
3042
This may be smaller than :attr: `st_size `/512 when the file has holes.
2945
3043
3044
+ This field is set with :data: `stat.STATX_BLOCKS `.
3045
+
2946
3046
.. attribute :: st_blksize
2947
3047
2948
3048
"Preferred" blocksize for efficient file system I/O. Writing to a file in
2949
3049
smaller chunks may cause an inefficient read-modify-rewrite.
2950
3050
3051
+ This field is set with :data: `stat.STATX_BLOCKSIZE `.
3052
+
2951
3053
.. attribute :: st_rdev
2952
3054
2953
3055
Type of device if an inode device.
@@ -2956,17 +3058,27 @@ features:
2956
3058
2957
3059
User defined flags for file.
2958
3060
3061
+ .. attribute :: stx_attributes
3062
+
3063
+ Additional attribute flags (``STATX_ATTR_* `` values).
3064
+
3065
+ This field is only set for calls using :func: `os.statx `.
3066
+
3067
+ .. attribute :: stx_attributes_mask
3068
+
3069
+ Attribute flags (``STATX_ATTR_* ``) that were supported on the file system
3070
+ containing the file. Flags not set in this mask are meaningless in
3071
+ :attr: `stx_attributes `.
3072
+
3073
+ This field is only set for calls using :func: `os.statx `.
3074
+
2959
3075
On other Unix systems (such as FreeBSD), the following attributes may be
2960
3076
available (but may be only filled out if root tries to use them):
2961
3077
2962
3078
.. attribute :: st_gen
2963
3079
2964
3080
File generation number.
2965
3081
2966
- .. attribute :: st_birthtime
2967
-
2968
- Time of file creation.
2969
-
2970
3082
On Solaris and derivatives, the following attributes may also be
2971
3083
available:
2972
3084
@@ -2998,12 +3110,16 @@ features:
2998
3110
:c:func: `GetFileInformationByHandle `. See the ``FILE_ATTRIBUTE_* ``
2999
3111
constants in the :mod: `stat ` module.
3000
3112
3113
+ This field is requested with :data: `stat.STATX_TYPE `.
3114
+
3001
3115
.. attribute :: st_reparse_tag
3002
3116
3003
3117
When :attr: `st_file_attributes ` has the ``FILE_ATTRIBUTE_REPARSE_POINT ``
3004
3118
set, this field contains the tag identifying the type of reparse point.
3005
3119
See the ``IO_REPARSE_TAG_* `` constants in the :mod: `stat ` module.
3006
3120
3121
+ This field is requested with :data: `stat.STATX_TYPE `.
3122
+
3007
3123
The standard module :mod: `stat ` defines functions and constants that are
3008
3124
useful for extracting information from a :c:type: `stat ` structure. (On
3009
3125
Windows, some items are filled with dummy values.)
@@ -3039,6 +3155,13 @@ features:
3039
3155
files as :const: `S_IFCHR `, :const: `S_IFIFO ` or :const: `S_IFBLK `
3040
3156
as appropriate.
3041
3157
3158
+ .. versionchanged :: 3.12
3159
+ Added the :attr: `stx_mask ` member along with :func: `statx `.
3160
+
3161
+ .. versionchanged :: 3.12
3162
+ Added the :attr: `st_birthtime ` member on Windows.
3163
+
3164
+
3042
3165
.. function :: statvfs(path)
3043
3166
3044
3167
Perform a :c:func: `statvfs ` system call on the given path. The return value is
0 commit comments