1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
|
<span id="os-miscellaneous-operating-system-interfaces"></span><h1>os — Miscellaneous operating system interfaces</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/os.py">Lib/os.py</a></p> <p>This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>, if you want to manipulate paths, see the <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a> module, and if you want to read all the lines in all the files on the command line see the <a class="reference internal" href="fileinput#module-fileinput" title="fileinput: Loop over standard input or a list of files."><code>fileinput</code></a> module. For creating temporary files and directories see the <a class="reference internal" href="tempfile#module-tempfile" title="tempfile: Generate temporary files and directories."><code>tempfile</code></a> module, and for high-level file and directory handling see the <a class="reference internal" href="shutil#module-shutil" title="shutil: High-level file operations, including copying."><code>shutil</code></a> module.</p> <p>Notes on the availability of these functions:</p> <ul class="simple"> <li>The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function <code>os.stat(path)</code> returns stat information about <em>path</em> in the same format (which happens to have originated with the POSIX interface).</li> <li>Extensions peculiar to a particular operating system are also available through the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module, but using them is of course a threat to portability.</li> <li>All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.</li> <li>On VxWorks, os.popen, os.fork, os.execv and os.spawn*p* are not supported.</li> <li>On WebAssembly platforms <code>wasm32-emscripten</code> and <code>wasm32-wasi</code>, large parts of the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module are not available or behave differently. API related to processes (e.g. <a class="reference internal" href="#os.fork" title="os.fork"><code>fork()</code></a>, <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a>), signals (e.g. <a class="reference internal" href="#os.kill" title="os.kill"><code>kill()</code></a>, <a class="reference internal" href="#os.wait" title="os.wait"><code>wait()</code></a>), and resources (e.g. <a class="reference internal" href="#os.nice" title="os.nice"><code>nice()</code></a>) are not available. Others like <a class="reference internal" href="#os.getuid" title="os.getuid"><code>getuid()</code></a> and <a class="reference internal" href="#os.getpid" title="os.getpid"><code>getpid()</code></a> are emulated or stubs.</li> </ul> <div class="admonition note"> <p class="admonition-title">Note</p> <p>All functions in this module raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> (or subclasses thereof) in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.</p> </div> <dl class="py exception"> <dt class="sig sig-object py" id="os.error">
<code>exception os.error</code> </dt> <dd>
<p>An alias for the built-in <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exception.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.name">
<code>os.name</code> </dt> <dd>
<p>The name of the operating system dependent module imported. The following names have currently been registered: <code>'posix'</code>, <code>'nt'</code>, <code>'java'</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="sys#sys.platform" title="sys.platform"><code>sys.platform</code></a> has a finer granularity. <a class="reference internal" href="#os.uname" title="os.uname"><code>os.uname()</code></a> gives system-dependent version information.</p> <p>The <a class="reference internal" href="platform#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code>platform</code></a> module provides detailed checks for the system’s identity.</p> </div> </dd>
</dl> <section id="file-names-command-line-arguments-and-environment-variables"> <span id="filesystem-encoding"></span><span id="os-filenames"></span><h2>File Names, Command Line Arguments, and Environment Variables</h2> <p>In Python, file names, command line arguments, and environment variables are represented using the string type. On some systems, decoding these strings to and from bytes is necessary before passing them to the operating system. Python uses the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a> to perform this conversion (see <a class="reference internal" href="sys#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code>sys.getfilesystemencoding()</code></a>).</p> <p>The <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a> are configured at Python startup by the <a class="reference internal" href="../c-api/init_config#c.PyConfig_Read" title="PyConfig_Read"><code>PyConfig_Read()</code></a> function: see <a class="reference internal" href="../c-api/init_config#c.PyConfig.filesystem_encoding" title="PyConfig.filesystem_encoding"><code>filesystem_encoding</code></a> and <a class="reference internal" href="../c-api/init_config#c.PyConfig.filesystem_errors" title="PyConfig.filesystem_errors"><code>filesystem_errors</code></a> members of <a class="reference internal" href="../c-api/init_config#c.PyConfig" title="PyConfig"><code>PyConfig</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.1: </span>On some systems, conversion using the file system encoding may fail. In this case, Python uses the <a class="reference internal" href="codecs#surrogateescape"><span class="std std-ref">surrogateescape encoding error handler</span></a>, which means that undecodable bytes are replaced by a Unicode character U+DC<em>xx</em> on decoding, and these are again translated to the original byte on encoding.</p> </div> <p>The <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">file system encoding</span></a> must guarantee to successfully decode all bytes below 128. If the file system encoding fails to provide this guarantee, API functions can raise <a class="reference internal" href="exceptions#UnicodeError" title="UnicodeError"><code>UnicodeError</code></a>.</p> <p>See also the <a class="reference internal" href="../glossary#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a>.</p> </section> <section id="python-utf-8-mode"> <span id="utf8-mode"></span><h2>Python UTF-8 Mode</h2> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7: </span>See <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0540/"><strong>PEP 540</strong></a> for more details.</p> </div> <p>The Python UTF-8 Mode ignores the <a class="reference internal" href="../glossary#term-locale-encoding"><span class="xref std std-term">locale encoding</span></a> and forces the usage of the UTF-8 encoding:</p> <ul class="simple"> <li>Use UTF-8 as the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding</span></a>.</li> <li>
<a class="reference internal" href="sys#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code>sys.getfilesystemencoding()</code></a> returns <code>'utf-8'</code>.</li> <li>
<a class="reference internal" href="locale#locale.getpreferredencoding" title="locale.getpreferredencoding"><code>locale.getpreferredencoding()</code></a> returns <code>'utf-8'</code> (the <em>do_setlocale</em> argument has no effect).</li> <li>
<a class="reference internal" href="sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a>, <a class="reference internal" href="sys#sys.stdout" title="sys.stdout"><code>sys.stdout</code></a>, and <a class="reference internal" href="sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a> all use UTF-8 as their text encoding, with the <code>surrogateescape</code> <a class="reference internal" href="codecs#error-handlers"><span class="std std-ref">error handler</span></a> being enabled for <a class="reference internal" href="sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a> and <a class="reference internal" href="sys#sys.stdout" title="sys.stdout"><code>sys.stdout</code></a> (<a class="reference internal" href="sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a> continues to use <code>backslashreplace</code> as it does in the default locale-aware mode)</li> <li>On Unix, <a class="reference internal" href="#os.device_encoding" title="os.device_encoding"><code>os.device_encoding()</code></a> returns <code>'utf-8'</code> rather than the device encoding.</li> </ul> <p>Note that the standard stream settings in UTF-8 mode can be overridden by <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONIOENCODING"><code>PYTHONIOENCODING</code></a> (just as they can be in the default locale-aware mode).</p> <p>As a consequence of the changes in those lower level APIs, other higher level APIs also exhibit different default behaviours:</p> <ul class="simple"> <li>Command line arguments, environment variables and filenames are decoded to text using the UTF-8 encoding.</li> <li>
<a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code>os.fsdecode()</code></a> and <a class="reference internal" href="#os.fsencode" title="os.fsencode"><code>os.fsencode()</code></a> use the UTF-8 encoding.</li> <li>
<a class="reference internal" href="functions#open" title="open"><code>open()</code></a>, <a class="reference internal" href="io#io.open" title="io.open"><code>io.open()</code></a>, and <a class="reference internal" href="codecs#codecs.open" title="codecs.open"><code>codecs.open()</code></a> use the UTF-8 encoding by default. However, they still use the strict error handler by default so that attempting to open a binary file in text mode is likely to raise an exception rather than producing nonsense data.</li> </ul> <p>The <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled if the LC_CTYPE locale is <code>C</code> or <code>POSIX</code> at Python startup (see the <a class="reference internal" href="../c-api/init_config#c.PyConfig_Read" title="PyConfig_Read"><code>PyConfig_Read()</code></a> function).</p> <p>It can be enabled or disabled using the <a class="reference internal" href="../using/cmdline#cmdoption-X"><code>-X utf8</code></a> command line option and the <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONUTF8"><code>PYTHONUTF8</code></a> environment variable.</p> <p>If the <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONUTF8"><code>PYTHONUTF8</code></a> environment variable is not set at all, then the interpreter defaults to using the current locale settings, <em>unless</em> the current locale is identified as a legacy ASCII-based locale (as described for <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline#envvar-PYTHONCOERCECLOCALE"><code>PYTHONCOERCECLOCALE</code></a>), and locale coercion is either disabled or fails. In such legacy locales, the interpreter will default to enabling UTF-8 mode unless explicitly instructed not to do so.</p> <p>The Python UTF-8 Mode can only be enabled at the Python startup. Its value can be read from <a class="reference internal" href="sys#sys.flags" title="sys.flags"><code>sys.flags.utf8_mode</code></a>.</p> <p>See also the <a class="reference internal" href="../using/windows#win-utf8-mode"><span class="std std-ref">UTF-8 mode on Windows</span></a> and the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
<span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0686/"><strong>PEP 686</strong></a>
</dt>
<dd>
<p>Python 3.15 will make <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> default.</p> </dd> </dl> </div> </section> <section id="process-parameters"> <span id="os-procinfo"></span><h2>Process Parameters</h2> <p>These functions and data items provide information and operate on the current process and user.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.ctermid">
<code>os.ctermid()</code> </dt> <dd>
<p>Return the filename corresponding to the controlling terminal of the process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.environ">
<code>os.environ</code> </dt> <dd>
<p>A <a class="reference internal" href="../glossary#term-mapping"><span class="xref std std-term">mapping</span></a> object where keys and values are strings that represent the process environment. For example, <code>environ['HOME']</code> is the pathname of your home directory (on some platforms), and is equivalent to <code>getenv("HOME")</code> in C.</p> <p>This mapping is captured the first time the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module is imported, typically during Python startup as part of processing <code>site.py</code>. Changes to the environment made after this time are not reflected in <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, except for changes made by modifying <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a> directly.</p> <p>This mapping may be used to modify the environment as well as query the environment. <a class="reference internal" href="#os.putenv" title="os.putenv"><code>putenv()</code></a> will be called automatically when the mapping is modified.</p> <p>On Unix, keys and values use <a class="reference internal" href="sys#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code>sys.getfilesystemencoding()</code></a> and <code>'surrogateescape'</code> error handler. Use <a class="reference internal" href="#os.environb" title="os.environb"><code>environb</code></a> if you would like to use a different encoding.</p> <p>On Windows, the keys are converted to uppercase. This also applies when getting, setting, or deleting an item. For example, <code>environ['monty'] = 'python'</code> maps the key <code>'MONTY'</code> to the value <code>'python'</code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Calling <a class="reference internal" href="#os.putenv" title="os.putenv"><code>putenv()</code></a> directly does not change <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, so it’s better to modify <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On some platforms, including FreeBSD and macOS, setting <code>environ</code> may cause memory leaks. Refer to the system documentation for <code>putenv()</code>.</p> </div> <p>You can delete items in this mapping to unset environment variables. <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code>unsetenv()</code></a> will be called automatically when an item is deleted from <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, and when one of the <code>pop()</code> or <code>clear()</code> methods is called.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to support <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>’s merge (<code>|</code>) and update (<code>|=</code>) operators.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.environb">
<code>os.environb</code> </dt> <dd>
<p>Bytes version of <a class="reference internal" href="#os.environ" title="os.environ"><code>environ</code></a>: a <a class="reference internal" href="../glossary#term-mapping"><span class="xref std std-term">mapping</span></a> object where both keys and values are <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> objects representing the process environment. <a class="reference internal" href="#os.environ" title="os.environ"><code>environ</code></a> and <a class="reference internal" href="#os.environb" title="os.environb"><code>environb</code></a> are synchronized (modifying <a class="reference internal" href="#os.environb" title="os.environb"><code>environb</code></a> updates <a class="reference internal" href="#os.environ" title="os.environ"><code>environ</code></a>, and vice versa).</p> <p><a class="reference internal" href="#os.environb" title="os.environb"><code>environb</code></a> is only available if <a class="reference internal" href="#os.supports_bytes_environ" title="os.supports_bytes_environ"><code>supports_bytes_environ</code></a> is <code>True</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Updated to support <span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0584/"><strong>PEP 584</strong></a>’s merge (<code>|</code>) and update (<code>|=</code>) operators.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py"> <span class="sig-prename descclassname">os.</span><span class="sig-name descname">chdir</span><span class="sig-paren">(</span><em class="sig-param"><span class="n">path</span></em><span class="sig-paren">)</span>
</dt> <dt class="sig sig-object py"> <span class="sig-prename descclassname">os.</span><span class="sig-name descname">fchdir</span><span class="sig-paren">(</span><em class="sig-param"><span class="n">fd</span></em><span class="sig-paren">)</span>
</dt> <dt class="sig sig-object py"> <span class="sig-prename descclassname">os.</span><span class="sig-name descname">getcwd</span><span class="sig-paren">(</span><span class="sig-paren">)</span>
</dt> <dd>
<p>These functions are described in <a class="reference internal" href="#os-file-dir"><span class="std std-ref">Files and Directories</span></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fsencode">
<code>os.fsencode(filename)</code> </dt> <dd>
<p>Encode <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like</span></a> <em>filename</em> to the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>; return <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> unchanged.</p> <p><a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code>fsdecode()</code></a> is the reverse function.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Support added to accept objects implementing the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fsdecode">
<code>os.fsdecode(filename)</code> </dt> <dd>
<p>Decode the <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like</span></a> <em>filename</em> from the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>; return <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> unchanged.</p> <p><a class="reference internal" href="#os.fsencode" title="os.fsencode"><code>fsencode()</code></a> is the reverse function.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Support added to accept objects implementing the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>os.PathLike</code></a> interface.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fspath">
<code>os.fspath(path)</code> </dt> <dd>
<p>Return the file system representation of the path.</p> <p>If <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> or <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> is passed in, it is returned unchanged. Otherwise <a class="reference internal" href="#os.PathLike.__fspath__" title="os.PathLike.__fspath__"><code>__fspath__()</code></a> is called and its value is returned as long as it is a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> or <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object. In all other cases, <a class="reference internal" href="exceptions#TypeError" title="TypeError"><code>TypeError</code></a> is raised.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="os.PathLike">
<code>class os.PathLike</code> </dt> <dd>
<p>An <a class="reference internal" href="../glossary#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> for objects representing a file system path, e.g. <a class="reference internal" href="pathlib#pathlib.PurePath" title="pathlib.PurePath"><code>pathlib.PurePath</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> <dl class="py method"> <dt class="sig sig-object py" id="os.PathLike.__fspath__">
<code>abstractmethod __fspath__()</code> </dt> <dd>
<p>Return the file system path representation of the object.</p> <p>The method should only return a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> or <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> object, with the preference being for <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a>.</p> </dd>
</dl> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getenv">
<code>os.getenv(key, default=None)</code> </dt> <dd>
<p>Return the value of the environment variable <em>key</em> as a string if it exists, or <em>default</em> if it doesn’t. <em>key</em> is a string. Note that since <a class="reference internal" href="#os.getenv" title="os.getenv"><code>getenv()</code></a> uses <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, the mapping of <a class="reference internal" href="#os.getenv" title="os.getenv"><code>getenv()</code></a> is similarly also captured on import, and the function may not reflect future environment changes.</p> <p>On Unix, keys and values are decoded with <a class="reference internal" href="sys#sys.getfilesystemencoding" title="sys.getfilesystemencoding"><code>sys.getfilesystemencoding()</code></a> and <code>'surrogateescape'</code> error handler. Use <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code>os.getenvb()</code></a> if you would like to use a different encoding.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getenvb">
<code>os.getenvb(key, default=None)</code> </dt> <dd>
<p>Return the value of the environment variable <em>key</em> as bytes if it exists, or <em>default</em> if it doesn’t. <em>key</em> must be bytes. Note that since <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code>getenvb()</code></a> uses <a class="reference internal" href="#os.environb" title="os.environb"><code>os.environb</code></a>, the mapping of <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code>getenvb()</code></a> is similarly also captured on import, and the function may not reflect future environment changes.</p> <p><a class="reference internal" href="#os.getenvb" title="os.getenvb"><code>getenvb()</code></a> is only available if <a class="reference internal" href="#os.supports_bytes_environ" title="os.supports_bytes_environ"><code>supports_bytes_environ</code></a> is <code>True</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.get_exec_path">
<code>os.get_exec_path(env=None)</code> </dt> <dd>
<p>Returns the list of directories that will be searched for a named executable, similar to a shell, when launching a process. <em>env</em>, when specified, should be an environment variable dictionary to lookup the PATH in. By default, when <em>env</em> is <code>None</code>, <a class="reference internal" href="#os.environ" title="os.environ"><code>environ</code></a> is used.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getegid">
<code>os.getegid()</code> </dt> <dd>
<p>Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.geteuid">
<code>os.geteuid()</code> </dt> <dd>
<p id="index-8">Return the current process’s effective user id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getgid">
<code>os.getgid()</code> </dt> <dd>
<p id="index-9">Return the real group id of the current process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> <p>The function is a stub on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getgrouplist">
<code>os.getgrouplist(user, group, /)</code> </dt> <dd>
<p>Return list of group ids that <em>user</em> belongs to. If <em>group</em> is not in the list, it is included; typically, <em>group</em> is specified as the group ID field from the password record for <em>user</em>, because that group ID will otherwise be potentially omitted.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getgroups">
<code>os.getgroups()</code> </dt> <dd>
<p>Return list of supplemental group ids associated with the current process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On macOS, <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code>getgroups()</code></a> behavior differs somewhat from other Unix platforms. If the Python interpreter was built with a deployment target of <code>10.5</code> or earlier, <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code>getgroups()</code></a> returns the list of effective group ids associated with the current user process; this list is limited to a system-defined number of entries, typically 16, and may be modified by calls to <a class="reference internal" href="#os.setgroups" title="os.setgroups"><code>setgroups()</code></a> if suitably privileged. If built with a deployment target greater than <code>10.5</code>, <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code>getgroups()</code></a> returns the current group access list for the user associated with the effective user id of the process; the group access list may change over the lifetime of the process, it is not affected by calls to <a class="reference internal" href="#os.setgroups" title="os.setgroups"><code>setgroups()</code></a>, and its length is not limited to 16. The deployment target value, <code>MACOSX_DEPLOYMENT_TARGET</code>, can be obtained with <a class="reference internal" href="sysconfig#sysconfig.get_config_var" title="sysconfig.get_config_var"><code>sysconfig.get_config_var()</code></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getlogin">
<code>os.getlogin()</code> </dt> <dd>
<p>Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use <a class="reference internal" href="getpass#getpass.getuser" title="getpass.getuser"><code>getpass.getuser()</code></a> since the latter checks the environment variables <span class="target" id="index-10"></span><code>LOGNAME</code> or <span class="target" id="index-11"></span><code>USERNAME</code> to find out who the user is, and falls back to <code>pwd.getpwuid(os.getuid())[0]</code> to get the login name of the current real user id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getpgid">
<code>os.getpgid(pid)</code> </dt> <dd>
<p>Return the process group id of the process with process id <em>pid</em>. If <em>pid</em> is 0, the process group id of the current process is returned.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getpgrp">
<code>os.getpgrp()</code> </dt> <dd>
<p id="index-12">Return the id of the current process group.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getpid">
<code>os.getpid()</code> </dt> <dd>
<p id="index-13">Return the current process id.</p> <p>The function is a stub on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getppid">
<code>os.getppid()</code> </dt> <dd>
<p id="index-14">Return the parent’s process id. When the parent process has exited, on Unix the id returned is the one of the init process (1), on Windows it is still the same id, which may be already reused by another process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getpriority">
<code>os.getpriority(which, who)</code> </dt> <dd>
<p id="index-15">Get program scheduling priority. The value <em>which</em> is one of <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code>PRIO_PROCESS</code></a>, <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code>PRIO_PGRP</code></a>, or <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code>PRIO_USER</code></a>, and <em>who</em> is interpreted relative to <em>which</em> (a process identifier for <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code>PRIO_PROCESS</code></a>, process group identifier for <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code>PRIO_PGRP</code></a>, and a user ID for <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code>PRIO_USER</code></a>). A zero value for <em>who</em> denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.PRIO_PROCESS">
<code>os.PRIO_PROCESS</code> </dt> <dt class="sig sig-object py" id="os.PRIO_PGRP">
<code>os.PRIO_PGRP</code> </dt> <dt class="sig sig-object py" id="os.PRIO_USER">
<code>os.PRIO_USER</code> </dt> <dd>
<p>Parameters for the <a class="reference internal" href="#os.getpriority" title="os.getpriority"><code>getpriority()</code></a> and <a class="reference internal" href="#os.setpriority" title="os.setpriority"><code>setpriority()</code></a> functions.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.PRIO_DARWIN_THREAD">
<code>os.PRIO_DARWIN_THREAD</code> </dt> <dt class="sig sig-object py" id="os.PRIO_DARWIN_PROCESS">
<code>os.PRIO_DARWIN_PROCESS</code> </dt> <dt class="sig sig-object py" id="os.PRIO_DARWIN_BG">
<code>os.PRIO_DARWIN_BG</code> </dt> <dt class="sig sig-object py" id="os.PRIO_DARWIN_NONUI">
<code>os.PRIO_DARWIN_NONUI</code> </dt> <dd>
<p>Parameters for the <a class="reference internal" href="#os.getpriority" title="os.getpriority"><code>getpriority()</code></a> and <a class="reference internal" href="#os.setpriority" title="os.setpriority"><code>setpriority()</code></a> functions.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: macOS</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getresuid">
<code>os.getresuid()</code> </dt> <dd>
<p>Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getresgid">
<code>os.getresgid()</code> </dt> <dd>
<p>Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved group ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getuid">
<code>os.getuid()</code> </dt> <dd>
<p id="index-16">Return the current process’s real user id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> <p>The function is a stub on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.initgroups">
<code>os.initgroups(username, gid, /)</code> </dt> <dd>
<p>Call the system initgroups() to initialize the group access list with all of the groups of which the specified username is a member, plus the specified group id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.putenv">
<code>os.putenv(key, value, /)</code> </dt> <dd>
<p id="index-17">Set the environment variable named <em>key</em> to the string <em>value</em>. Such changes to the environment affect subprocesses started with <a class="reference internal" href="#os.system" title="os.system"><code>os.system()</code></a>, <a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> or <a class="reference internal" href="#os.fork" title="os.fork"><code>fork()</code></a> and <a class="reference internal" href="#os.execv" title="os.execv"><code>execv()</code></a>.</p> <p>Assignments to items in <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a> are automatically translated into corresponding calls to <a class="reference internal" href="#os.putenv" title="os.putenv"><code>putenv()</code></a>; however, calls to <a class="reference internal" href="#os.putenv" title="os.putenv"><code>putenv()</code></a> don’t update <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, so it is actually preferable to assign to items of <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>. This also applies to <a class="reference internal" href="#os.getenv" title="os.getenv"><code>getenv()</code></a> and <a class="reference internal" href="#os.getenvb" title="os.getenvb"><code>getenvb()</code></a>, which respectively use <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a> and <a class="reference internal" href="#os.environb" title="os.environb"><code>os.environb</code></a> in their implementations.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On some platforms, including FreeBSD and macOS, setting <code>environ</code> may cause memory leaks. Refer to the system documentation for <code>putenv()</code>.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.putenv</code> with arguments <code>key</code>, <code>value</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>The function is now always available.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setegid">
<code>os.setegid(egid, /)</code> </dt> <dd>
<p>Set the current process’s effective group id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.seteuid">
<code>os.seteuid(euid, /)</code> </dt> <dd>
<p>Set the current process’s effective user id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setgid">
<code>os.setgid(gid, /)</code> </dt> <dd>
<p>Set the current process’ group id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setgroups">
<code>os.setgroups(groups, /)</code> </dt> <dd>
<p>Set the list of supplemental group ids associated with the current process to <em>groups</em>. <em>groups</em> must be a sequence, and each element must be an integer identifying a group. This operation is typically available only to the superuser.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On macOS, the length of <em>groups</em> may not exceed the system-defined maximum number of effective group ids, typically 16. See the documentation for <a class="reference internal" href="#os.getgroups" title="os.getgroups"><code>getgroups()</code></a> for cases where it may not return the same group list set by calling setgroups().</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setns">
<code>os.setns(fd, nstype=0)</code> </dt> <dd>
<p>Reassociate the current thread with a Linux namespace. See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/setns(2)">setns(2)</a></em> and <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/namespaces(7)">namespaces(7)</a></em> man pages for more details.</p> <p>If <em>fd</em> refers to a <code>/proc/<em>pid</em>/ns/</code> link, <code>setns()</code> reassociates the calling thread with the namespace associated with that link, and <em>nstype</em> may be set to one of the <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_NEW* constants</span></a> to impose constraints on the operation (<code>0</code> means no constraints).</p> <p>Since Linux 5.8, <em>fd</em> may refer to a PID file descriptor obtained from <a class="reference internal" href="#os.pidfd_open" title="os.pidfd_open"><code>pidfd_open()</code></a>. In this case, <code>setns()</code> reassociates the calling thread into one or more of the same namespaces as the thread referred to by <em>fd</em>. This is subject to any constraints imposed by <em>nstype</em>, which is a bit mask combining one or more of the <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_NEW* constants</span></a>, e.g. <code>setns(fd, os.CLONE_NEWUTS | os.CLONE_NEWPID)</code>. The caller’s memberships in unspecified namespaces are left unchanged.</p> <p><em>fd</em> can be any object with a <a class="reference internal" href="io#io.IOBase.fileno" title="io.IOBase.fileno"><code>fileno()</code></a> method, or a raw file descriptor.</p> <p>This example reassociates the thread with the <code>init</code> process’s network namespace:</p> <pre data-language="python">fd = os.open("/proc/1/ns/net", os.O_RDONLY)
os.setns(fd, os.CLONE_NEWNET)
os.close(fd)
</pre> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 3.0 with glibc >= 2.14.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="#os.unshare" title="os.unshare"><code>unshare()</code></a> function.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setpgrp">
<code>os.setpgrp()</code> </dt> <dd>
<p>Call the system call <code>setpgrp()</code> or <code>setpgrp(0, 0)</code> depending on which version is implemented (if any). See the Unix manual for the semantics.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setpgid">
<code>os.setpgid(pid, pgrp, /)</code> </dt> <dd>
<p>Call the system call <code>setpgid()</code> to set the process group id of the process with id <em>pid</em> to the process group with id <em>pgrp</em>. See the Unix manual for the semantics.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setpriority">
<code>os.setpriority(which, who, priority)</code> </dt> <dd>
<p id="index-18">Set program scheduling priority. The value <em>which</em> is one of <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code>PRIO_PROCESS</code></a>, <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code>PRIO_PGRP</code></a>, or <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code>PRIO_USER</code></a>, and <em>who</em> is interpreted relative to <em>which</em> (a process identifier for <a class="reference internal" href="#os.PRIO_PROCESS" title="os.PRIO_PROCESS"><code>PRIO_PROCESS</code></a>, process group identifier for <a class="reference internal" href="#os.PRIO_PGRP" title="os.PRIO_PGRP"><code>PRIO_PGRP</code></a>, and a user ID for <a class="reference internal" href="#os.PRIO_USER" title="os.PRIO_USER"><code>PRIO_USER</code></a>). A zero value for <em>who</em> denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process. <em>priority</em> is a value in the range -20 to 19. The default priority is 0; lower priorities cause more favorable scheduling.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setregid">
<code>os.setregid(rgid, egid, /)</code> </dt> <dd>
<p>Set the current process’s real and effective group ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setresgid">
<code>os.setresgid(rgid, egid, sgid, /)</code> </dt> <dd>
<p>Set the current process’s real, effective, and saved group ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setresuid">
<code>os.setresuid(ruid, euid, suid, /)</code> </dt> <dd>
<p>Set the current process’s real, effective, and saved user ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setreuid">
<code>os.setreuid(ruid, euid, /)</code> </dt> <dd>
<p>Set the current process’s real and effective user ids.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getsid">
<code>os.getsid(pid, /)</code> </dt> <dd>
<p>Call the system call <code>getsid()</code>. See the Unix manual for the semantics.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setsid">
<code>os.setsid()</code> </dt> <dd>
<p>Call the system call <code>setsid()</code>. See the Unix manual for the semantics.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setuid">
<code>os.setuid(uid, /)</code> </dt> <dd>
<p id="index-19">Set the current process’s user id.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.strerror">
<code>os.strerror(code, /)</code> </dt> <dd>
<p>Return the error message corresponding to the error code in <em>code</em>. On platforms where <code>strerror()</code> returns <code>NULL</code> when given an unknown error number, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.supports_bytes_environ">
<code>os.supports_bytes_environ</code> </dt> <dd>
<p><code>True</code> if the native OS type of the environment is bytes (eg. <code>False</code> on Windows).</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.umask">
<code>os.umask(mask, /)</code> </dt> <dd>
<p>Set the current numeric umask and return the previous umask.</p> <p>The function is a stub on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.uname">
<code>os.uname()</code> </dt> <dd>
<p id="index-20">Returns information identifying the current operating system. The return value is an object with five attributes:</p> <ul class="simple"> <li>
<code>sysname</code> - operating system name</li> <li>
<code>nodename</code> - name of machine on network (implementation-defined)</li> <li>
<code>release</code> - operating system release</li> <li>
<code>version</code> - operating system version</li> <li>
<code>machine</code> - hardware identifier</li> </ul> <p>For backwards compatibility, this object is also iterable, behaving like a five-tuple containing <code>sysname</code>, <code>nodename</code>, <code>release</code>, <code>version</code>, and <code>machine</code> in that order.</p> <p>Some systems truncate <code>nodename</code> to 8 characters or to the leading component; a better way to get the hostname is <a class="reference internal" href="socket#socket.gethostname" title="socket.gethostname"><code>socket.gethostname()</code></a> or even <code>socket.gethostbyaddr(socket.gethostname())</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Return type changed from a tuple to a tuple-like object with named attributes.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.unsetenv">
<code>os.unsetenv(key, /)</code> </dt> <dd>
<p id="index-21">Unset (delete) the environment variable named <em>key</em>. Such changes to the environment affect subprocesses started with <a class="reference internal" href="#os.system" title="os.system"><code>os.system()</code></a>, <a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> or <a class="reference internal" href="#os.fork" title="os.fork"><code>fork()</code></a> and <a class="reference internal" href="#os.execv" title="os.execv"><code>execv()</code></a>.</p> <p>Deletion of items in <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a> is automatically translated into a corresponding call to <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code>unsetenv()</code></a>; however, calls to <a class="reference internal" href="#os.unsetenv" title="os.unsetenv"><code>unsetenv()</code></a> don’t update <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>, so it is actually preferable to delete items of <a class="reference internal" href="#os.environ" title="os.environ"><code>os.environ</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.unsetenv</code> with argument <code>key</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>The function is now always available and is also available on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.unshare">
<code>os.unshare(flags)</code> </dt> <dd>
<p>Disassociate parts of the process execution context, and move them into a newly created namespace. See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/unshare(2)">unshare(2)</a></em> man page for more details. The <em>flags</em> argument is a bit mask, combining zero or more of the <a class="reference internal" href="#os-unshare-clone-flags"><span class="std std-ref">CLONE_* constants</span></a>, that specifies which parts of the execution context should be unshared from their existing associations and moved to a new namespace. If the <em>flags</em> argument is <code>0</code>, no changes are made to the calling process’s execution context.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.16.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="#os.setns" title="os.setns"><code>setns()</code></a> function.</p> </div> </dd>
</dl> <p id="os-unshare-clone-flags">Flags to the <a class="reference internal" href="#os.unshare" title="os.unshare"><code>unshare()</code></a> function, if the implementation supports them. See <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/unshare(2)">unshare(2)</a></em> in the Linux manual for their exact effect and availability.</p> <dl class="py data"> <dt class="sig sig-object py" id="os.CLONE_FILES">
<code>os.CLONE_FILES</code> </dt> <dt class="sig sig-object py" id="os.CLONE_FS">
<code>os.CLONE_FS</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWCGROUP">
<code>os.CLONE_NEWCGROUP</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWIPC">
<code>os.CLONE_NEWIPC</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWNET">
<code>os.CLONE_NEWNET</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWNS">
<code>os.CLONE_NEWNS</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWPID">
<code>os.CLONE_NEWPID</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWTIME">
<code>os.CLONE_NEWTIME</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWUSER">
<code>os.CLONE_NEWUSER</code> </dt> <dt class="sig sig-object py" id="os.CLONE_NEWUTS">
<code>os.CLONE_NEWUTS</code> </dt> <dt class="sig sig-object py" id="os.CLONE_SIGHAND">
<code>os.CLONE_SIGHAND</code> </dt> <dt class="sig sig-object py" id="os.CLONE_SYSVSEM">
<code>os.CLONE_SYSVSEM</code> </dt> <dt class="sig sig-object py" id="os.CLONE_THREAD">
<code>os.CLONE_THREAD</code> </dt> <dt class="sig sig-object py" id="os.CLONE_VM">
<code>os.CLONE_VM</code> </dt> <dd></dd>
</dl> </section> <section id="file-object-creation"> <span id="os-newstreams"></span><h2>File Object Creation</h2> <p>These functions create new <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file objects</span></a>. (See also <a class="reference internal" href="#os.open" title="os.open"><code>open()</code></a> for opening file descriptors.)</p> <dl class="py function"> <dt class="sig sig-object py" id="os.fdopen">
<code>os.fdopen(fd, *args, **kwargs)</code> </dt> <dd>
<p>Return an open file object connected to the file descriptor <em>fd</em>. This is an alias of the <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> built-in function and accepts the same arguments. The only difference is that the first argument of <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code>fdopen()</code></a> must always be an integer.</p> </dd>
</dl> </section> <section id="file-descriptor-operations"> <span id="os-fd-ops"></span><h2>File Descriptor Operations</h2> <p>These functions operate on I/O streams referenced using file descriptors.</p> <p>File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors.</p> <p>The <a class="reference internal" href="io#io.IOBase.fileno" title="io.IOBase.fileno"><code>fileno()</code></a> method can be used to obtain the file descriptor associated with a <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file object</span></a> when required. Note that using the file descriptor directly will bypass the file object methods, ignoring aspects such as internal buffering of data.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.close">
<code>os.close(fd)</code> </dt> <dd>
<p>Close file descriptor <em>fd</em>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is intended for low-level I/O and must be applied to a file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code>pipe()</code></a>. To close a “file object” returned by the built-in function <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> or by <a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> or <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code>fdopen()</code></a>, use its <a class="reference internal" href="io#io.IOBase.close" title="io.IOBase.close"><code>close()</code></a> method.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.closerange">
<code>os.closerange(fd_low, fd_high, /)</code> </dt> <dd>
<p>Close all file descriptors from <em>fd_low</em> (inclusive) to <em>fd_high</em> (exclusive), ignoring errors. Equivalent to (but much faster than):</p> <pre data-language="python">for fd in range(fd_low, fd_high):
try:
os.close(fd)
except OSError:
pass
</pre> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.copy_file_range">
<code>os.copy_file_range(src, dst, count, offset_src=None, offset_dst=None)</code> </dt> <dd>
<p>Copy <em>count</em> bytes from file descriptor <em>src</em>, starting from offset <em>offset_src</em>, to file descriptor <em>dst</em>, starting from offset <em>offset_dst</em>. If <em>offset_src</em> is None, then <em>src</em> is read from the current position; respectively for <em>offset_dst</em>.</p> <p>In Linux kernel older than 5.3, the files pointed by <em>src</em> and <em>dst</em> must reside in the same filesystem, otherwise an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised with <a class="reference internal" href="exceptions#OSError.errno" title="OSError.errno"><code>errno</code></a> set to <a class="reference internal" href="errno#errno.EXDEV" title="errno.EXDEV"><code>errno.EXDEV</code></a>.</p> <p>This copy is done without the additional cost of transferring data from the kernel to user space and then back into the kernel. Additionally, some filesystems could implement extra optimizations, such as the use of reflinks (i.e., two or more inodes that share pointers to the same copy-on-write disk blocks; supported file systems include btrfs and XFS) and server-side copy (in the case of NFS).</p> <p>The function copies bytes between two file descriptors. Text options, like the encoding and the line ending, are ignored.</p> <p>The return value is the amount of bytes copied. This could be less than the amount requested.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On Linux, <a class="reference internal" href="#os.copy_file_range" title="os.copy_file_range"><code>os.copy_file_range()</code></a> should not be used for copying a range of a pseudo file from a special filesystem like procfs and sysfs. It will always copy no bytes and return 0 as if the file was empty because of a known Linux kernel issue.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.5 with glibc >= 2.27.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.device_encoding">
<code>os.device_encoding(fd)</code> </dt> <dd>
<p>Return a string describing the encoding of the device associated with <em>fd</em> if it is connected to a terminal; else return <a class="reference internal" href="constants#None" title="None"><code>None</code></a>.</p> <p>On Unix, if the <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> is enabled, return <code>'UTF-8'</code> rather than the device encoding.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>On Unix, the function now implements the Python UTF-8 Mode.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.dup">
<code>os.dup(fd, /)</code> </dt> <dd>
<p>Return a duplicate of file descriptor <em>fd</em>. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p> <p>On Windows, when duplicating a standard stream (0: stdin, 1: stdout, 2: stderr), the new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">inheritable</span></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptor is now non-inheritable.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.dup2">
<code>os.dup2(fd, fd2, inheritable=True)</code> </dt> <dd>
<p>Duplicate file descriptor <em>fd</em> to <em>fd2</em>, closing the latter first if necessary. Return <em>fd2</em>. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">inheritable</span></a> by default or non-inheritable if <em>inheritable</em> is <code>False</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Add the optional <em>inheritable</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Return <em>fd2</em> on success. Previously, <code>None</code> was always returned.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fchmod">
<code>os.fchmod(fd, mode)</code> </dt> <dd>
<p>Change the mode of the file given by <em>fd</em> to the numeric <em>mode</em>. See the docs for <a class="reference internal" href="#os.chmod" title="os.chmod"><code>chmod()</code></a> for possible values of <em>mode</em>. As of Python 3.3, this is equivalent to <code>os.chmod(fd, mode)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chmod</code> with arguments <code>path</code>, <code>mode</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fchown">
<code>os.fchown(fd, uid, gid)</code> </dt> <dd>
<p>Change the owner and group id of the file given by <em>fd</em> to the numeric <em>uid</em> and <em>gid</em>. To leave one of the ids unchanged, set it to -1. See <a class="reference internal" href="#os.chown" title="os.chown"><code>chown()</code></a>. As of Python 3.3, this is equivalent to <code>os.chown(fd, uid,
gid)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chown</code> with arguments <code>path</code>, <code>uid</code>, <code>gid</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fdatasync">
<code>os.fdatasync(fd)</code> </dt> <dd>
<p>Force write of file with filedescriptor <em>fd</em> to disk. Does not force update of metadata.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is not available on MacOS.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fpathconf">
<code>os.fpathconf(fd, name, /)</code> </dt> <dd>
<p>Return system configuration information relevant to an open file. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given in the <code>pathconf_names</code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code>pathconf_names</code>, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised with <a class="reference internal" href="errno#errno.EINVAL" title="errno.EINVAL"><code>errno.EINVAL</code></a> for the error number.</p> <p>As of Python 3.3, this is equivalent to <code>os.pathconf(fd, name)</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fstat">
<code>os.fstat(fd)</code> </dt> <dd>
<p>Get the status of the file descriptor <em>fd</em>. Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> object.</p> <p>As of Python 3.3, this is equivalent to <code>os.stat(fd)</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a> function.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fstatvfs">
<code>os.fstatvfs(fd, /)</code> </dt> <dd>
<p>Return information about the filesystem containing the file associated with file descriptor <em>fd</em>, like <a class="reference internal" href="#os.statvfs" title="os.statvfs"><code>statvfs()</code></a>. As of Python 3.3, this is equivalent to <code>os.statvfs(fd)</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fsync">
<code>os.fsync(fd)</code> </dt> <dd>
<p>Force write of file with filedescriptor <em>fd</em> to disk. On Unix, this calls the native <code>fsync()</code> function; on Windows, the MS <code>_commit()</code> function.</p> <p>If you’re starting with a buffered Python <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em>, first do <code>f.flush()</code>, and then do <code>os.fsync(f.fileno())</code>, to ensure that all internal buffers associated with <em>f</em> are written to disk.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.ftruncate">
<code>os.ftruncate(fd, length, /)</code> </dt> <dd>
<p>Truncate the file corresponding to file descriptor <em>fd</em>, so that it is at most <em>length</em> bytes in size. As of Python 3.3, this is equivalent to <code>os.truncate(fd, length)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.truncate</code> with arguments <code>fd</code>, <code>length</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for Windows</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.get_blocking">
<code>os.get_blocking(fd, /)</code> </dt> <dd>
<p>Get the blocking mode of the file descriptor: <code>False</code> if the <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code>O_NONBLOCK</code></a> flag is set, <code>True</code> if the flag is cleared.</p> <p>See also <a class="reference internal" href="#os.set_blocking" title="os.set_blocking"><code>set_blocking()</code></a> and <a class="reference internal" href="socket#socket.socket.setblocking" title="socket.socket.setblocking"><code>socket.socket.setblocking()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> <p>On Windows, this function is limited to pipes.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for pipes on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.isatty">
<code>os.isatty(fd, /)</code> </dt> <dd>
<p>Return <code>True</code> if the file descriptor <em>fd</em> is open and connected to a tty(-like) device, else <code>False</code>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lockf">
<code>os.lockf(fd, cmd, len, /)</code> </dt> <dd>
<p>Apply, test or remove a POSIX lock on an open file descriptor. <em>fd</em> is an open file descriptor. <em>cmd</em> specifies the command to use - one of <a class="reference internal" href="#os.F_LOCK" title="os.F_LOCK"><code>F_LOCK</code></a>, <a class="reference internal" href="#os.F_TLOCK" title="os.F_TLOCK"><code>F_TLOCK</code></a>, <a class="reference internal" href="#os.F_ULOCK" title="os.F_ULOCK"><code>F_ULOCK</code></a> or <a class="reference internal" href="#os.F_TEST" title="os.F_TEST"><code>F_TEST</code></a>. <em>len</em> specifies the section of the file to lock.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.lockf</code> with arguments <code>fd</code>, <code>cmd</code>, <code>len</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.F_LOCK">
<code>os.F_LOCK</code> </dt> <dt class="sig sig-object py" id="os.F_TLOCK">
<code>os.F_TLOCK</code> </dt> <dt class="sig sig-object py" id="os.F_ULOCK">
<code>os.F_ULOCK</code> </dt> <dt class="sig sig-object py" id="os.F_TEST">
<code>os.F_TEST</code> </dt> <dd>
<p>Flags that specify what action <a class="reference internal" href="#os.lockf" title="os.lockf"><code>lockf()</code></a> will take.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.login_tty">
<code>os.login_tty(fd, /)</code> </dt> <dd>
<p>Prepare the tty of which fd is a file descriptor for a new login session. Make the calling process a session leader; make the tty the controlling tty, the stdin, the stdout, and the stderr of the calling process; close fd.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lseek">
<code>os.lseek(fd, pos, whence, /)</code> </dt> <dd>
<p>Set the current position of file descriptor <em>fd</em> to position <em>pos</em>, modified by <em>whence</em>, and return the new position in bytes relative to the start of the file. Valid values for <em>whence</em> are:</p> <ul class="simple"> <li>
<a class="reference internal" href="#os.SEEK_SET" title="os.SEEK_SET"><code>SEEK_SET</code></a> or <code>0</code> – set <em>pos</em> relative to the beginning of the file</li> <li>
<a class="reference internal" href="#os.SEEK_CUR" title="os.SEEK_CUR"><code>SEEK_CUR</code></a> or <code>1</code> – set <em>pos</em> relative to the current file position</li> <li>
<a class="reference internal" href="#os.SEEK_END" title="os.SEEK_END"><code>SEEK_END</code></a> or <code>2</code> – set <em>pos</em> relative to the end of the file</li> <li>
<a class="reference internal" href="#os.SEEK_HOLE" title="os.SEEK_HOLE"><code>SEEK_HOLE</code></a> – set <em>pos</em> to the next data location, relative to <em>pos</em>
</li> <li>
<a class="reference internal" href="#os.SEEK_DATA" title="os.SEEK_DATA"><code>SEEK_DATA</code></a> – set <em>pos</em> to the next data hole, relative to <em>pos</em>
</li> </ul> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Add support for <code>SEEK_HOLE</code> and <code>SEEK_DATA</code>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SEEK_SET">
<code>os.SEEK_SET</code> </dt> <dt class="sig sig-object py" id="os.SEEK_CUR">
<code>os.SEEK_CUR</code> </dt> <dt class="sig sig-object py" id="os.SEEK_END">
<code>os.SEEK_END</code> </dt> <dd>
<p>Parameters to the <a class="reference internal" href="#os.lseek" title="os.lseek"><code>lseek()</code></a> function and the <a class="reference internal" href="io#io.IOBase.seek" title="io.IOBase.seek"><code>seek()</code></a> method on <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file-like objects</span></a>, for whence to adjust the file position indicator.</p> <dl class="simple"> <dt>
<code></code> <a class="reference internal" href="#os.SEEK_SET" title="os.SEEK_SET"><code>SEEK_SET</code></a>
</dt>
<dd>
<p>Adjust the file position relative to the beginning of the file.</p> </dd> <dt>
<code></code> <a class="reference internal" href="#os.SEEK_CUR" title="os.SEEK_CUR"><code>SEEK_CUR</code></a>
</dt>
<dd>
<p>Adjust the file position relative to the current file position.</p> </dd> <dt>
<code></code> <a class="reference internal" href="#os.SEEK_END" title="os.SEEK_END"><code>SEEK_END</code></a>
</dt>
<dd>
<p>Adjust the file position relative to the end of the file.</p> </dd> </dl> <p>Their values are 0, 1, and 2, respectively.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SEEK_HOLE">
<code>os.SEEK_HOLE</code> </dt> <dt class="sig sig-object py" id="os.SEEK_DATA">
<code>os.SEEK_DATA</code> </dt> <dd>
<p>Parameters to the <a class="reference internal" href="#os.lseek" title="os.lseek"><code>lseek()</code></a> function and the <a class="reference internal" href="io#io.IOBase.seek" title="io.IOBase.seek"><code>seek()</code></a> method on <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file-like objects</span></a>, for seeking file data and holes on sparsely allocated files.</p> <dl class="simple"> <dt>
<code>SEEK_DATA</code> </dt>
<dd>
<p>Adjust the file offset to the next location containing data, relative to the seek position.</p> </dd> <dt>
<code>SEEK_HOLE</code> </dt>
<dd>
<p>Adjust the file offset to the next location containing a hole, relative to the seek position. A hole is defined as a sequence of zeros.</p> </dd> </dl> <div class="admonition note"> <p class="admonition-title">Note</p> <p>These operations only make sense for filesystems that support them.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 3.1, macOS, Unix</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.open">
<code>os.open(path, flags, mode=0o777, *, dir_fd=None)</code> </dt> <dd>
<p>Open the file <em>path</em> and set various flags according to <em>flags</em> and possibly its mode according to <em>mode</em>. When computing <em>mode</em>, the current umask value is first masked out. Return the file descriptor for the newly opened file. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p> <p>For a description of the flag and mode values, see the C run-time documentation; flag constants (like <a class="reference internal" href="#os.O_RDONLY" title="os.O_RDONLY"><code>O_RDONLY</code></a> and <a class="reference internal" href="#os.O_WRONLY" title="os.O_WRONLY"><code>O_WRONLY</code></a>) are defined in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module. In particular, on Windows adding <a class="reference internal" href="#os.O_BINARY" title="os.O_BINARY"><code>O_BINARY</code></a> is needed to open files in binary mode.</p> <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> with the <em>dir_fd</em> parameter.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>open</code> with arguments <code>path</code>, <code>mode</code>, <code>flags</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptor is now non-inheritable.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is intended for low-level I/O. For normal usage, use the built-in function <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>, which returns a <a class="reference internal" href="../glossary#term-file-object"><span class="xref std std-term">file object</span></a> with <code>read()</code> and <code>write()</code> methods (and many more). To wrap a file descriptor in a file object, use <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code>fdopen()</code></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an <a class="reference internal" href="exceptions#InterruptedError" title="InterruptedError"><code>InterruptedError</code></a> exception (see <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <p>The following constants are options for the <em>flags</em> parameter to the <a class="reference internal" href="#os.open" title="os.open"><code>open()</code></a> function. They can be combined using the bitwise OR operator <code>|</code>. Some of them are not available on all platforms. For descriptions of their availability and use, consult the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/open(2)">open(2)</a></em> manual page on Unix or <a class="reference external" href="https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx">the MSDN</a> on Windows.</p> <dl class="py data"> <dt class="sig sig-object py" id="os.O_RDONLY">
<code>os.O_RDONLY</code> </dt> <dt class="sig sig-object py" id="os.O_WRONLY">
<code>os.O_WRONLY</code> </dt> <dt class="sig sig-object py" id="os.O_RDWR">
<code>os.O_RDWR</code> </dt> <dt class="sig sig-object py" id="os.O_APPEND">
<code>os.O_APPEND</code> </dt> <dt class="sig sig-object py" id="os.O_CREAT">
<code>os.O_CREAT</code> </dt> <dt class="sig sig-object py" id="os.O_EXCL">
<code>os.O_EXCL</code> </dt> <dt class="sig sig-object py" id="os.O_TRUNC">
<code>os.O_TRUNC</code> </dt> <dd>
<p>The above constants are available on Unix and Windows.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.O_DSYNC">
<code>os.O_DSYNC</code> </dt> <dt class="sig sig-object py" id="os.O_RSYNC">
<code>os.O_RSYNC</code> </dt> <dt class="sig sig-object py" id="os.O_SYNC">
<code>os.O_SYNC</code> </dt> <dt class="sig sig-object py" id="os.O_NDELAY">
<code>os.O_NDELAY</code> </dt> <dt class="sig sig-object py" id="os.O_NONBLOCK">
<code>os.O_NONBLOCK</code> </dt> <dt class="sig sig-object py" id="os.O_NOCTTY">
<code>os.O_NOCTTY</code> </dt> <dt class="sig sig-object py" id="os.O_CLOEXEC">
<code>os.O_CLOEXEC</code> </dt> <dd>
<p>The above constants are only available on Unix.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Add <a class="reference internal" href="#os.O_CLOEXEC" title="os.O_CLOEXEC"><code>O_CLOEXEC</code></a> constant.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.O_BINARY">
<code>os.O_BINARY</code> </dt> <dt class="sig sig-object py" id="os.O_NOINHERIT">
<code>os.O_NOINHERIT</code> </dt> <dt class="sig sig-object py" id="os.O_SHORT_LIVED">
<code>os.O_SHORT_LIVED</code> </dt> <dt class="sig sig-object py" id="os.O_TEMPORARY">
<code>os.O_TEMPORARY</code> </dt> <dt class="sig sig-object py" id="os.O_RANDOM">
<code>os.O_RANDOM</code> </dt> <dt class="sig sig-object py" id="os.O_SEQUENTIAL">
<code>os.O_SEQUENTIAL</code> </dt> <dt class="sig sig-object py" id="os.O_TEXT">
<code>os.O_TEXT</code> </dt> <dd>
<p>The above constants are only available on Windows.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.O_EVTONLY">
<code>os.O_EVTONLY</code> </dt> <dt class="sig sig-object py" id="os.O_FSYNC">
<code>os.O_FSYNC</code> </dt> <dt class="sig sig-object py" id="os.O_SYMLINK">
<code>os.O_SYMLINK</code> </dt> <dt class="sig sig-object py" id="os.O_NOFOLLOW_ANY">
<code>os.O_NOFOLLOW_ANY</code> </dt> <dd>
<p>The above constants are only available on macOS.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Add <a class="reference internal" href="#os.O_EVTONLY" title="os.O_EVTONLY"><code>O_EVTONLY</code></a>, <a class="reference internal" href="#os.O_FSYNC" title="os.O_FSYNC"><code>O_FSYNC</code></a>, <a class="reference internal" href="#os.O_SYMLINK" title="os.O_SYMLINK"><code>O_SYMLINK</code></a> and <a class="reference internal" href="#os.O_NOFOLLOW_ANY" title="os.O_NOFOLLOW_ANY"><code>O_NOFOLLOW_ANY</code></a> constants.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.O_ASYNC">
<code>os.O_ASYNC</code> </dt> <dt class="sig sig-object py" id="os.O_DIRECT">
<code>os.O_DIRECT</code> </dt> <dt class="sig sig-object py" id="os.O_DIRECTORY">
<code>os.O_DIRECTORY</code> </dt> <dt class="sig sig-object py" id="os.O_NOFOLLOW">
<code>os.O_NOFOLLOW</code> </dt> <dt class="sig sig-object py" id="os.O_NOATIME">
<code>os.O_NOATIME</code> </dt> <dt class="sig sig-object py" id="os.O_PATH">
<code>os.O_PATH</code> </dt> <dt class="sig sig-object py" id="os.O_TMPFILE">
<code>os.O_TMPFILE</code> </dt> <dt class="sig sig-object py" id="os.O_SHLOCK">
<code>os.O_SHLOCK</code> </dt> <dt class="sig sig-object py" id="os.O_EXLOCK">
<code>os.O_EXLOCK</code> </dt> <dd>
<p>The above constants are extensions and not present if they are not defined by the C library.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Add <a class="reference internal" href="#os.O_PATH" title="os.O_PATH"><code>O_PATH</code></a> on systems that support it. Add <a class="reference internal" href="#os.O_TMPFILE" title="os.O_TMPFILE"><code>O_TMPFILE</code></a>, only available on Linux Kernel 3.11 or newer.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.openpty">
<code>os.openpty()</code> </dt> <dd>
<p id="index-23">Open a new pseudo-terminal pair. Return a pair of file descriptors <code>(master, slave)</code> for the pty and the tty, respectively. The new file descriptors are <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>. For a (slightly) more portable approach, use the <a class="reference internal" href="pty#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code>pty</code></a> module.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptors are now non-inheritable.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pipe">
<code>os.pipe()</code> </dt> <dd>
<p>Create a pipe. Return a pair of file descriptors <code>(r, w)</code> usable for reading and writing, respectively. The new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The new file descriptors are now non-inheritable.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pipe2">
<code>os.pipe2(flags, /)</code> </dt> <dd>
<p>Create a pipe with <em>flags</em> set atomically. <em>flags</em> can be constructed by ORing together one or more of these values: <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code>O_NONBLOCK</code></a>, <a class="reference internal" href="#os.O_CLOEXEC" title="os.O_CLOEXEC"><code>O_CLOEXEC</code></a>. Return a pair of file descriptors <code>(r, w)</code> usable for reading and writing, respectively.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.posix_fallocate">
<code>os.posix_fallocate(fd, offset, len, /)</code> </dt> <dd>
<p>Ensures that enough disk space is allocated for the file specified by <em>fd</em> starting from <em>offset</em> and continuing for <em>len</em> bytes.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.posix_fadvise">
<code>os.posix_fadvise(fd, offset, len, advice, /)</code> </dt> <dd>
<p>Announces an intention to access data in a specific pattern thus allowing the kernel to make optimizations. The advice applies to the region of the file specified by <em>fd</em> starting at <em>offset</em> and continuing for <em>len</em> bytes. <em>advice</em> is one of <a class="reference internal" href="#os.POSIX_FADV_NORMAL" title="os.POSIX_FADV_NORMAL"><code>POSIX_FADV_NORMAL</code></a>, <a class="reference internal" href="#os.POSIX_FADV_SEQUENTIAL" title="os.POSIX_FADV_SEQUENTIAL"><code>POSIX_FADV_SEQUENTIAL</code></a>, <a class="reference internal" href="#os.POSIX_FADV_RANDOM" title="os.POSIX_FADV_RANDOM"><code>POSIX_FADV_RANDOM</code></a>, <a class="reference internal" href="#os.POSIX_FADV_NOREUSE" title="os.POSIX_FADV_NOREUSE"><code>POSIX_FADV_NOREUSE</code></a>, <a class="reference internal" href="#os.POSIX_FADV_WILLNEED" title="os.POSIX_FADV_WILLNEED"><code>POSIX_FADV_WILLNEED</code></a> or <a class="reference internal" href="#os.POSIX_FADV_DONTNEED" title="os.POSIX_FADV_DONTNEED"><code>POSIX_FADV_DONTNEED</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.POSIX_FADV_NORMAL">
<code>os.POSIX_FADV_NORMAL</code> </dt> <dt class="sig sig-object py" id="os.POSIX_FADV_SEQUENTIAL">
<code>os.POSIX_FADV_SEQUENTIAL</code> </dt> <dt class="sig sig-object py" id="os.POSIX_FADV_RANDOM">
<code>os.POSIX_FADV_RANDOM</code> </dt> <dt class="sig sig-object py" id="os.POSIX_FADV_NOREUSE">
<code>os.POSIX_FADV_NOREUSE</code> </dt> <dt class="sig sig-object py" id="os.POSIX_FADV_WILLNEED">
<code>os.POSIX_FADV_WILLNEED</code> </dt> <dt class="sig sig-object py" id="os.POSIX_FADV_DONTNEED">
<code>os.POSIX_FADV_DONTNEED</code> </dt> <dd>
<p>Flags that can be used in <em>advice</em> in <a class="reference internal" href="#os.posix_fadvise" title="os.posix_fadvise"><code>posix_fadvise()</code></a> that specify the access pattern that is likely to be used.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pread">
<code>os.pread(fd, n, offset, /)</code> </dt> <dd>
<p>Read at most <em>n</em> bytes from file descriptor <em>fd</em> at a position of <em>offset</em>, leaving the file offset unchanged.</p> <p>Return a bytestring containing the bytes read. If the end of the file referred to by <em>fd</em> has been reached, an empty bytes object is returned.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.preadv">
<code>os.preadv(fd, buffers, offset, flags=0, /)</code> </dt> <dd>
<p>Read from a file descriptor <em>fd</em> at a position of <em>offset</em> into mutable <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> <em>buffers</em>, leaving the file offset unchanged. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data.</p> <p>The flags argument contains a bitwise OR of zero or more of the following flags:</p> <ul class="simple"> <li><a class="reference internal" href="#os.RWF_HIPRI" title="os.RWF_HIPRI"><code>RWF_HIPRI</code></a></li> <li><a class="reference internal" href="#os.RWF_NOWAIT" title="os.RWF_NOWAIT"><code>RWF_NOWAIT</code></a></li> </ul> <p>Return the total number of bytes actually read which can be less than the total capacity of all the objects.</p> <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code>sysconf()</code></a> value <code>'SC_IOV_MAX'</code>) on the number of buffers that can be used.</p> <p>Combine the functionality of <a class="reference internal" href="#os.readv" title="os.readv"><code>os.readv()</code></a> and <a class="reference internal" href="#os.pread" title="os.pread"><code>os.pread()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.30, FreeBSD >= 6.0, OpenBSD >= 2.7, AIX >= 7.1.</p> <p>Using flags requires Linux >= 4.6.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RWF_NOWAIT">
<code>os.RWF_NOWAIT</code> </dt> <dd>
<p>Do not wait for data which is not immediately available. If this flag is specified, the system call will return instantly if it would have to read data from the backing storage or wait for a lock.</p> <p>If some data was successfully read, it will return the number of bytes read. If no bytes were read, it will return <code>-1</code> and set errno to <a class="reference internal" href="errno#errno.EAGAIN" title="errno.EAGAIN"><code>errno.EAGAIN</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.14.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RWF_HIPRI">
<code>os.RWF_HIPRI</code> </dt> <dd>
<p>High priority read/write. Allows block-based filesystems to use polling of the device, which provides lower latency, but may use additional resources.</p> <p>Currently, on Linux, this feature is usable only on a file descriptor opened using the <a class="reference internal" href="#os.O_DIRECT" title="os.O_DIRECT"><code>O_DIRECT</code></a> flag.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.6.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pwrite">
<code>os.pwrite(fd, str, offset, /)</code> </dt> <dd>
<p>Write the bytestring in <em>str</em> to file descriptor <em>fd</em> at position of <em>offset</em>, leaving the file offset unchanged.</p> <p>Return the number of bytes actually written.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pwritev">
<code>os.pwritev(fd, buffers, offset, flags=0, /)</code> </dt> <dd>
<p>Write the <em>buffers</em> contents to file descriptor <em>fd</em> at a offset <em>offset</em>, leaving the file offset unchanged. <em>buffers</em> must be a sequence of <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. Buffers are processed in array order. Entire contents of the first buffer is written before proceeding to the second, and so on.</p> <p>The flags argument contains a bitwise OR of zero or more of the following flags:</p> <ul class="simple"> <li><a class="reference internal" href="#os.RWF_DSYNC" title="os.RWF_DSYNC"><code>RWF_DSYNC</code></a></li> <li><a class="reference internal" href="#os.RWF_SYNC" title="os.RWF_SYNC"><code>RWF_SYNC</code></a></li> <li><a class="reference internal" href="#os.RWF_APPEND" title="os.RWF_APPEND"><code>RWF_APPEND</code></a></li> </ul> <p>Return the total number of bytes actually written.</p> <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code>sysconf()</code></a> value <code>'SC_IOV_MAX'</code>) on the number of buffers that can be used.</p> <p>Combine the functionality of <a class="reference internal" href="#os.writev" title="os.writev"><code>os.writev()</code></a> and <a class="reference internal" href="#os.pwrite" title="os.pwrite"><code>os.pwrite()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.30, FreeBSD >= 6.0, OpenBSD >= 2.7, AIX >= 7.1.</p> <p>Using flags requires Linux >= 4.6.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RWF_DSYNC">
<code>os.RWF_DSYNC</code> </dt> <dd>
<p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_DSYNC" title="os.O_DSYNC"><code>O_DSYNC</code></a> <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> flag. This flag effect applies only to the data range written by the system call.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.7.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RWF_SYNC">
<code>os.RWF_SYNC</code> </dt> <dd>
<p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_SYNC" title="os.O_SYNC"><code>O_SYNC</code></a> <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> flag. This flag effect applies only to the data range written by the system call.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.7.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RWF_APPEND">
<code>os.RWF_APPEND</code> </dt> <dd>
<p>Provide a per-write equivalent of the <a class="reference internal" href="#os.O_APPEND" title="os.O_APPEND"><code>O_APPEND</code></a> <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> flag. This flag is meaningful only for <a class="reference internal" href="#os.pwritev" title="os.pwritev"><code>os.pwritev()</code></a>, and its effect applies only to the data range written by the system call. The <em>offset</em> argument does not affect the write operation; the data is always appended to the end of the file. However, if the <em>offset</em> argument is <code>-1</code>, the current file <em>offset</em> is updated.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 4.16.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.read">
<code>os.read(fd, n, /)</code> </dt> <dd>
<p>Read at most <em>n</em> bytes from file descriptor <em>fd</em>.</p> <p>Return a bytestring containing the bytes read. If the end of the file referred to by <em>fd</em> has been reached, an empty bytes object is returned.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is intended for low-level I/O and must be applied to a file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code>pipe()</code></a>. To read a “file object” returned by the built-in function <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> or by <a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> or <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code>fdopen()</code></a>, or <a class="reference internal" href="sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a>, use its <code>read()</code> or <code>readline()</code> methods.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an <a class="reference internal" href="exceptions#InterruptedError" title="InterruptedError"><code>InterruptedError</code></a> exception (see <span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sendfile">
<code>os.sendfile(out_fd, in_fd, offset, count)</code> </dt> <dt class="sig sig-object py"> <span class="sig-prename descclassname">os.</span><span class="sig-name descname">sendfile</span><span class="sig-paren">(</span><em class="sig-param"><span class="n">out_fd</span></em>, <em class="sig-param"><span class="n">in_fd</span></em>, <em class="sig-param"><span class="n">offset</span></em>, <em class="sig-param"><span class="n">count</span></em>, <em class="sig-param"><span class="n">headers</span><span class="o">=</span><span class="default_value">()</span></em>, <em class="sig-param"><span class="n">trailers</span><span class="o">=</span><span class="default_value">()</span></em>, <em class="sig-param"><span class="n">flags</span><span class="o">=</span><span class="default_value">0</span></em><span class="sig-paren">)</span>
</dt> <dd>
<p>Copy <em>count</em> bytes from file descriptor <em>in_fd</em> to file descriptor <em>out_fd</em> starting at <em>offset</em>. Return the number of bytes sent. When EOF is reached return <code>0</code>.</p> <p>The first function notation is supported by all platforms that define <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code>sendfile()</code></a>.</p> <p>On Linux, if <em>offset</em> is given as <code>None</code>, the bytes are read from the current position of <em>in_fd</em> and the position of <em>in_fd</em> is updated.</p> <p>The second case may be used on macOS and FreeBSD where <em>headers</em> and <em>trailers</em> are arbitrary sequences of buffers that are written before and after the data from <em>in_fd</em> is written. It returns the same as the first case.</p> <p>On macOS and FreeBSD, a value of <code>0</code> for <em>count</em> specifies to send until the end of <em>in_fd</em> is reached.</p> <p>All platforms support sockets as <em>out_fd</em> file descriptor, and some platforms allow other types (e.g. regular file, pipe) as well.</p> <p>Cross-platform applications should not use <em>headers</em>, <em>trailers</em> and <em>flags</em> arguments.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>For a higher-level wrapper of <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code>sendfile()</code></a>, see <a class="reference internal" href="socket#socket.socket.sendfile" title="socket.socket.sendfile"><code>socket.socket.sendfile()</code></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Parameters <em>out</em> and <em>in</em> was renamed to <em>out_fd</em> and <em>in_fd</em>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SF_NODISKIO">
<code>os.SF_NODISKIO</code> </dt> <dt class="sig sig-object py" id="os.SF_MNOWAIT">
<code>os.SF_MNOWAIT</code> </dt> <dt class="sig sig-object py" id="os.SF_SYNC">
<code>os.SF_SYNC</code> </dt> <dd>
<p>Parameters to the <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code>sendfile()</code></a> function, if the implementation supports them.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SF_NOCACHE">
<code>os.SF_NOCACHE</code> </dt> <dd>
<p>Parameter to the <a class="reference internal" href="#os.sendfile" title="os.sendfile"><code>sendfile()</code></a> function, if the implementation supports it. The data won’t be cached in the virtual memory and will be freed afterwards.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.set_blocking">
<code>os.set_blocking(fd, blocking, /)</code> </dt> <dd>
<p>Set the blocking mode of the specified file descriptor. Set the <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code>O_NONBLOCK</code></a> flag if blocking is <code>False</code>, clear the flag otherwise.</p> <p>See also <a class="reference internal" href="#os.get_blocking" title="os.get_blocking"><code>get_blocking()</code></a> and <a class="reference internal" href="socket#socket.socket.setblocking" title="socket.socket.setblocking"><code>socket.socket.setblocking()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> <p>On Windows, this function is limited to pipes.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for pipes on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.splice">
<code>os.splice(src, dst, count, offset_src=None, offset_dst=None)</code> </dt> <dd>
<p>Transfer <em>count</em> bytes from file descriptor <em>src</em>, starting from offset <em>offset_src</em>, to file descriptor <em>dst</em>, starting from offset <em>offset_dst</em>. At least one of the file descriptors must refer to a pipe. If <em>offset_src</em> is None, then <em>src</em> is read from the current position; respectively for <em>offset_dst</em>. The offset associated to the file descriptor that refers to a pipe must be <code>None</code>. The files pointed by <em>src</em> and <em>dst</em> must reside in the same filesystem, otherwise an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised with <a class="reference internal" href="exceptions#OSError.errno" title="OSError.errno"><code>errno</code></a> set to <a class="reference internal" href="errno#errno.EXDEV" title="errno.EXDEV"><code>errno.EXDEV</code></a>.</p> <p>This copy is done without the additional cost of transferring data from the kernel to user space and then back into the kernel. Additionally, some filesystems could implement extra optimizations. The copy is done as if both files are opened as binary.</p> <p>Upon successful completion, returns the number of bytes spliced to or from the pipe. A return value of 0 means end of input. If <em>src</em> refers to a pipe, then this means that there was no data to transfer, and it would not make sense to block because there are no writers connected to the write end of the pipe.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.17 with glibc >= 2.5</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SPLICE_F_MOVE">
<code>os.SPLICE_F_MOVE</code> </dt> <dt class="sig sig-object py" id="os.SPLICE_F_NONBLOCK">
<code>os.SPLICE_F_NONBLOCK</code> </dt> <dt class="sig sig-object py" id="os.SPLICE_F_MORE">
<code>os.SPLICE_F_MORE</code> </dt> <dd>
<div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.readv">
<code>os.readv(fd, buffers, /)</code> </dt> <dd>
<p>Read from a file descriptor <em>fd</em> into a number of mutable <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> <em>buffers</em>. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data.</p> <p>Return the total number of bytes actually read which can be less than the total capacity of all the objects.</p> <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code>sysconf()</code></a> value <code>'SC_IOV_MAX'</code>) on the number of buffers that can be used.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.tcgetpgrp">
<code>os.tcgetpgrp(fd, /)</code> </dt> <dd>
<p>Return the process group associated with the terminal given by <em>fd</em> (an open file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a>).</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.tcsetpgrp">
<code>os.tcsetpgrp(fd, pg, /)</code> </dt> <dd>
<p>Set the process group associated with the terminal given by <em>fd</em> (an open file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a>) to <em>pg</em>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.ttyname">
<code>os.ttyname(fd, /)</code> </dt> <dd>
<p>Return a string which specifies the terminal device associated with file descriptor <em>fd</em>. If <em>fd</em> is not associated with a terminal device, an exception is raised.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.write">
<code>os.write(fd, str, /)</code> </dt> <dd>
<p>Write the bytestring in <em>str</em> to file descriptor <em>fd</em>.</p> <p>Return the number of bytes actually written.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is intended for low-level I/O and must be applied to a file descriptor as returned by <a class="reference internal" href="#os.open" title="os.open"><code>os.open()</code></a> or <a class="reference internal" href="#os.pipe" title="os.pipe"><code>pipe()</code></a>. To write a “file object” returned by the built-in function <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> or by <a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> or <a class="reference internal" href="#os.fdopen" title="os.fdopen"><code>fdopen()</code></a>, or <a class="reference internal" href="sys#sys.stdout" title="sys.stdout"><code>sys.stdout</code></a> or <a class="reference internal" href="sys#sys.stderr" title="sys.stderr"><code>sys.stderr</code></a>, use its <code>write()</code> method.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an <a class="reference internal" href="exceptions#InterruptedError" title="InterruptedError"><code>InterruptedError</code></a> exception (see <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.writev">
<code>os.writev(fd, buffers, /)</code> </dt> <dd>
<p>Write the contents of <em>buffers</em> to file descriptor <em>fd</em>. <em>buffers</em> must be a sequence of <a class="reference internal" href="../glossary#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. Buffers are processed in array order. Entire contents of the first buffer is written before proceeding to the second, and so on.</p> <p>Returns the total number of bytes actually written.</p> <p>The operating system may set a limit (<a class="reference internal" href="#os.sysconf" title="os.sysconf"><code>sysconf()</code></a> value <code>'SC_IOV_MAX'</code>) on the number of buffers that can be used.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <section id="querying-the-size-of-a-terminal"> <span id="terminal-size"></span><h3>Querying the size of a terminal</h3> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <dl class="py function"> <dt class="sig sig-object py" id="os.get_terminal_size">
<code>os.get_terminal_size(fd=STDOUT_FILENO, /)</code> </dt> <dd>
<p>Return the size of the terminal window as <code>(columns, lines)</code>, tuple of type <a class="reference internal" href="#os.terminal_size" title="os.terminal_size"><code>terminal_size</code></a>.</p> <p>The optional argument <code>fd</code> (default <code>STDOUT_FILENO</code>, or standard output) specifies which file descriptor should be queried.</p> <p>If the file descriptor is not connected to a terminal, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised.</p> <p><a class="reference internal" href="shutil#shutil.get_terminal_size" title="shutil.get_terminal_size"><code>shutil.get_terminal_size()</code></a> is the high-level function which should normally be used, <code>os.get_terminal_size</code> is the low-level implementation.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="os.terminal_size">
<code>class os.terminal_size</code> </dt> <dd>
<p>A subclass of tuple, holding <code>(columns, lines)</code> of the terminal window size.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.terminal_size.columns">
<code>columns</code> </dt> <dd>
<p>Width of the terminal window in characters.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.terminal_size.lines">
<code>lines</code> </dt> <dd>
<p>Height of the terminal window in characters.</p> </dd>
</dl> </dd>
</dl> </section> <section id="inheritance-of-file-descriptors"> <span id="fd-inheritance"></span><h3>Inheritance of File Descriptors</h3> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> <p>A file descriptor has an “inheritable” flag which indicates if the file descriptor can be inherited by child processes. Since Python 3.4, file descriptors created by Python are non-inheritable by default.</p> <p>On UNIX, non-inheritable file descriptors are closed in child processes at the execution of a new program, other file descriptors are inherited.</p> <p>On Windows, non-inheritable handles and file descriptors are closed in child processes, except for standard streams (file descriptors 0, 1 and 2: stdin, stdout and stderr), which are always inherited. Using <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> functions, all inheritable handles and all inheritable file descriptors are inherited. Using the <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> module, all file descriptors except standard streams are closed, and inheritable handles are only inherited if the <em>close_fds</em> parameter is <code>False</code>.</p> <p>On WebAssembly platforms <code>wasm32-emscripten</code> and <code>wasm32-wasi</code>, the file descriptor cannot be modified.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.get_inheritable">
<code>os.get_inheritable(fd, /)</code> </dt> <dd>
<p>Get the “inheritable” flag of the specified file descriptor (a boolean).</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.set_inheritable">
<code>os.set_inheritable(fd, inheritable, /)</code> </dt> <dd>
<p>Set the “inheritable” flag of the specified file descriptor.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.get_handle_inheritable">
<code>os.get_handle_inheritable(handle, /)</code> </dt> <dd>
<p>Get the “inheritable” flag of the specified handle (a boolean).</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.set_handle_inheritable">
<code>os.set_handle_inheritable(handle, inheritable, /)</code> </dt> <dd>
<p>Set the “inheritable” flag of the specified handle.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> </dd>
</dl> </section> </section> <section id="files-and-directories"> <span id="os-file-dir"></span><h2>Files and Directories</h2> <p>On some Unix platforms, many of these functions support one or more of these features:</p> <ul id="path-fd"> <li>
<p><strong>specifying a file descriptor:</strong> Normally the <em>path</em> argument provided to functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module must be a string specifying a file path. However, some functions now alternatively accept an open file descriptor for their <em>path</em> argument. The function will then operate on the file referred to by the descriptor. (For POSIX systems, Python will call the variant of the function prefixed with <code>f</code> (e.g. call <code>fchdir</code> instead of <code>chdir</code>).)</p> <p>You can check whether or not <em>path</em> can be specified as a file descriptor for a particular function on your platform using <a class="reference internal" href="#os.supports_fd" title="os.supports_fd"><code>os.supports_fd</code></a>. If this functionality is unavailable, using it will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>.</p> <p>If the function also supports <em>dir_fd</em> or <em>follow_symlinks</em> arguments, it’s an error to specify one of those when supplying <em>path</em> as a file descriptor.</p> </li> </ul> <ul id="dir-fd"> <li>
<p><strong>paths relative to directory descriptors:</strong> If <em>dir_fd</em> is not <code>None</code>, it should be a file descriptor referring to a directory, and the path to operate on should be relative; path will then be relative to that directory. If the path is absolute, <em>dir_fd</em> is ignored. (For POSIX systems, Python will call the variant of the function with an <code>at</code> suffix and possibly prefixed with <code>f</code> (e.g. call <code>faccessat</code> instead of <code>access</code>).</p> <p>You can check whether or not <em>dir_fd</em> is supported for a particular function on your platform using <a class="reference internal" href="#os.supports_dir_fd" title="os.supports_dir_fd"><code>os.supports_dir_fd</code></a>. If it’s unavailable, using it will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>.</p> </li> </ul> <ul id="follow-symlinks"> <li>
<p><strong>not following symlinks:</strong> If <em>follow_symlinks</em> is <code>False</code>, and the last element of the path to operate on is a symbolic link, the function will operate on the symbolic link itself rather than the file pointed to by the link. (For POSIX systems, Python will call the <code>l...</code> variant of the function.)</p> <p>You can check whether or not <em>follow_symlinks</em> is supported for a particular function on your platform using <a class="reference internal" href="#os.supports_follow_symlinks" title="os.supports_follow_symlinks"><code>os.supports_follow_symlinks</code></a>. If it’s unavailable, using it will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>.</p> </li> </ul> <dl class="py function"> <dt class="sig sig-object py" id="os.access">
<code>os.access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)</code> </dt> <dd>
<p>Use the real uid/gid to test for access to <em>path</em>. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to <em>path</em>. <em>mode</em> should be <a class="reference internal" href="#os.F_OK" title="os.F_OK"><code>F_OK</code></a> to test the existence of <em>path</em>, or it can be the inclusive OR of one or more of <a class="reference internal" href="#os.R_OK" title="os.R_OK"><code>R_OK</code></a>, <a class="reference internal" href="#os.W_OK" title="os.W_OK"><code>W_OK</code></a>, and <a class="reference internal" href="#os.X_OK" title="os.X_OK"><code>X_OK</code></a> to test permissions. Return <a class="reference internal" href="constants#True" title="True"><code>True</code></a> if access is allowed, <a class="reference internal" href="constants#False" title="False"><code>False</code></a> if not. See the Unix man page <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/access(2)">access(2)</a></em> for more information.</p> <p>This function can support specifying <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p>If <em>effective_ids</em> is <code>True</code>, <a class="reference internal" href="#os.access" title="os.access"><code>access()</code></a> will perform its access checks using the effective uid/gid instead of the real uid/gid. <em>effective_ids</em> may not be supported on your platform; you can check whether or not it is available using <a class="reference internal" href="#os.supports_effective_ids" title="os.supports_effective_ids"><code>os.supports_effective_ids</code></a>. If it is unavailable, using it will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Using <a class="reference internal" href="#os.access" title="os.access"><code>access()</code></a> to check if a user is authorized to e.g. open a file before actually doing so using <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it. It’s preferable to use <a class="reference internal" href="../glossary#term-EAFP"><span class="xref std std-term">EAFP</span></a> techniques. For example:</p> <pre data-language="python">if os.access("myfile", os.R_OK):
with open("myfile") as fp:
return fp.read()
return "some default data"
</pre> <p>is better written as:</p> <pre data-language="python">try:
fp = open("myfile")
except PermissionError:
return "some default data"
else:
with fp:
return fp.read()
</pre> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>I/O operations may fail even when <a class="reference internal" href="#os.access" title="os.access"><code>access()</code></a> indicates that they would succeed, particularly for operations on network filesystems which may have permissions semantics beyond the usual POSIX permission-bit model.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em>, <em>effective_ids</em>, and <em>follow_symlinks</em> parameters.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.F_OK">
<code>os.F_OK</code> </dt> <dt class="sig sig-object py" id="os.R_OK">
<code>os.R_OK</code> </dt> <dt class="sig sig-object py" id="os.W_OK">
<code>os.W_OK</code> </dt> <dt class="sig sig-object py" id="os.X_OK">
<code>os.X_OK</code> </dt> <dd>
<p>Values to pass as the <em>mode</em> parameter of <a class="reference internal" href="#os.access" title="os.access"><code>access()</code></a> to test the existence, readability, writability and executability of <em>path</em>, respectively.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.chdir">
<code>os.chdir(path)</code> </dt> <dd>
<p id="index-26">Change the current working directory to <em>path</em>.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>. The descriptor must refer to an opened directory, not an open file.</p> <p>This function can raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> and subclasses such as <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a>, <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a>, and <a class="reference internal" href="exceptions#NotADirectoryError" title="NotADirectoryError"><code>NotADirectoryError</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chdir</code> with argument <code>path</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as a file descriptor on some platforms.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.chflags">
<code>os.chflags(path, flags, *, follow_symlinks=True)</code> </dt> <dd>
<p>Set the flags of <em>path</em> to the numeric <em>flags</em>. <em>flags</em> may take a combination (bitwise OR) of the following values (as defined in the <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a> module):</p> <ul class="simple"> <li><a class="reference internal" href="stat#stat.UF_NODUMP" title="stat.UF_NODUMP"><code>stat.UF_NODUMP</code></a></li> <li><a class="reference internal" href="stat#stat.UF_IMMUTABLE" title="stat.UF_IMMUTABLE"><code>stat.UF_IMMUTABLE</code></a></li> <li><a class="reference internal" href="stat#stat.UF_APPEND" title="stat.UF_APPEND"><code>stat.UF_APPEND</code></a></li> <li><a class="reference internal" href="stat#stat.UF_OPAQUE" title="stat.UF_OPAQUE"><code>stat.UF_OPAQUE</code></a></li> <li><a class="reference internal" href="stat#stat.UF_NOUNLINK" title="stat.UF_NOUNLINK"><code>stat.UF_NOUNLINK</code></a></li> <li><a class="reference internal" href="stat#stat.UF_COMPRESSED" title="stat.UF_COMPRESSED"><code>stat.UF_COMPRESSED</code></a></li> <li><a class="reference internal" href="stat#stat.UF_HIDDEN" title="stat.UF_HIDDEN"><code>stat.UF_HIDDEN</code></a></li> <li><a class="reference internal" href="stat#stat.SF_ARCHIVED" title="stat.SF_ARCHIVED"><code>stat.SF_ARCHIVED</code></a></li> <li><a class="reference internal" href="stat#stat.SF_IMMUTABLE" title="stat.SF_IMMUTABLE"><code>stat.SF_IMMUTABLE</code></a></li> <li><a class="reference internal" href="stat#stat.SF_APPEND" title="stat.SF_APPEND"><code>stat.SF_APPEND</code></a></li> <li><a class="reference internal" href="stat#stat.SF_NOUNLINK" title="stat.SF_NOUNLINK"><code>stat.SF_NOUNLINK</code></a></li> <li><a class="reference internal" href="stat#stat.SF_SNAPSHOT" title="stat.SF_SNAPSHOT"><code>stat.SF_SNAPSHOT</code></a></li> </ul> <p>This function can support <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chflags</code> with arguments <code>path</code>, <code>flags</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>follow_symlinks</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.chmod">
<code>os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)</code> </dt> <dd>
<p>Change the mode of <em>path</em> to the numeric <em>mode</em>. <em>mode</em> may take one of the following values (as defined in the <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a> module) or bitwise ORed combinations of them:</p> <ul class="simple"> <li><a class="reference internal" href="stat#stat.S_ISUID" title="stat.S_ISUID"><code>stat.S_ISUID</code></a></li> <li><a class="reference internal" href="stat#stat.S_ISGID" title="stat.S_ISGID"><code>stat.S_ISGID</code></a></li> <li><a class="reference internal" href="stat#stat.S_ENFMT" title="stat.S_ENFMT"><code>stat.S_ENFMT</code></a></li> <li><a class="reference internal" href="stat#stat.S_ISVTX" title="stat.S_ISVTX"><code>stat.S_ISVTX</code></a></li> <li><a class="reference internal" href="stat#stat.S_IREAD" title="stat.S_IREAD"><code>stat.S_IREAD</code></a></li> <li><a class="reference internal" href="stat#stat.S_IWRITE" title="stat.S_IWRITE"><code>stat.S_IWRITE</code></a></li> <li><a class="reference internal" href="stat#stat.S_IEXEC" title="stat.S_IEXEC"><code>stat.S_IEXEC</code></a></li> <li><a class="reference internal" href="stat#stat.S_IRWXU" title="stat.S_IRWXU"><code>stat.S_IRWXU</code></a></li> <li><a class="reference internal" href="stat#stat.S_IRUSR" title="stat.S_IRUSR"><code>stat.S_IRUSR</code></a></li> <li><a class="reference internal" href="stat#stat.S_IWUSR" title="stat.S_IWUSR"><code>stat.S_IWUSR</code></a></li> <li><a class="reference internal" href="stat#stat.S_IXUSR" title="stat.S_IXUSR"><code>stat.S_IXUSR</code></a></li> <li><a class="reference internal" href="stat#stat.S_IRWXG" title="stat.S_IRWXG"><code>stat.S_IRWXG</code></a></li> <li><a class="reference internal" href="stat#stat.S_IRGRP" title="stat.S_IRGRP"><code>stat.S_IRGRP</code></a></li> <li><a class="reference internal" href="stat#stat.S_IWGRP" title="stat.S_IWGRP"><code>stat.S_IWGRP</code></a></li> <li><a class="reference internal" href="stat#stat.S_IXGRP" title="stat.S_IXGRP"><code>stat.S_IXGRP</code></a></li> <li><a class="reference internal" href="stat#stat.S_IRWXO" title="stat.S_IRWXO"><code>stat.S_IRWXO</code></a></li> <li><a class="reference internal" href="stat#stat.S_IROTH" title="stat.S_IROTH"><code>stat.S_IROTH</code></a></li> <li><a class="reference internal" href="stat#stat.S_IWOTH" title="stat.S_IWOTH"><code>stat.S_IWOTH</code></a></li> <li><a class="reference internal" href="stat#stat.S_IXOTH" title="stat.S_IXOTH"><code>stat.S_IXOTH</code></a></li> </ul> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>, <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Although Windows supports <a class="reference internal" href="#os.chmod" title="os.chmod"><code>chmod()</code></a>, you can only set the file’s read-only flag with it (via the <code>stat.S_IWRITE</code> and <code>stat.S_IREAD</code> constants or a corresponding integer value). All other bits are ignored.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chmod</code> with arguments <code>path</code>, <code>mode</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor, and the <em>dir_fd</em> and <em>follow_symlinks</em> arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.chown">
<code>os.chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)</code> </dt> <dd>
<p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. To leave one of the ids unchanged, set it to -1.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>, <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p>See <a class="reference internal" href="shutil#shutil.chown" title="shutil.chown"><code>shutil.chown()</code></a> for a higher-level function that accepts names in addition to numeric ids.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chown</code> with arguments <code>path</code>, <code>uid</code>, <code>gid</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor, and the <em>dir_fd</em> and <em>follow_symlinks</em> arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Supports a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.chroot">
<code>os.chroot(path)</code> </dt> <dd>
<p>Change the root directory of the current process to <em>path</em>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fchdir">
<code>os.fchdir(fd)</code> </dt> <dd>
<p>Change the current working directory to the directory represented by the file descriptor <em>fd</em>. The descriptor must refer to an opened directory, not an open file. As of Python 3.3, this is equivalent to <code>os.chdir(fd)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chdir</code> with argument <code>path</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getcwd">
<code>os.getcwd()</code> </dt> <dd>
<p>Return a string representing the current working directory.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getcwdb">
<code>os.getcwdb()</code> </dt> <dd>
<p>Return a bytestring representing the current working directory.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The function now uses the UTF-8 encoding on Windows, rather than the ANSI code page: see <span class="target" id="index-27"></span><a class="pep reference external" href="https://peps.python.org/pep-0529/"><strong>PEP 529</strong></a> for the rationale. The function is no longer deprecated on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lchflags">
<code>os.lchflags(path, flags)</code> </dt> <dd>
<p>Set the flags of <em>path</em> to the numeric <em>flags</em>, like <a class="reference internal" href="#os.chflags" title="os.chflags"><code>chflags()</code></a>, but do not follow symbolic links. As of Python 3.3, this is equivalent to <code>os.chflags(path, flags, follow_symlinks=False)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chflags</code> with arguments <code>path</code>, <code>flags</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lchmod">
<code>os.lchmod(path, mode)</code> </dt> <dd>
<p>Change the mode of <em>path</em> to the numeric <em>mode</em>. If path is a symlink, this affects the symlink rather than the target. See the docs for <a class="reference internal" href="#os.chmod" title="os.chmod"><code>chmod()</code></a> for possible values of <em>mode</em>. As of Python 3.3, this is equivalent to <code>os.chmod(path, mode, follow_symlinks=False)</code>.</p> <p><code>lchmod()</code> is not part of POSIX, but Unix implementations may have it if changing the mode of symbolic links is supported.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chmod</code> with arguments <code>path</code>, <code>mode</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Linux, FreeBSD >= 1.3, NetBSD >= 1.3, not OpenBSD</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lchown">
<code>os.lchown(path, uid, gid)</code> </dt> <dd>
<p>Change the owner and group id of <em>path</em> to the numeric <em>uid</em> and <em>gid</em>. This function will not follow symbolic links. As of Python 3.3, this is equivalent to <code>os.chown(path, uid, gid, follow_symlinks=False)</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.chown</code> with arguments <code>path</code>, <code>uid</code>, <code>gid</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.link">
<code>os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)</code> </dt> <dd>
<p>Create a hard link pointing to <em>src</em> named <em>dst</em>.</p> <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>, and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.link</code> with arguments <code>src</code>, <code>dst</code>, <code>src_dir_fd</code>, <code>dst_dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added Windows support.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added the <em>src_dir_fd</em>, <em>dst_dir_fd</em>, and <em>follow_symlinks</em> arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.listdir">
<code>os.listdir(path='.')</code> </dt> <dd>
<p>Return a list containing the names of the entries in the directory given by <em>path</em>. The list is in arbitrary order, and does not include the special entries <code>'.'</code> and <code>'..'</code> even if they are present in the directory. If a file is removed from or added to the directory during the call of this function, whether a name for that file be included is unspecified.</p> <p><em>path</em> may be a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>. If <em>path</em> is of type <code>bytes</code> (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface), the filenames returned will also be of type <code>bytes</code>; in all other circumstances, they will be of type <code>str</code>.</p> <p>This function can also support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>; the file descriptor must refer to a directory.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.listdir</code> with argument <code>path</code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>To encode <code>str</code> filenames to <code>bytes</code>, use <a class="reference internal" href="#os.fsencode" title="os.fsencode"><code>fsencode()</code></a>.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> function returns directory entries along with file attribute information, giving better performance for many common use cases.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <em>path</em> parameter became optional.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.listdrives">
<code>os.listdrives()</code> </dt> <dd>
<p>Return a list containing the names of drives on a Windows system.</p> <p>A drive name typically looks like <code>'C:\\'</code>. Not every drive name will be associated with a volume, and some may be inaccessible for a variety of reasons, including permissions, network connectivity or missing media. This function does not test for access.</p> <p>May raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if an error occurs collecting the drive names.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.listdrives</code> with no arguments.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.listmounts">
<code>os.listmounts(volume)</code> </dt> <dd>
<p>Return a list containing the mount points for a volume on a Windows system.</p> <p><em>volume</em> must be represented as a GUID path, like those returned by <a class="reference internal" href="#os.listvolumes" title="os.listvolumes"><code>os.listvolumes()</code></a>. Volumes may be mounted in multiple locations or not at all. In the latter case, the list will be empty. Mount points that are not associated with a volume will not be returned by this function.</p> <p>The mount points return by this function will be absolute paths, and may be longer than the drive name.</p> <p>Raises <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if the volume is not recognized or if an error occurs collecting the paths.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.listmounts</code> with argument <code>volume</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.listvolumes">
<code>os.listvolumes()</code> </dt> <dd>
<p>Return a list containing the volumes in the system.</p> <p>Volumes are typically represented as a GUID path that looks like <code>\\?\Volume{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\</code>. Files can usually be accessed through a GUID path, permissions allowing. However, users are generally not familiar with them, and so the recommended use of this function is to retrieve mount points using <a class="reference internal" href="#os.listmounts" title="os.listmounts"><code>os.listmounts()</code></a>.</p> <p>May raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if an error occurs collecting the volumes.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.listvolumes</code> with no arguments.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.lstat">
<code>os.lstat(path, *, dir_fd=None)</code> </dt> <dd>
<p>Perform the equivalent of an <code>lstat()</code> system call on the given path. Similar to <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a>, but does not follow symbolic links. Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> object.</p> <p>On platforms that do not support symbolic links, this is an alias for <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a>.</p> <p>As of Python 3.3, this is equivalent to <code>os.stat(path, dir_fd=dir_fd,
follow_symlinks=False)</code>.</p> <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a> function.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Added the <em>dir_fd</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, now opens reparse points that represent another path (name surrogates), including symbolic links and directory junctions. Other kinds of reparse points are resolved by the operating system as for <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.mkdir">
<code>os.mkdir(path, mode=0o777, *, dir_fd=None)</code> </dt> <dd>
<p>Create a directory named <em>path</em> with numeric mode <em>mode</em>.</p> <p>If the directory already exists, <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is raised. If a parent directory in the path does not exist, <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is raised.</p> <p id="mkdir-modebits">On some systems, <em>mode</em> is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of the <em>mode</em>) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call <a class="reference internal" href="#os.chmod" title="os.chmod"><code>chmod()</code></a> explicitly to set them.</p> <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p>It is also possible to create temporary directories; see the <a class="reference internal" href="tempfile#module-tempfile" title="tempfile: Generate temporary files and directories."><code>tempfile</code></a> module’s <a class="reference internal" href="tempfile#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>tempfile.mkdtemp()</code></a> function.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.mkdir</code> with arguments <code>path</code>, <code>mode</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.makedirs">
<code>os.makedirs(name, mode=0o777, exist_ok=False)</code> </dt> <dd>
<p id="index-28">Recursive directory creation function. Like <a class="reference internal" href="#os.mkdir" title="os.mkdir"><code>mkdir()</code></a>, but makes all intermediate-level directories needed to contain the leaf directory.</p> <p>The <em>mode</em> parameter is passed to <a class="reference internal" href="#os.mkdir" title="os.mkdir"><code>mkdir()</code></a> for creating the leaf directory; see <a class="reference internal" href="#mkdir-modebits"><span class="std std-ref">the mkdir() description</span></a> for how it is interpreted. To set the file permission bits of any newly created parent directories you can set the umask before invoking <a class="reference internal" href="#os.makedirs" title="os.makedirs"><code>makedirs()</code></a>. The file permission bits of existing parent directories are not changed.</p> <p>If <em>exist_ok</em> is <code>False</code> (the default), a <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is raised if the target directory already exists.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="#os.makedirs" title="os.makedirs"><code>makedirs()</code></a> will become confused if the path elements to create include <a class="reference internal" href="#os.pardir" title="os.pardir"><code>pardir</code></a> (eg. “..” on UNIX systems).</p> </div> <p>This function handles UNC paths correctly.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.mkdir</code> with arguments <code>path</code>, <code>mode</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2: </span>The <em>exist_ok</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4.1: </span>Before Python 3.4.1, if <em>exist_ok</em> was <code>True</code> and the directory existed, <a class="reference internal" href="#os.makedirs" title="os.makedirs"><code>makedirs()</code></a> would still raise an error if <em>mode</em> did not match the mode of the existing directory. Since this behavior was impossible to implement safely, it was removed in Python 3.4.1. See <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=21082">bpo-21082</a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>The <em>mode</em> argument no longer affects the file permission bits of newly created intermediate-level directories.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.mkfifo">
<code>os.mkfifo(path, mode=0o666, *, dir_fd=None)</code> </dt> <dd>
<p>Create a FIFO (a named pipe) named <em>path</em> with numeric mode <em>mode</em>. The current umask value is first masked out from the mode.</p> <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p>FIFOs are pipes that can be accessed like regular files. FIFOs exist until they are deleted (for example with <a class="reference internal" href="#os.unlink" title="os.unlink"><code>os.unlink()</code></a>). Generally, FIFOs are used as rendezvous between “client” and “server” type processes: the server opens the FIFO for reading, and the client opens it for writing. Note that <a class="reference internal" href="#os.mkfifo" title="os.mkfifo"><code>mkfifo()</code></a> doesn’t open the FIFO — it just creates the rendezvous point.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.mknod">
<code>os.mknod(path, mode=0o600, device=0, *, dir_fd=None)</code> </dt> <dd>
<p>Create a filesystem node (file, device special file or named pipe) named <em>path</em>. <em>mode</em> specifies both the permissions to use and the type of node to be created, being combined (bitwise OR) with one of <code>stat.S_IFREG</code>, <code>stat.S_IFCHR</code>, <code>stat.S_IFBLK</code>, and <code>stat.S_IFIFO</code> (those constants are available in <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a>). For <code>stat.S_IFCHR</code> and <code>stat.S_IFBLK</code>, <em>device</em> defines the newly created device special file (probably using <a class="reference internal" href="#os.makedev" title="os.makedev"><code>os.makedev()</code></a>), otherwise it is ignored.</p> <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.major">
<code>os.major(device, /)</code> </dt> <dd>
<p>Extract the device major number from a raw device number (usually the <code>st_dev</code> or <code>st_rdev</code> field from <code>stat</code>).</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.minor">
<code>os.minor(device, /)</code> </dt> <dd>
<p>Extract the device minor number from a raw device number (usually the <code>st_dev</code> or <code>st_rdev</code> field from <code>stat</code>).</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.makedev">
<code>os.makedev(major, minor, /)</code> </dt> <dd>
<p>Compose a raw device number from the major and minor device numbers.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pathconf">
<code>os.pathconf(path, name)</code> </dt> <dd>
<p>Return system configuration information relevant to a named file. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given in the <code>pathconf_names</code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code>pathconf_names</code>, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised with <a class="reference internal" href="errno#errno.EINVAL" title="errno.EINVAL"><code>errno.EINVAL</code></a> for the error number.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.pathconf_names">
<code>os.pathconf_names</code> </dt> <dd>
<p>Dictionary mapping names accepted by <a class="reference internal" href="#os.pathconf" title="os.pathconf"><code>pathconf()</code></a> and <a class="reference internal" href="#os.fpathconf" title="os.fpathconf"><code>fpathconf()</code></a> to the integer values defined for those names by the host operating system. This can be used to determine the set of names known to the system.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.readlink">
<code>os.readlink(path, *, dir_fd=None)</code> </dt> <dd>
<p>Return a string representing the path to which the symbolic link points. The result may be either an absolute or relative pathname; if it is relative, it may be converted to an absolute pathname using <code>os.path.join(os.path.dirname(path), result)</code>.</p> <p>If the <em>path</em> is a string object (directly or indirectly through a <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface), the result will also be a string object, and the call may raise a UnicodeDecodeError. If the <em>path</em> is a bytes object (direct or indirectly), the result will be a bytes object.</p> <p>This function can also support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p>When trying to resolve a path that may contain links, use <a class="reference internal" href="os.path#os.path.realpath" title="os.path.realpath"><code>realpath()</code></a> to properly handle recursion and platform differences.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> on Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> and a bytes object on Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for directory junctions, and changed to return the substitution path (which typically includes <code>\\?\</code> prefix) rather than the optional “print name” field that was previously returned.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.remove">
<code>os.remove(path, *, dir_fd=None)</code> </dt> <dd>
<p>Remove (delete) the file <em>path</em>. If <em>path</em> is a directory, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised. Use <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code>rmdir()</code></a> to remove directories. If the file does not exist, a <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is raised.</p> <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p>On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.</p> <p>This function is semantically identical to <a class="reference internal" href="#os.unlink" title="os.unlink"><code>unlink()</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.remove</code> with arguments <code>path</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.removedirs">
<code>os.removedirs(name)</code> </dt> <dd>
<p id="index-29">Remove directories recursively. Works like <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code>rmdir()</code></a> except that, if the leaf directory is successfully removed, <a class="reference internal" href="#os.removedirs" title="os.removedirs"><code>removedirs()</code></a> tries to successively remove every parent directory mentioned in <em>path</em> until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, <code>os.removedirs('foo/bar/baz')</code> will first remove the directory <code>'foo/bar/baz'</code>, and then remove <code>'foo/bar'</code> and <code>'foo'</code> if they are empty. Raises <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if the leaf directory could not be successfully removed.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.remove</code> with arguments <code>path</code>, <code>dir_fd</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.rename">
<code>os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)</code> </dt> <dd>
<p>Rename the file or directory <em>src</em> to <em>dst</em>. If <em>dst</em> exists, the operation will fail with an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> subclass in a number of cases:</p> <p>On Windows, if <em>dst</em> exists a <a class="reference internal" href="exceptions#FileExistsError" title="FileExistsError"><code>FileExistsError</code></a> is always raised. The operation may fail if <em>src</em> and <em>dst</em> are on different filesystems. Use <a class="reference internal" href="shutil#shutil.move" title="shutil.move"><code>shutil.move()</code></a> to support moves to a different filesystem.</p> <p>On Unix, if <em>src</em> is a file and <em>dst</em> is a directory or vice-versa, an <a class="reference internal" href="exceptions#IsADirectoryError" title="IsADirectoryError"><code>IsADirectoryError</code></a> or a <a class="reference internal" href="exceptions#NotADirectoryError" title="NotADirectoryError"><code>NotADirectoryError</code></a> will be raised respectively. If both are directories and <em>dst</em> is empty, <em>dst</em> will be silently replaced. If <em>dst</em> is a non-empty directory, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised. If both are files, <em>dst</em> will be replaced silently if the user has permission. The operation may fail on some Unix flavors if <em>src</em> and <em>dst</em> are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).</p> <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p>If you want cross-platform overwriting of the destination, use <a class="reference internal" href="#os.replace" title="os.replace"><code>replace()</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.rename</code> with arguments <code>src</code>, <code>dst</code>, <code>src_dir_fd</code>, <code>dst_dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>src_dir_fd</em> and <em>dst_dir_fd</em> arguments.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.renames">
<code>os.renames(old, new)</code> </dt> <dd>
<p>Recursive directory or file renaming function. Works like <a class="reference internal" href="#os.rename" title="os.rename"><code>rename()</code></a>, except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned away using <a class="reference internal" href="#os.removedirs" title="os.removedirs"><code>removedirs()</code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function can fail with the new directory structure made if you lack permissions needed to remove the leaf directory or file.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.rename</code> with arguments <code>src</code>, <code>dst</code>, <code>src_dir_fd</code>, <code>dst_dir_fd</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>old</em> and <em>new</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.replace">
<code>os.replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)</code> </dt> <dd>
<p>Rename the file or directory <em>src</em> to <em>dst</em>. If <em>dst</em> is a non-empty directory, <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> will be raised. If <em>dst</em> exists and is a file, it will be replaced silently if the user has permission. The operation may fail if <em>src</em> and <em>dst</em> are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).</p> <p>This function can support specifying <em>src_dir_fd</em> and/or <em>dst_dir_fd</em> to supply <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.rename</code> with arguments <code>src</code>, <code>dst</code>, <code>src_dir_fd</code>, <code>dst_dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.rmdir">
<code>os.rmdir(path, *, dir_fd=None)</code> </dt> <dd>
<p>Remove (delete) the directory <em>path</em>. If the directory does not exist or is not empty, a <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> or an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised respectively. In order to remove whole directory trees, <a class="reference internal" href="shutil#shutil.rmtree" title="shutil.rmtree"><code>shutil.rmtree()</code></a> can be used.</p> <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.rmdir</code> with arguments <code>path</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.scandir">
<code>os.scandir(path='.')</code> </dt> <dd>
<p>Return an iterator of <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code>os.DirEntry</code></a> objects corresponding to the entries in the directory given by <em>path</em>. The entries are yielded in arbitrary order, and the special entries <code>'.'</code> and <code>'..'</code> are not included. If a file is removed from or added to the directory after creating the iterator, whether an entry for that file be included is unspecified.</p> <p>Using <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> instead of <a class="reference internal" href="#os.listdir" title="os.listdir"><code>listdir()</code></a> can significantly increase the performance of code that also needs file type or file attribute information, because <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code>os.DirEntry</code></a> objects expose this information if the operating system provides it when scanning a directory. All <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code>os.DirEntry</code></a> methods may perform a system call, but <a class="reference internal" href="#os.DirEntry.is_dir" title="os.DirEntry.is_dir"><code>is_dir()</code></a> and <a class="reference internal" href="#os.DirEntry.is_file" title="os.DirEntry.is_file"><code>is_file()</code></a> usually only require a system call for symbolic links; <a class="reference internal" href="#os.DirEntry.stat" title="os.DirEntry.stat"><code>os.DirEntry.stat()</code></a> always requires a system call on Unix but only requires one for symbolic links on Windows.</p> <p><em>path</em> may be a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>. If <em>path</em> is of type <code>bytes</code> (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface), the type of the <a class="reference internal" href="#os.DirEntry.name" title="os.DirEntry.name"><code>name</code></a> and <a class="reference internal" href="#os.DirEntry.path" title="os.DirEntry.path"><code>path</code></a> attributes of each <a class="reference internal" href="#os.DirEntry" title="os.DirEntry"><code>os.DirEntry</code></a> will be <code>bytes</code>; in all other circumstances, they will be of type <code>str</code>.</p> <p>This function can also support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>; the file descriptor must refer to a directory.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.scandir</code> with argument <code>path</code>.</p> <p>The <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> iterator supports the <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol and has the following method:</p> <dl class="py method"> <dt class="sig sig-object py" id="os.scandir.close">
<code>scandir.close()</code> </dt> <dd>
<p>Close the iterator and free acquired resources.</p> <p>This is called automatically when the iterator is exhausted or garbage collected, or when an error happens during iterating. However it is advisable to call it explicitly or use the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <p>The following example shows a simple use of <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> to display all the files (excluding directories) in the given <em>path</em> that don’t start with <code>'.'</code>. The <code>entry.is_file()</code> call will generally not make an additional system call:</p> <pre data-language="python">with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
print(entry.name)
</pre> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On Unix-based systems, <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> uses the system’s <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html">opendir()</a> and <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/009695399/functions/readdir_r.html">readdir()</a> functions. On Windows, it uses the Win32 <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx">FindFirstFileW</a> and <a class="reference external" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx">FindNextFileW</a> functions.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6: </span>Added support for the <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol and the <a class="reference internal" href="#os.scandir.close" title="os.scandir.close"><code>close()</code></a> method. If a <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> iterator is neither exhausted nor explicitly closed a <a class="reference internal" href="exceptions#ResourceWarning" title="ResourceWarning"><code>ResourceWarning</code></a> will be emitted in its destructor.</p> <p>The function accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Added support for <a class="reference internal" href="#path-fd"><span class="std std-ref">file descriptors</span></a> on Unix.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="os.DirEntry">
<code>class os.DirEntry</code> </dt> <dd>
<p>Object yielded by <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> to expose the file path and other file attributes of a directory entry.</p> <p><a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> will provide as much of this information as possible without making additional system calls. When a <code>stat()</code> or <code>lstat()</code> system call is made, the <code>os.DirEntry</code> object will cache the result.</p> <p><code>os.DirEntry</code> instances are not intended to be stored in long-lived data structures; if you know the file metadata has changed or if a long time has elapsed since calling <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a>, call <code>os.stat(entry.path)</code> to fetch up-to-date information.</p> <p>Because the <code>os.DirEntry</code> methods can make operating system calls, they may also raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>. If you need very fine-grained control over errors, you can catch <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> when calling one of the <code>os.DirEntry</code> methods and handle as appropriate.</p> <p>To be directly usable as a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>, <code>os.DirEntry</code> implements the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface.</p> <p>Attributes and methods on a <code>os.DirEntry</code> instance are as follows:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.DirEntry.name">
<code>name</code> </dt> <dd>
<p>The entry’s base filename, relative to the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument.</p> <p>The <a class="reference internal" href="#os.name" title="os.name"><code>name</code></a> attribute will be <code>bytes</code> if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument is of type <code>bytes</code> and <code>str</code> otherwise. Use <a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code>fsdecode()</code></a> to decode byte filenames.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.DirEntry.path">
<code>path</code> </dt> <dd>
<p>The entry’s full path name: equivalent to <code>os.path.join(scandir_path,
entry.name)</code> where <em>scandir_path</em> is the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument. The path is only absolute if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument was absolute. If the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument was a <a class="reference internal" href="#path-fd"><span class="std std-ref">file descriptor</span></a>, the <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>path</code></a> attribute is the same as the <a class="reference internal" href="#os.name" title="os.name"><code>name</code></a> attribute.</p> <p>The <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>path</code></a> attribute will be <code>bytes</code> if the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> <em>path</em> argument is of type <code>bytes</code> and <code>str</code> otherwise. Use <a class="reference internal" href="#os.fsdecode" title="os.fsdecode"><code>fsdecode()</code></a> to decode byte filenames.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.inode">
<code>inode()</code> </dt> <dd>
<p>Return the inode number of the entry.</p> <p>The result is cached on the <code>os.DirEntry</code> object. Use <code>os.stat(entry.path, follow_symlinks=False).st_ino</code> to fetch up-to-date information.</p> <p>On the first, uncached call, a system call is required on Windows but not on Unix.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.is_dir">
<code>is_dir(*, follow_symlinks=True)</code> </dt> <dd>
<p>Return <code>True</code> if this entry is a directory or a symbolic link pointing to a directory; return <code>False</code> if the entry is or points to any other kind of file, or if it doesn’t exist anymore.</p> <p>If <em>follow_symlinks</em> is <code>False</code>, return <code>True</code> only if this entry is a directory (without following symlinks); return <code>False</code> if the entry is any other kind of file or if it doesn’t exist anymore.</p> <p>The result is cached on the <code>os.DirEntry</code> object, with a separate cache for <em>follow_symlinks</em> <code>True</code> and <code>False</code>. Call <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> along with <a class="reference internal" href="stat#stat.S_ISDIR" title="stat.S_ISDIR"><code>stat.S_ISDIR()</code></a> to fetch up-to-date information.</p> <p>On the first, uncached call, no system call is required in most cases. Specifically, for non-symlinks, neither Windows or Unix require a system call, except on certain Unix file systems, such as network file systems, that return <code>dirent.d_type == DT_UNKNOWN</code>. If the entry is a symlink, a system call will be required to follow the symlink unless <em>follow_symlinks</em> is <code>False</code>.</p> <p>This method can raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>, such as <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a>, but <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is caught and not raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.is_file">
<code>is_file(*, follow_symlinks=True)</code> </dt> <dd>
<p>Return <code>True</code> if this entry is a file or a symbolic link pointing to a file; return <code>False</code> if the entry is or points to a directory or other non-file entry, or if it doesn’t exist anymore.</p> <p>If <em>follow_symlinks</em> is <code>False</code>, return <code>True</code> only if this entry is a file (without following symlinks); return <code>False</code> if the entry is a directory or other non-file entry, or if it doesn’t exist anymore.</p> <p>The result is cached on the <code>os.DirEntry</code> object. Caching, system calls made, and exceptions raised are as per <a class="reference internal" href="#os.DirEntry.is_dir" title="os.DirEntry.is_dir"><code>is_dir()</code></a>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.is_symlink">
<code>is_symlink()</code> </dt> <dd>
<p>Return <code>True</code> if this entry is a symbolic link (even if broken); return <code>False</code> if the entry points to a directory or any kind of file, or if it doesn’t exist anymore.</p> <p>The result is cached on the <code>os.DirEntry</code> object. Call <a class="reference internal" href="os.path#os.path.islink" title="os.path.islink"><code>os.path.islink()</code></a> to fetch up-to-date information.</p> <p>On the first, uncached call, no system call is required in most cases. Specifically, neither Windows or Unix require a system call, except on certain Unix file systems, such as network file systems, that return <code>dirent.d_type == DT_UNKNOWN</code>.</p> <p>This method can raise <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>, such as <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a>, but <a class="reference internal" href="exceptions#FileNotFoundError" title="FileNotFoundError"><code>FileNotFoundError</code></a> is caught and not raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.is_junction">
<code>is_junction()</code> </dt> <dd>
<p>Return <code>True</code> if this entry is a junction (even if broken); return <code>False</code> if the entry points to a regular directory, any kind of file, a symlink, or if it doesn’t exist anymore.</p> <p>The result is cached on the <code>os.DirEntry</code> object. Call <a class="reference internal" href="os.path#os.path.isjunction" title="os.path.isjunction"><code>os.path.isjunction()</code></a> to fetch up-to-date information.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="os.DirEntry.stat">
<code>stat(*, follow_symlinks=True)</code> </dt> <dd>
<p>Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> object for this entry. This method follows symbolic links by default; to stat a symbolic link add the <code>follow_symlinks=False</code> argument.</p> <p>On Unix, this method always requires a system call. On Windows, it only requires a system call if <em>follow_symlinks</em> is <code>True</code> and the entry is a reparse point (for example, a symbolic link or directory junction).</p> <p>On Windows, the <code>st_ino</code>, <code>st_dev</code> and <code>st_nlink</code> attributes of the <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> are always set to zero. Call <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> to get these attributes.</p> <p>The result is cached on the <code>os.DirEntry</code> object, with a separate cache for <em>follow_symlinks</em> <code>True</code> and <code>False</code>. Call <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> to fetch up-to-date information.</p> </dd>
</dl> <p>Note that there is a nice correspondence between several attributes and methods of <code>os.DirEntry</code> and of <a class="reference internal" href="pathlib#pathlib.Path" title="pathlib.Path"><code>pathlib.Path</code></a>. In particular, the <code>name</code> attribute has the same meaning, as do the <code>is_dir()</code>, <code>is_file()</code>, <code>is_symlink()</code>, <code>is_junction()</code>, and <code>stat()</code> methods.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added support for the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface. Added support for <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> paths on Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>The <code>st_ctime</code> attribute of a stat result is deprecated on Windows. The file creation time is properly available as <code>st_birthtime</code>, and in the future <code>st_ctime</code> may be changed to return zero or the metadata change time, if available.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.stat">
<code>os.stat(path, *, dir_fd=None, follow_symlinks=True)</code> </dt> <dd>
<p>Get the status of a file or a file descriptor. Perform the equivalent of a <code>stat()</code> system call on the given path. <em>path</em> may be specified as either a string or bytes – directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface – or as an open file descriptor. Return a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> object.</p> <p>This function normally follows symlinks; to stat a symlink add the argument <code>follow_symlinks=False</code>, or use <a class="reference internal" href="#os.lstat" title="os.lstat"><code>lstat()</code></a>.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p>On Windows, passing <code>follow_symlinks=False</code> will disable following all name-surrogate reparse points, which includes symlinks and directory junctions. Other types of reparse points that do not resemble links or that the operating system is unable to follow will be opened directly. When following a chain of multiple links, this may result in the original link being returned instead of the non-link that prevented full traversal. To obtain stat results for the final path in this case, use the <a class="reference internal" href="os.path#os.path.realpath" title="os.path.realpath"><code>os.path.realpath()</code></a> function to resolve the path name as far as possible and call <a class="reference internal" href="#os.lstat" title="os.lstat"><code>lstat()</code></a> on the result. This does not apply to dangling symlinks or junction points, which will raise the usual exceptions.</p> <p id="index-30">Example:</p> <pre data-language="python">>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026,
st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295,
st_mtime=1297230027, st_ctime=1297230027)
>>> statinfo.st_size
264
</pre> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="#os.fstat" title="os.fstat"><code>fstat()</code></a> and <a class="reference internal" href="#os.lstat" title="os.lstat"><code>lstat()</code></a> functions.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added the <em>dir_fd</em> and <em>follow_symlinks</em> arguments, specifying a file descriptor instead of a path.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, all reparse points that can be resolved by the operating system are now followed, and passing <code>follow_symlinks=False</code> disables following all name surrogate reparse points. If the operating system reaches a reparse point that it is not able to follow, <em>stat</em> now returns the information for the original path as if <code>follow_symlinks=False</code> had been specified instead of raising an error.</p> </div> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="os.stat_result">
<code>class os.stat_result</code> </dt> <dd>
<p>Object whose attributes correspond roughly to the members of the <code>stat</code> structure. It is used for the result of <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a>, <a class="reference internal" href="#os.fstat" title="os.fstat"><code>os.fstat()</code></a> and <a class="reference internal" href="#os.lstat" title="os.lstat"><code>os.lstat()</code></a>.</p> <p>Attributes:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_mode">
<code>st_mode</code> </dt> <dd>
<p>File mode: file type and file mode bits (permissions).</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_ino">
<code>st_ino</code> </dt> <dd>
<p>Platform dependent, but if non-zero, uniquely identifies the file for a given value of <code>st_dev</code>. Typically:</p> <ul class="simple"> <li>the inode number on Unix,</li> <li>the <a class="reference external" href="https://msdn.microsoft.com/en-us/library/aa363788">file index</a> on Windows</li> </ul> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_dev">
<code>st_dev</code> </dt> <dd>
<p>Identifier of the device on which this file resides.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_nlink">
<code>st_nlink</code> </dt> <dd>
<p>Number of hard links.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_uid">
<code>st_uid</code> </dt> <dd>
<p>User identifier of the file owner.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_gid">
<code>st_gid</code> </dt> <dd>
<p>Group identifier of the file owner.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_size">
<code>st_size</code> </dt> <dd>
<p>Size of the file in bytes, if it is a regular file or a symbolic link. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte.</p> </dd>
</dl> <p>Timestamps:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_atime">
<code>st_atime</code> </dt> <dd>
<p>Time of most recent access expressed in seconds.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_mtime">
<code>st_mtime</code> </dt> <dd>
<p>Time of most recent content modification expressed in seconds.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_ctime">
<code>st_ctime</code> </dt> <dd>
<p>Time of most recent metadata change expressed in seconds.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span><code>st_ctime</code> is deprecated on Windows. Use <code>st_birthtime</code> for the file creation time. In the future, <code>st_ctime</code> will contain the time of the most recent metadata change, as for other platforms.</p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_atime_ns">
<code>st_atime_ns</code> </dt> <dd>
<p>Time of most recent access expressed in nanoseconds as an integer.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_mtime_ns">
<code>st_mtime_ns</code> </dt> <dd>
<p>Time of most recent content modification expressed in nanoseconds as an integer.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_ctime_ns">
<code>st_ctime_ns</code> </dt> <dd>
<p>Time of most recent metadata change expressed in nanoseconds as an integer.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span><code>st_ctime_ns</code> is deprecated on Windows. Use <code>st_birthtime_ns</code> for the file creation time. In the future, <code>st_ctime</code> will contain the time of the most recent metadata change, as for other platforms.</p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_birthtime">
<code>st_birthtime</code> </dt> <dd>
<p>Time of file creation expressed in seconds. This attribute is not always available, and may raise <a class="reference internal" href="exceptions#AttributeError" title="AttributeError"><code>AttributeError</code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span><code>st_birthtime</code> is now available on Windows.</p> </div> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_birthtime_ns">
<code>st_birthtime_ns</code> </dt> <dd>
<p>Time of file creation expressed in nanoseconds as an integer. This attribute is not always available, and may raise <a class="reference internal" href="exceptions#AttributeError" title="AttributeError"><code>AttributeError</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The exact meaning and resolution of the <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code>st_atime</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code>st_mtime</code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code>st_ctime</code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code>st_birthtime</code></a> attributes depend on the operating system and the file system. For example, on Windows systems using the FAT32 file systems, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code>st_mtime</code></a> has 2-second resolution, and <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code>st_atime</code></a> has only 1-day resolution. See your operating system documentation for details.</p> <p>Similarly, although <a class="reference internal" href="#os.stat_result.st_atime_ns" title="os.stat_result.st_atime_ns"><code>st_atime_ns</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime_ns" title="os.stat_result.st_mtime_ns"><code>st_mtime_ns</code></a>, <a class="reference internal" href="#os.stat_result.st_ctime_ns" title="os.stat_result.st_ctime_ns"><code>st_ctime_ns</code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime_ns" title="os.stat_result.st_birthtime_ns"><code>st_birthtime_ns</code></a> are always expressed in nanoseconds, many systems do not provide nanosecond precision. On systems that do provide nanosecond precision, the floating-point object used to store <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code>st_atime</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code>st_mtime</code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code>st_ctime</code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code>st_birthtime</code></a> cannot preserve all of it, and as such will be slightly inexact. If you need the exact timestamps you should always use <a class="reference internal" href="#os.stat_result.st_atime_ns" title="os.stat_result.st_atime_ns"><code>st_atime_ns</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime_ns" title="os.stat_result.st_mtime_ns"><code>st_mtime_ns</code></a>, <a class="reference internal" href="#os.stat_result.st_ctime_ns" title="os.stat_result.st_ctime_ns"><code>st_ctime_ns</code></a> and <a class="reference internal" href="#os.stat_result.st_birthtime_ns" title="os.stat_result.st_birthtime_ns"><code>st_birthtime_ns</code></a>.</p> </div> <p>On some Unix systems (such as Linux), the following attributes may also be available:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_blocks">
<code>st_blocks</code> </dt> <dd>
<p>Number of 512-byte blocks allocated for file. This may be smaller than <a class="reference internal" href="#os.stat_result.st_size" title="os.stat_result.st_size"><code>st_size</code></a>/512 when the file has holes.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_blksize">
<code>st_blksize</code> </dt> <dd>
<p>“Preferred” blocksize for efficient file system I/O. Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_rdev">
<code>st_rdev</code> </dt> <dd>
<p>Type of device if an inode device.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_flags">
<code>st_flags</code> </dt> <dd>
<p>User defined flags for file.</p> </dd>
</dl> <p>On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them):</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_gen">
<code>st_gen</code> </dt> <dd>
<p>File generation number.</p> </dd>
</dl> <p>On Solaris and derivatives, the following attributes may also be available:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_fstype">
<code>st_fstype</code> </dt> <dd>
<p>String that uniquely identifies the type of the filesystem that contains the file.</p> </dd>
</dl> <p>On macOS systems, the following attributes may also be available:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_rsize">
<code>st_rsize</code> </dt> <dd>
<p>Real size of the file.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_creator">
<code>st_creator</code> </dt> <dd>
<p>Creator of the file.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_type">
<code>st_type</code> </dt> <dd>
<p>File type.</p> </dd>
</dl> <p>On Windows systems, the following attributes are also available:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_file_attributes">
<code>st_file_attributes</code> </dt> <dd>
<p>Windows file attributes: <code>dwFileAttributes</code> member of the <code>BY_HANDLE_FILE_INFORMATION</code> structure returned by <code>GetFileInformationByHandle()</code>. See the <code>FILE_ATTRIBUTE_* <stat.FILE_ATTRIBUTE_ARCHIVE></code> constants in the <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a> module.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="os.stat_result.st_reparse_tag">
<code>st_reparse_tag</code> </dt> <dd>
<p>When <a class="reference internal" href="#os.stat_result.st_file_attributes" title="os.stat_result.st_file_attributes"><code>st_file_attributes</code></a> has the <a class="reference internal" href="stat#stat.FILE_ATTRIBUTE_REPARSE_POINT" title="stat.FILE_ATTRIBUTE_REPARSE_POINT"><code>FILE_ATTRIBUTE_REPARSE_POINT</code></a> set, this field contains the tag identifying the type of reparse point. See the <a class="reference internal" href="stat#stat.IO_REPARSE_TAG_SYMLINK" title="stat.IO_REPARSE_TAG_SYMLINK"><code>IO_REPARSE_TAG_*</code></a> constants in the <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a> module.</p> </dd>
</dl> <p>The standard module <a class="reference internal" href="stat#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><code>stat</code></a> defines functions and constants that are useful for extracting information from a <code>stat</code> structure. (On Windows, some items are filled with dummy values.)</p> <p>For backward compatibility, a <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> instance is also accessible as a tuple of at least 10 integers giving the most important (and portable) members of the <code>stat</code> structure, in the order <a class="reference internal" href="#os.stat_result.st_mode" title="os.stat_result.st_mode"><code>st_mode</code></a>, <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code>st_ino</code></a>, <a class="reference internal" href="#os.stat_result.st_dev" title="os.stat_result.st_dev"><code>st_dev</code></a>, <a class="reference internal" href="#os.stat_result.st_nlink" title="os.stat_result.st_nlink"><code>st_nlink</code></a>, <a class="reference internal" href="#os.stat_result.st_uid" title="os.stat_result.st_uid"><code>st_uid</code></a>, <a class="reference internal" href="#os.stat_result.st_gid" title="os.stat_result.st_gid"><code>st_gid</code></a>, <a class="reference internal" href="#os.stat_result.st_size" title="os.stat_result.st_size"><code>st_size</code></a>, <a class="reference internal" href="#os.stat_result.st_atime" title="os.stat_result.st_atime"><code>st_atime</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code>st_mtime</code></a>, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code>st_ctime</code></a>. More items may be added at the end by some implementations. For compatibility with older Python versions, accessing <a class="reference internal" href="#os.stat_result" title="os.stat_result"><code>stat_result</code></a> as a tuple always returns integers.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added the <a class="reference internal" href="#os.stat_result.st_atime_ns" title="os.stat_result.st_atime_ns"><code>st_atime_ns</code></a>, <a class="reference internal" href="#os.stat_result.st_mtime_ns" title="os.stat_result.st_mtime_ns"><code>st_mtime_ns</code></a>, and <a class="reference internal" href="#os.stat_result.st_ctime_ns" title="os.stat_result.st_ctime_ns"><code>st_ctime_ns</code></a> members.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5: </span>Added the <a class="reference internal" href="#os.stat_result.st_file_attributes" title="os.stat_result.st_file_attributes"><code>st_file_attributes</code></a> member on Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Windows now returns the file index as <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code>st_ino</code></a> when available.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7: </span>Added the <a class="reference internal" href="#os.stat_result.st_fstype" title="os.stat_result.st_fstype"><code>st_fstype</code></a> member to Solaris/derivatives.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>Added the <a class="reference internal" href="#os.stat_result.st_reparse_tag" title="os.stat_result.st_reparse_tag"><code>st_reparse_tag</code></a> member on Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>On Windows, the <a class="reference internal" href="#os.stat_result.st_mode" title="os.stat_result.st_mode"><code>st_mode</code></a> member now identifies special files as <code>S_IFCHR</code>, <code>S_IFIFO</code> or <code>S_IFBLK</code> as appropriate.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>On Windows, <a class="reference internal" href="#os.stat_result.st_ctime" title="os.stat_result.st_ctime"><code>st_ctime</code></a> is deprecated. Eventually, it will contain the last metadata change time, for consistency with other platforms, but for now still contains creation time. Use <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code>st_birthtime</code></a> for the creation time.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>On Windows, <a class="reference internal" href="#os.stat_result.st_ino" title="os.stat_result.st_ino"><code>st_ino</code></a> may now be up to 128 bits, depending on the file system. Previously it would not be above 64 bits, and larger file identifiers would be arbitrarily packed.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>On Windows, <a class="reference internal" href="#os.stat_result.st_rdev" title="os.stat_result.st_rdev"><code>st_rdev</code></a> no longer returns a value. Previously it would contain the same as <a class="reference internal" href="#os.stat_result.st_dev" title="os.stat_result.st_dev"><code>st_dev</code></a>, which was incorrect.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12: </span>Added the <a class="reference internal" href="#os.stat_result.st_birthtime" title="os.stat_result.st_birthtime"><code>st_birthtime</code></a> member on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.statvfs">
<code>os.statvfs(path)</code> </dt> <dd>
<p>Perform a <code>statvfs()</code> system call on the given path. The return value is an object whose attributes describe the filesystem on the given path, and correspond to the members of the <code>statvfs</code> structure, namely: <code>f_bsize</code>, <code>f_frsize</code>, <code>f_blocks</code>, <code>f_bfree</code>, <code>f_bavail</code>, <code>f_files</code>, <code>f_ffree</code>, <code>f_favail</code>, <code>f_flag</code>, <code>f_namemax</code>, <code>f_fsid</code>.</p> <p>Two module-level constants are defined for the <code>f_flag</code> attribute’s bit-flags: if <code>ST_RDONLY</code> is set, the filesystem is mounted read-only, and if <code>ST_NOSUID</code> is set, the semantics of setuid/setgid bits are disabled or not supported.</p> <p>Additional module-level constants are defined for GNU/glibc based systems. These are <code>ST_NODEV</code> (disallow access to device special files), <code>ST_NOEXEC</code> (disallow program execution), <code>ST_SYNCHRONOUS</code> (writes are synced at once), <code>ST_MANDLOCK</code> (allow mandatory locks on an FS), <code>ST_WRITE</code> (write on file/directory/symlink), <code>ST_APPEND</code> (append-only file), <code>ST_IMMUTABLE</code> (immutable file), <code>ST_NOATIME</code> (do not update access times), <code>ST_NODIRATIME</code> (do not update directory access times), <code>ST_RELATIME</code> (update atime relative to mtime/ctime).</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>The <code>ST_RDONLY</code> and <code>ST_NOSUID</code> constants were added.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>The <code>ST_NODEV</code>, <code>ST_NOEXEC</code>, <code>ST_SYNCHRONOUS</code>, <code>ST_MANDLOCK</code>, <code>ST_WRITE</code>, <code>ST_APPEND</code>, <code>ST_IMMUTABLE</code>, <code>ST_NOATIME</code>, <code>ST_NODIRATIME</code>, and <code>ST_RELATIME</code> constants were added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7: </span>Added <code>f_fsid</code>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.supports_dir_fd">
<code>os.supports_dir_fd</code> </dt> <dd>
<p>A <a class="reference internal" href="stdtypes#set" title="set"><code>set</code></a> object indicating which functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module accept an open file descriptor for their <em>dir_fd</em> parameter. Different platforms provide different features, and the underlying functionality Python uses to implement the <em>dir_fd</em> parameter is not available on all platforms Python supports. For consistency’s sake, functions that may support <em>dir_fd</em> always allow specifying the parameter, but will throw an exception if the functionality is used when it’s not locally available. (Specifying <code>None</code> for <em>dir_fd</em> is always supported on all platforms.)</p> <p>To check whether a particular function accepts an open file descriptor for its <em>dir_fd</em> parameter, use the <code>in</code> operator on <code>supports_dir_fd</code>. As an example, this expression evaluates to <code>True</code> if <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> accepts open file descriptors for <em>dir_fd</em> on the local platform:</p> <pre data-language="python">os.stat in os.supports_dir_fd
</pre> <p>Currently <em>dir_fd</em> parameters only work on Unix platforms; none of them work on Windows.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.supports_effective_ids">
<code>os.supports_effective_ids</code> </dt> <dd>
<p>A <a class="reference internal" href="stdtypes#set" title="set"><code>set</code></a> object indicating whether <a class="reference internal" href="#os.access" title="os.access"><code>os.access()</code></a> permits specifying <code>True</code> for its <em>effective_ids</em> parameter on the local platform. (Specifying <code>False</code> for <em>effective_ids</em> is always supported on all platforms.) If the local platform supports it, the collection will contain <a class="reference internal" href="#os.access" title="os.access"><code>os.access()</code></a>; otherwise it will be empty.</p> <p>This expression evaluates to <code>True</code> if <a class="reference internal" href="#os.access" title="os.access"><code>os.access()</code></a> supports <code>effective_ids=True</code> on the local platform:</p> <pre data-language="python">os.access in os.supports_effective_ids
</pre> <p>Currently <em>effective_ids</em> is only supported on Unix platforms; it does not work on Windows.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.supports_fd">
<code>os.supports_fd</code> </dt> <dd>
<p>A <a class="reference internal" href="stdtypes#set" title="set"><code>set</code></a> object indicating which functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module permit specifying their <em>path</em> parameter as an open file descriptor on the local platform. Different platforms provide different features, and the underlying functionality Python uses to accept open file descriptors as <em>path</em> arguments is not available on all platforms Python supports.</p> <p>To determine whether a particular function permits specifying an open file descriptor for its <em>path</em> parameter, use the <code>in</code> operator on <code>supports_fd</code>. As an example, this expression evaluates to <code>True</code> if <a class="reference internal" href="#os.chdir" title="os.chdir"><code>os.chdir()</code></a> accepts open file descriptors for <em>path</em> on your local platform:</p> <pre data-language="python">os.chdir in os.supports_fd
</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.supports_follow_symlinks">
<code>os.supports_follow_symlinks</code> </dt> <dd>
<p>A <a class="reference internal" href="stdtypes#set" title="set"><code>set</code></a> object indicating which functions in the <a class="reference internal" href="#module-os" title="os: Miscellaneous operating system interfaces."><code>os</code></a> module accept <code>False</code> for their <em>follow_symlinks</em> parameter on the local platform. Different platforms provide different features, and the underlying functionality Python uses to implement <em>follow_symlinks</em> is not available on all platforms Python supports. For consistency’s sake, functions that may support <em>follow_symlinks</em> always allow specifying the parameter, but will throw an exception if the functionality is used when it’s not locally available. (Specifying <code>True</code> for <em>follow_symlinks</em> is always supported on all platforms.)</p> <p>To check whether a particular function accepts <code>False</code> for its <em>follow_symlinks</em> parameter, use the <code>in</code> operator on <code>supports_follow_symlinks</code>. As an example, this expression evaluates to <code>True</code> if you may specify <code>follow_symlinks=False</code> when calling <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> on the local platform:</p> <pre data-language="python">os.stat in os.supports_follow_symlinks
</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.symlink">
<code>os.symlink(src, dst, target_is_directory=False, *, dir_fd=None)</code> </dt> <dd>
<p>Create a symbolic link pointing to <em>src</em> named <em>dst</em>.</p> <p>On Windows, a symlink represents either a file or a directory, and does not morph to the target dynamically. If the target is present, the type of the symlink will be created to match. Otherwise, the symlink will be created as a directory if <em>target_is_directory</em> is <code>True</code> or a file symlink (the default) otherwise. On non-Windows platforms, <em>target_is_directory</em> is ignored.</p> <p>This function can support <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>On newer versions of Windows 10, unprivileged accounts can create symlinks if Developer Mode is enabled. When Developer Mode is not available/enabled, the <em>SeCreateSymbolicLinkPrivilege</em> privilege is required, or the process must be run as an administrator.</p> <p><a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised when the function is called by an unprivileged user.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.symlink</code> with arguments <code>src</code>, <code>dst</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> <p>The function is limited on Emscripten and WASI, see <a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Added support for Windows 6.0 (Vista) symbolic links.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added the <em>dir_fd</em> argument, and now allow <em>target_is_directory</em> on non-Windows platforms.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>src</em> and <em>dst</em>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for unelevated symlinks on Windows with Developer Mode.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sync">
<code>os.sync()</code> </dt> <dd>
<p>Force write of everything to disk.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.truncate">
<code>os.truncate(path, length)</code> </dt> <dd>
<p>Truncate the file corresponding to <em>path</em>, so that it is at most <em>length</em> bytes in size.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.truncate</code> with arguments <code>path</code>, <code>length</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Added support for Windows</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.unlink">
<code>os.unlink(path, *, dir_fd=None)</code> </dt> <dd>
<p>Remove (delete) the file <em>path</em>. This function is semantically identical to <a class="reference internal" href="#os.remove" title="os.remove"><code>remove()</code></a>; the <code>unlink</code> name is its traditional Unix name. Please see the documentation for <a class="reference internal" href="#os.remove" title="os.remove"><code>remove()</code></a> for further information.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.remove</code> with arguments <code>path</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <em>dir_fd</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.utime">
<code>os.utime(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True)</code> </dt> <dd>
<p>Set the access and modified times of the file specified by <em>path</em>.</p> <p><a class="reference internal" href="#os.utime" title="os.utime"><code>utime()</code></a> takes two optional parameters, <em>times</em> and <em>ns</em>. These specify the times set on <em>path</em> and are used as follows:</p> <ul class="simple"> <li>If <em>ns</em> is specified, it must be a 2-tuple of the form <code>(atime_ns, mtime_ns)</code> where each member is an int expressing nanoseconds.</li> <li>If <em>times</em> is not <code>None</code>, it must be a 2-tuple of the form <code>(atime, mtime)</code> where each member is an int or float expressing seconds.</li> <li>If <em>times</em> is <code>None</code> and <em>ns</em> is unspecified, this is equivalent to specifying <code>ns=(atime_ns, mtime_ns)</code> where both times are the current time.</li> </ul> <p>It is an error to specify tuples for both <em>times</em> and <em>ns</em>.</p> <p>Note that the exact times you set here may not be returned by a subsequent <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a> call, depending on the resolution with which your operating system records access and modification times; see <a class="reference internal" href="#os.stat" title="os.stat"><code>stat()</code></a>. The best way to preserve exact times is to use the <em>st_atime_ns</em> and <em>st_mtime_ns</em> fields from the <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a> result object with the <em>ns</em> parameter to <a class="reference internal" href="#os.utime" title="os.utime"><code>utime()</code></a>.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a>, <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.utime</code> with arguments <code>path</code>, <code>times</code>, <code>ns</code>, <code>dir_fd</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor, and the <em>dir_fd</em>, <em>follow_symlinks</em>, and <em>ns</em> parameters.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.walk">
<code>os.walk(top, topdown=True, onerror=None, followlinks=False)</code> </dt> <dd>
<p id="index-31">Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory <em>top</em> (including <em>top</em> itself), it yields a 3-tuple <code>(dirpath, dirnames,
filenames)</code>.</p> <p><em>dirpath</em> is a string, the path to the directory. <em>dirnames</em> is a list of the names of the subdirectories in <em>dirpath</em> (including symlinks to directories, and excluding <code>'.'</code> and <code>'..'</code>). <em>filenames</em> is a list of the names of the non-directory files in <em>dirpath</em>. Note that the names in the lists contain no path components. To get a full path (which begins with <em>top</em>) to a file or directory in <em>dirpath</em>, do <code>os.path.join(dirpath, name)</code>. Whether or not the lists are sorted depends on the file system. If a file is removed from or added to the <em>dirpath</em> directory during generating the lists, whether a name for that file be included is unspecified.</p> <p>If optional argument <em>topdown</em> is <code>True</code> or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). If <em>topdown</em> is <code>False</code>, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up). No matter the value of <em>topdown</em>, the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated.</p> <p>When <em>topdown</em> is <code>True</code>, the caller can modify the <em>dirnames</em> list in-place (perhaps using <a class="reference internal" href="../reference/simple_stmts#del"><code>del</code></a> or slice assignment), and <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> will only recurse into the subdirectories whose names remain in <em>dirnames</em>; this can be used to prune the search, impose a specific order of visiting, or even to inform <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> about directories the caller creates or renames before it resumes <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> again. Modifying <em>dirnames</em> when <em>topdown</em> is <code>False</code> has no effect on the behavior of the walk, because in bottom-up mode the directories in <em>dirnames</em> are generated before <em>dirpath</em> itself is generated.</p> <p>By default, errors from the <a class="reference internal" href="#os.scandir" title="os.scandir"><code>scandir()</code></a> call are ignored. If optional argument <em>onerror</em> is specified, it should be a function; it will be called with one argument, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the <code>filename</code> attribute of the exception object.</p> <p>By default, <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> will not walk down into symbolic links that resolve to directories. Set <em>followlinks</em> to <code>True</code> to visit directories pointed to by symlinks, on systems that support them.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Be aware that setting <em>followlinks</em> to <code>True</code> can lead to infinite recursion if a link points to a parent directory of itself. <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> does not keep track of the directories it visited already.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>If you pass a relative pathname, don’t change the current working directory between resumptions of <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a>. <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> never changes the current directory, and assumes that its caller doesn’t either.</p> </div> <p>This example displays the number of bytes taken by non-directory files in each directory under the starting directory, except that it doesn’t look under any CVS subdirectory:</p> <pre data-language="python">import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
</pre> <p>In the next example (simple implementation of <a class="reference internal" href="shutil#shutil.rmtree" title="shutil.rmtree"><code>shutil.rmtree()</code></a>), walking the tree bottom-up is essential, <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code>rmdir()</code></a> doesn’t allow deleting a directory before the directory is empty:</p> <pre data-language="python"># Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
</pre> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.walk</code> with arguments <code>top</code>, <code>topdown</code>, <code>onerror</code>, <code>followlinks</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>This function now calls <a class="reference internal" href="#os.scandir" title="os.scandir"><code>os.scandir()</code></a> instead of <a class="reference internal" href="#os.listdir" title="os.listdir"><code>os.listdir()</code></a>, making it faster by reducing the number of calls to <a class="reference internal" href="#os.stat" title="os.stat"><code>os.stat()</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fwalk">
<code>os.fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)</code> </dt> <dd>
<p id="index-32">This behaves exactly like <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a>, except that it yields a 4-tuple <code>(dirpath, dirnames, filenames, dirfd)</code>, and it supports <code>dir_fd</code>.</p> <p><em>dirpath</em>, <em>dirnames</em> and <em>filenames</em> are identical to <a class="reference internal" href="#os.walk" title="os.walk"><code>walk()</code></a> output, and <em>dirfd</em> is a file descriptor referring to the directory <em>dirpath</em>.</p> <p>This function always supports <a class="reference internal" href="#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>. Note however that, unlike other functions, the <a class="reference internal" href="#os.fwalk" title="os.fwalk"><code>fwalk()</code></a> default value for <em>follow_symlinks</em> is <code>False</code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Since <a class="reference internal" href="#os.fwalk" title="os.fwalk"><code>fwalk()</code></a> yields file descriptors, those are only valid until the next iteration step, so you should duplicate them (e.g. with <a class="reference internal" href="#os.dup" title="os.dup"><code>dup()</code></a>) if you want to keep them longer.</p> </div> <p>This example displays the number of bytes taken by non-directory files in each directory under the starting directory, except that it doesn’t look under any CVS subdirectory:</p> <pre data-language="python">import os
for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
print(root, "consumes", end="")
print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
</pre> <p>In the next example, walking the tree bottom-up is essential: <a class="reference internal" href="#os.rmdir" title="os.rmdir"><code>rmdir()</code></a> doesn’t allow deleting a directory before the directory is empty:</p> <pre data-language="python"># Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files, rootfd in os.fwalk(top, topdown=False):
for name in files:
os.unlink(name, dir_fd=rootfd)
for name in dirs:
os.rmdir(name, dir_fd=rootfd)
</pre> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.fwalk</code> with arguments <code>top</code>, <code>topdown</code>, <code>onerror</code>, <code>follow_symlinks</code>, <code>dir_fd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Added support for <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> paths.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.memfd_create">
<code>os.memfd_create(name[, flags=os.MFD_CLOEXEC])</code> </dt> <dd>
<p>Create an anonymous file and return a file descriptor that refers to it. <em>flags</em> must be one of the <code>os.MFD_*</code> constants available on the system (or a bitwise ORed combination of them). By default, the new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p> <p>The name supplied in <em>name</em> is used as a filename and will be displayed as the target of the corresponding symbolic link in the directory <code>/proc/self/fd/</code>. The displayed name is always prefixed with <code>memfd:</code> and serves only for debugging purposes. Names do not affect the behavior of the file descriptor, and as such multiple files can have the same name without any side effects.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 3.17 with glibc >= 2.27.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.MFD_CLOEXEC">
<code>os.MFD_CLOEXEC</code> </dt> <dt class="sig sig-object py" id="os.MFD_ALLOW_SEALING">
<code>os.MFD_ALLOW_SEALING</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGETLB">
<code>os.MFD_HUGETLB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_SHIFT">
<code>os.MFD_HUGE_SHIFT</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_MASK">
<code>os.MFD_HUGE_MASK</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_64KB">
<code>os.MFD_HUGE_64KB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_512KB">
<code>os.MFD_HUGE_512KB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_1MB">
<code>os.MFD_HUGE_1MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_2MB">
<code>os.MFD_HUGE_2MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_8MB">
<code>os.MFD_HUGE_8MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_16MB">
<code>os.MFD_HUGE_16MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_32MB">
<code>os.MFD_HUGE_32MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_256MB">
<code>os.MFD_HUGE_256MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_512MB">
<code>os.MFD_HUGE_512MB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_1GB">
<code>os.MFD_HUGE_1GB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_2GB">
<code>os.MFD_HUGE_2GB</code> </dt> <dt class="sig sig-object py" id="os.MFD_HUGE_16GB">
<code>os.MFD_HUGE_16GB</code> </dt> <dd>
<p>These flags can be passed to <a class="reference internal" href="#os.memfd_create" title="os.memfd_create"><code>memfd_create()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 3.17 with glibc >= 2.27</p> <p>The <code>MFD_HUGE*</code> flags are only available since Linux 4.14.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.eventfd">
<code>os.eventfd(initval[, flags=os.EFD_CLOEXEC])</code> </dt> <dd>
<p>Create and return an event file descriptor. The file descriptors supports raw <a class="reference internal" href="#os.read" title="os.read"><code>read()</code></a> and <a class="reference internal" href="#os.write" title="os.write"><code>write()</code></a> with a buffer size of 8, <a class="reference internal" href="select#select.select" title="select.select"><code>select()</code></a>, <a class="reference internal" href="select#select.poll" title="select.poll"><code>poll()</code></a> and similar. See man page <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/eventfd(2)">eventfd(2)</a></em> for more information. By default, the new file descriptor is <a class="reference internal" href="#fd-inheritance"><span class="std std-ref">non-inheritable</span></a>.</p> <p><em>initval</em> is the initial value of the event counter. The initial value must be an 32 bit unsigned integer. Please note that the initial value is limited to a 32 bit unsigned int although the event counter is an unsigned 64 bit integer with a maximum value of 2<sup>64</sup>-2.</p> <p><em>flags</em> can be constructed from <a class="reference internal" href="#os.EFD_CLOEXEC" title="os.EFD_CLOEXEC"><code>EFD_CLOEXEC</code></a>, <a class="reference internal" href="#os.EFD_NONBLOCK" title="os.EFD_NONBLOCK"><code>EFD_NONBLOCK</code></a>, and <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code>EFD_SEMAPHORE</code></a>.</p> <p>If <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code>EFD_SEMAPHORE</code></a> is specified and the event counter is non-zero, <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code>eventfd_read()</code></a> returns 1 and decrements the counter by one.</p> <p>If <a class="reference internal" href="#os.EFD_SEMAPHORE" title="os.EFD_SEMAPHORE"><code>EFD_SEMAPHORE</code></a> is not specified and the event counter is non-zero, <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code>eventfd_read()</code></a> returns the current event counter value and resets the counter to zero.</p> <p>If the event counter is zero and <a class="reference internal" href="#os.EFD_NONBLOCK" title="os.EFD_NONBLOCK"><code>EFD_NONBLOCK</code></a> is not specified, <a class="reference internal" href="#os.eventfd_read" title="os.eventfd_read"><code>eventfd_read()</code></a> blocks.</p> <p><a class="reference internal" href="#os.eventfd_write" title="os.eventfd_write"><code>eventfd_write()</code></a> increments the event counter. Write blocks if the write operation would increment the counter to a value larger than 2<sup>64</sup>-2.</p> <p>Example:</p> <pre data-language="python">import os
# semaphore with start value '1'
fd = os.eventfd(1, os.EFD_SEMAPHORE | os.EFC_CLOEXEC)
try:
# acquire semaphore
v = os.eventfd_read(fd)
try:
do_work()
finally:
# release semaphore
os.eventfd_write(fd, v)
finally:
os.close(fd)
</pre> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.27 with glibc >= 2.8</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.eventfd_read">
<code>os.eventfd_read(fd)</code> </dt> <dd>
<p>Read value from an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a> file descriptor and return a 64 bit unsigned int. The function does not verify that <em>fd</em> is an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.27</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.eventfd_write">
<code>os.eventfd_write(fd, value)</code> </dt> <dd>
<p>Add value to an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a> file descriptor. <em>value</em> must be a 64 bit unsigned int. The function does not verify that <em>fd</em> is an <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.27</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EFD_CLOEXEC">
<code>os.EFD_CLOEXEC</code> </dt> <dd>
<p>Set close-on-exec flag for new <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a> file descriptor.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.27</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EFD_NONBLOCK">
<code>os.EFD_NONBLOCK</code> </dt> <dd>
<p>Set <a class="reference internal" href="#os.O_NONBLOCK" title="os.O_NONBLOCK"><code>O_NONBLOCK</code></a> status flag for new <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a> file descriptor.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.27</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EFD_SEMAPHORE">
<code>os.EFD_SEMAPHORE</code> </dt> <dd>
<p>Provide semaphore-like semantics for reads from a <a class="reference internal" href="#os.eventfd" title="os.eventfd"><code>eventfd()</code></a> file descriptor. On read the internal counter is decremented by one.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 2.6.30</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd>
</dl> <section id="linux-extended-attributes"> <h3>Linux extended attributes</h3> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <p>These functions are all available on Linux only.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.getxattr">
<code>os.getxattr(path, attribute, *, follow_symlinks=True)</code> </dt> <dd>
<p>Return the value of the extended filesystem attribute <em>attribute</em> for <em>path</em>. <em>attribute</em> can be bytes or str (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface). If it is str, it is encoded with the filesystem encoding.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.getxattr</code> with arguments <code>path</code>, <code>attribute</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.listxattr">
<code>os.listxattr(path=None, *, follow_symlinks=True)</code> </dt> <dd>
<p>Return a list of the extended filesystem attributes on <em>path</em>. The attributes in the list are represented as strings decoded with the filesystem encoding. If <em>path</em> is <code>None</code>, <a class="reference internal" href="#os.listxattr" title="os.listxattr"><code>listxattr()</code></a> will examine the current directory.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.listxattr</code> with argument <code>path</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.removexattr">
<code>os.removexattr(path, attribute, *, follow_symlinks=True)</code> </dt> <dd>
<p>Removes the extended filesystem attribute <em>attribute</em> from <em>path</em>. <em>attribute</em> should be bytes or str (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface). If it is a string, it is encoded with the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.removexattr</code> with arguments <code>path</code>, <code>attribute</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.setxattr">
<code>os.setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)</code> </dt> <dd>
<p>Set the extended filesystem attribute <em>attribute</em> on <em>path</em> to <em>value</em>. <em>attribute</em> must be a bytes or str with no embedded NULs (directly or indirectly through the <a class="reference internal" href="#os.PathLike" title="os.PathLike"><code>PathLike</code></a> interface). If it is a str, it is encoded with the <a class="reference internal" href="../glossary#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>. <em>flags</em> may be <a class="reference internal" href="#os.XATTR_REPLACE" title="os.XATTR_REPLACE"><code>XATTR_REPLACE</code></a> or <a class="reference internal" href="#os.XATTR_CREATE" title="os.XATTR_CREATE"><code>XATTR_CREATE</code></a>. If <a class="reference internal" href="#os.XATTR_REPLACE" title="os.XATTR_REPLACE"><code>XATTR_REPLACE</code></a> is given and the attribute does not exist, <code>ENODATA</code> will be raised. If <a class="reference internal" href="#os.XATTR_CREATE" title="os.XATTR_CREATE"><code>XATTR_CREATE</code></a> is given and the attribute already exists, the attribute will not be created and <code>EEXISTS</code> will be raised.</p> <p>This function can support <a class="reference internal" href="#path-fd"><span class="std std-ref">specifying a file descriptor</span></a> and <a class="reference internal" href="#follow-symlinks"><span class="std std-ref">not following symlinks</span></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>A bug in Linux kernel versions less than 2.6.39 caused the flags argument to be ignored on some filesystems.</p> </div> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.setxattr</code> with arguments <code>path</code>, <code>attribute</code>, <code>value</code>, <code>flags</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for <em>path</em> and <em>attribute</em>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.XATTR_SIZE_MAX">
<code>os.XATTR_SIZE_MAX</code> </dt> <dd>
<p>The maximum size the value of an extended attribute can be. Currently, this is 64 KiB on Linux.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.XATTR_CREATE">
<code>os.XATTR_CREATE</code> </dt> <dd>
<p>This is a possible value for the flags argument in <a class="reference internal" href="#os.setxattr" title="os.setxattr"><code>setxattr()</code></a>. It indicates the operation must create an attribute.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.XATTR_REPLACE">
<code>os.XATTR_REPLACE</code> </dt> <dd>
<p>This is a possible value for the flags argument in <a class="reference internal" href="#os.setxattr" title="os.setxattr"><code>setxattr()</code></a>. It indicates the operation must replace an existing attribute.</p> </dd>
</dl> </section> </section> <section id="process-management"> <span id="os-process"></span><h2>Process Management</h2> <p>These functions may be used to create and manage processes.</p> <p>The various <a class="reference internal" href="#os.execl" title="os.execl"><code>exec*</code></a> functions take a list of arguments for the new program loaded into the process. In each case, the first of these arguments is passed to the new program as its own name rather than as an argument a user may have typed on a command line. For the C programmer, this is the <code>argv[0]</code> passed to a program’s <code>main()</code>. For example, <code>os.execv('/bin/echo',
['foo', 'bar'])</code> will only print <code>bar</code> on standard output; <code>foo</code> will seem to be ignored.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.abort">
<code>os.abort()</code> </dt> <dd>
<p>Generate a <code>SIGABRT</code> signal to the current process. On Unix, the default behavior is to produce a core dump; on Windows, the process immediately returns an exit code of <code>3</code>. Be aware that calling this function will not call the Python signal handler registered for <code>SIGABRT</code> with <a class="reference internal" href="signal#signal.signal" title="signal.signal"><code>signal.signal()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.add_dll_directory">
<code>os.add_dll_directory(path)</code> </dt> <dd>
<p>Add a path to the DLL search path.</p> <p>This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through <a class="reference internal" href="sys#sys.path" title="sys.path"><code>sys.path</code></a>), and also by <a class="reference internal" href="ctypes#module-ctypes" title="ctypes: A foreign function library for Python."><code>ctypes</code></a>.</p> <p>Remove the directory by calling <strong>close()</strong> on the returned object or using it in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement.</p> <p>See the <a class="reference external" href="https://msdn.microsoft.com/44228cf2-6306-466c-8f16-f513cd3ba8b5">Microsoft documentation</a> for more information about how DLLs are loaded.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.add_dll_directory</code> with argument <code>path</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>Previous versions of CPython would resolve DLLs using the default behavior for the current process. This led to inconsistencies, such as only sometimes searching <span class="target" id="index-33"></span><code>PATH</code> or the current working directory, and OS functions such as <code>AddDllDirectory</code> having no effect.</p> <p>In 3.8, the two primary ways DLLs are loaded now explicitly override the process-wide behavior to ensure consistency. See the <a class="reference internal" href="https://docs.python.org/3.12/whatsnew/3.8.html#bpo-36085-whatsnew"><span class="std std-ref">porting notes</span></a> for information on updating libraries.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.execl">
<code>os.execl(path, arg0, arg1, ...)</code> </dt> <dt class="sig sig-object py" id="os.execle">
<code>os.execle(path, arg0, arg1, ..., env)</code> </dt> <dt class="sig sig-object py" id="os.execlp">
<code>os.execlp(file, arg0, arg1, ...)</code> </dt> <dt class="sig sig-object py" id="os.execlpe">
<code>os.execlpe(file, arg0, arg1, ..., env)</code> </dt> <dt class="sig sig-object py" id="os.execv">
<code>os.execv(path, args)</code> </dt> <dt class="sig sig-object py" id="os.execve">
<code>os.execve(path, args, env)</code> </dt> <dt class="sig sig-object py" id="os.execvp">
<code>os.execvp(file, args)</code> </dt> <dt class="sig sig-object py" id="os.execvpe">
<code>os.execvpe(file, args, env)</code> </dt> <dd>
<p>These functions all execute a new program, replacing the current process; they do not return. On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Errors will be reported as <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> exceptions.</p> <p>The current process is replaced immediately. Open file objects and descriptors are not flushed, so if there may be data buffered on these open files, you should flush them using <code>sys.stdout.flush()</code> or <a class="reference internal" href="#os.fsync" title="os.fsync"><code>os.fsync()</code></a> before calling an <a class="reference internal" href="#os.execl" title="os.execl"><code>exec*</code></a> function.</p> <p>The “l” and “v” variants of the <a class="reference internal" href="#os.execl" title="os.execl"><code>exec*</code></a> functions differ in how command-line arguments are passed. The “l” variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the <code>execl*()</code> functions. The “v” variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the <em>args</em> parameter. In either case, the arguments to the child process should start with the name of the command being run, but this is not enforced.</p> <p>The variants which include a “p” near the end (<a class="reference internal" href="#os.execlp" title="os.execlp"><code>execlp()</code></a>, <a class="reference internal" href="#os.execlpe" title="os.execlpe"><code>execlpe()</code></a>, <a class="reference internal" href="#os.execvp" title="os.execvp"><code>execvp()</code></a>, and <a class="reference internal" href="#os.execvpe" title="os.execvpe"><code>execvpe()</code></a>) will use the <span class="target" id="index-34"></span><code>PATH</code> environment variable to locate the program <em>file</em>. When the environment is being replaced (using one of the <a class="reference internal" href="#os.execl" title="os.execl"><code>exec*e</code></a> variants, discussed in the next paragraph), the new environment is used as the source of the <span class="target" id="index-35"></span><code>PATH</code> variable. The other variants, <a class="reference internal" href="#os.execl" title="os.execl"><code>execl()</code></a>, <a class="reference internal" href="#os.execle" title="os.execle"><code>execle()</code></a>, <a class="reference internal" href="#os.execv" title="os.execv"><code>execv()</code></a>, and <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a>, will not use the <span class="target" id="index-36"></span><code>PATH</code> variable to locate the executable; <em>path</em> must contain an appropriate absolute or relative path. Relative paths must include at least one slash, even on Windows, as plain names will not be resolved.</p> <p>For <a class="reference internal" href="#os.execle" title="os.execle"><code>execle()</code></a>, <a class="reference internal" href="#os.execlpe" title="os.execlpe"><code>execlpe()</code></a>, <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a>, and <a class="reference internal" href="#os.execvpe" title="os.execvpe"><code>execvpe()</code></a> (note that these all end in “e”), the <em>env</em> parameter must be a mapping which is used to define the environment variables for the new process (these are used instead of the current process’ environment); the functions <a class="reference internal" href="#os.execl" title="os.execl"><code>execl()</code></a>, <a class="reference internal" href="#os.execlp" title="os.execlp"><code>execlp()</code></a>, <a class="reference internal" href="#os.execv" title="os.execv"><code>execv()</code></a>, and <a class="reference internal" href="#os.execvp" title="os.execvp"><code>execvp()</code></a> all cause the new process to inherit the environment of the current process.</p> <p>For <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a> on some platforms, <em>path</em> may also be specified as an open file descriptor. This functionality may not be supported on your platform; you can check whether or not it is available using <a class="reference internal" href="#os.supports_fd" title="os.supports_fd"><code>os.supports_fd</code></a>. If it is unavailable, using it will raise a <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.exec</code> with arguments <code>path</code>, <code>args</code>, <code>env</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Added support for specifying <em>path</em> as an open file descriptor for <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os._exit">
<code>os._exit(n)</code> </dt> <dd>
<p>Exit the process with status <em>n</em>, without calling cleanup handlers, flushing stdio buffers, etc.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The standard way to exit is <a class="reference internal" href="sys#sys.exit" title="sys.exit"><code>sys.exit(n)</code></a>. <code>_exit()</code> should normally only be used in the child process after a <a class="reference internal" href="#os.fork" title="os.fork"><code>fork()</code></a>.</p> </div> </dd>
</dl> <p>The following exit codes are defined and can be used with <a class="reference internal" href="#os._exit" title="os._exit"><code>_exit()</code></a>, although they are not required. These are typically used for system programs written in Python, such as a mail server’s external command delivery program.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Some of these may not be available on all Unix platforms, since there is some variation. These constants are defined where they are defined by the underlying platform.</p> </div> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_OK">
<code>os.EX_OK</code> </dt> <dd>
<p>Exit code that means no error occurred. May be taken from the defined value of <code>EXIT_SUCCESS</code> on some platforms. Generally has a value of zero.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_USAGE">
<code>os.EX_USAGE</code> </dt> <dd>
<p>Exit code that means the command was used incorrectly, such as when the wrong number of arguments are given.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_DATAERR">
<code>os.EX_DATAERR</code> </dt> <dd>
<p>Exit code that means the input data was incorrect.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_NOINPUT">
<code>os.EX_NOINPUT</code> </dt> <dd>
<p>Exit code that means an input file did not exist or was not readable.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_NOUSER">
<code>os.EX_NOUSER</code> </dt> <dd>
<p>Exit code that means a specified user did not exist.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_NOHOST">
<code>os.EX_NOHOST</code> </dt> <dd>
<p>Exit code that means a specified host did not exist.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_UNAVAILABLE">
<code>os.EX_UNAVAILABLE</code> </dt> <dd>
<p>Exit code that means that a required service is unavailable.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_SOFTWARE">
<code>os.EX_SOFTWARE</code> </dt> <dd>
<p>Exit code that means an internal software error was detected.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_OSERR">
<code>os.EX_OSERR</code> </dt> <dd>
<p>Exit code that means an operating system error was detected, such as the inability to fork or create a pipe.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_OSFILE">
<code>os.EX_OSFILE</code> </dt> <dd>
<p>Exit code that means some system file did not exist, could not be opened, or had some other kind of error.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_CANTCREAT">
<code>os.EX_CANTCREAT</code> </dt> <dd>
<p>Exit code that means a user specified output file could not be created.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_IOERR">
<code>os.EX_IOERR</code> </dt> <dd>
<p>Exit code that means that an error occurred while doing I/O on some file.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_TEMPFAIL">
<code>os.EX_TEMPFAIL</code> </dt> <dd>
<p>Exit code that means a temporary failure occurred. This indicates something that may not really be an error, such as a network connection that couldn’t be made during a retryable operation.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_PROTOCOL">
<code>os.EX_PROTOCOL</code> </dt> <dd>
<p>Exit code that means that a protocol exchange was illegal, invalid, or not understood.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_NOPERM">
<code>os.EX_NOPERM</code> </dt> <dd>
<p>Exit code that means that there were insufficient permissions to perform the operation (but not intended for file system problems).</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_CONFIG">
<code>os.EX_CONFIG</code> </dt> <dd>
<p>Exit code that means that some kind of configuration error occurred.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.EX_NOTFOUND">
<code>os.EX_NOTFOUND</code> </dt> <dd>
<p>Exit code that means something like “an entry was not found”.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.fork">
<code>os.fork()</code> </dt> <dd>
<p>Fork a child process. Return <code>0</code> in the child and the child’s process id in the parent. If an error occurs <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised.</p> <p>Note that some platforms including FreeBSD <= 6.3 and Cygwin have known issues when using <code>fork()</code> from a thread.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.fork</code> with no arguments.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>If you use TLS sockets in an application calling <code>fork()</code>, see the warning in the <a class="reference internal" href="ssl#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code>ssl</code></a> documentation.</p> </div> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>On macOS the use of this function is unsafe when mixed with using higher-level system APIs, and that includes using <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Calling <code>fork()</code> in a subinterpreter is no longer supported (<a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised).</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>If Python is able to detect that your process has multiple threads, <a class="reference internal" href="#os.fork" title="os.fork"><code>os.fork()</code></a> now raises a <a class="reference internal" href="exceptions#DeprecationWarning" title="DeprecationWarning"><code>DeprecationWarning</code></a>.</p> <p>We chose to surface this as a warning, when detectable, to better inform developers of a design problem that the POSIX platform specifically notes as not supported. Even in code that <em>appears</em> to work, it has never been safe to mix threading with <a class="reference internal" href="#os.fork" title="os.fork"><code>os.fork()</code></a> on POSIX platforms. The CPython runtime itself has always made API calls that are not safe for use in the child process when threads existed in the parent (such as <code>malloc</code> and <code>free</code>).</p> <p>Users of macOS or users of libc or malloc implementations other than those typically found in glibc to date are among those already more likely to experience deadlocks running such code.</p> <p>See <a class="reference external" href="https://discuss.python.org/t/33555">this discussion on fork being incompatible with threads</a> for technical details of why we’re surfacing this longstanding platform compatibility problem to developers.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.forkpty">
<code>os.forkpty()</code> </dt> <dd>
<p>Fork a child process, using a new pseudo-terminal as the child’s controlling terminal. Return a pair of <code>(pid, fd)</code>, where <em>pid</em> is <code>0</code> in the child, the new child’s process id in the parent, and <em>fd</em> is the file descriptor of the master end of the pseudo-terminal. For a more portable approach, use the <a class="reference internal" href="pty#module-pty" title="pty: Pseudo-Terminal Handling for Unix. (Unix)"><code>pty</code></a> module. If an error occurs <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.forkpty</code> with no arguments.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>On macOS the use of this function is unsafe when mixed with using higher-level system APIs, and that includes using <a class="reference internal" href="urllib.request#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code>urllib.request</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>If Python is able to detect that your process has multiple threads, this now raises a <a class="reference internal" href="exceptions#DeprecationWarning" title="DeprecationWarning"><code>DeprecationWarning</code></a>. See the longer explanation on <a class="reference internal" href="#os.fork" title="os.fork"><code>os.fork()</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Calling <code>forkpty()</code> in a subinterpreter is no longer supported (<a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised).</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.kill">
<code>os.kill(pid, sig, /)</code> </dt> <dd>
<p id="index-37">Send signal <em>sig</em> to the process <em>pid</em>. Constants for the specific signals available on the host platform are defined in the <a class="reference internal" href="signal#module-signal" title="signal: Set handlers for asynchronous events."><code>signal</code></a> module.</p> <p>Windows: The <a class="reference internal" href="signal#signal.CTRL_C_EVENT" title="signal.CTRL_C_EVENT"><code>signal.CTRL_C_EVENT</code></a> and <a class="reference internal" href="signal#signal.CTRL_BREAK_EVENT" title="signal.CTRL_BREAK_EVENT"><code>signal.CTRL_BREAK_EVENT</code></a> signals are special signals which can only be sent to console processes which share a common console window, e.g., some subprocesses. Any other value for <em>sig</em> will cause the process to be unconditionally killed by the TerminateProcess API, and the exit code will be set to <em>sig</em>. The Windows version of <a class="reference internal" href="#os.kill" title="os.kill"><code>kill()</code></a> additionally takes process handles to be killed.</p> <p>See also <a class="reference internal" href="signal#signal.pthread_kill" title="signal.pthread_kill"><code>signal.pthread_kill()</code></a>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.kill</code> with arguments <code>pid</code>, <code>sig</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2: </span>Windows support.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.killpg">
<code>os.killpg(pgid, sig, /)</code> </dt> <dd>
<p id="index-38">Send the signal <em>sig</em> to the process group <em>pgid</em>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.killpg</code> with arguments <code>pgid</code>, <code>sig</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.nice">
<code>os.nice(increment, /)</code> </dt> <dd>
<p>Add <em>increment</em> to the process’s “niceness”. Return the new niceness.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.pidfd_open">
<code>os.pidfd_open(pid, flags=0)</code> </dt> <dd>
<p>Return a file descriptor referring to the process <em>pid</em> with <em>flags</em> set. This descriptor can be used to perform process management without races and signals.</p> <p>See the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/pidfd_open(2)">pidfd_open(2)</a></em> man page for more details.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 5.3</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> <dl class="py data"> <dt class="sig sig-object py" id="os.PIDFD_NONBLOCK">
<code>os.PIDFD_NONBLOCK</code> </dt> <dd>
<p>This flag indicates that the file descriptor will be non-blocking. If the process referred to by the file descriptor has not yet terminated, then an attempt to wait on the file descriptor using <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/waitid(2)">waitid(2)</a></em> will immediately return the error <a class="reference internal" href="errno#errno.EAGAIN" title="errno.EAGAIN"><code>EAGAIN</code></a> rather than blocking.</p> </dd>
</dl> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 5.10</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.12.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.plock">
<code>os.plock(op, /)</code> </dt> <dd>
<p>Lock program segments into memory. The value of <em>op</em> (defined in <code><sys/lock.h></code>) determines which segments are locked.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.popen">
<code>os.popen(cmd, mode='r', buffering=- 1)</code> </dt> <dd>
<p>Open a pipe to or from command <em>cmd</em>. The return value is an open file object connected to the pipe, which can be read or written depending on whether <em>mode</em> is <code>'r'</code> (default) or <code>'w'</code>. The <em>buffering</em> argument have the same meaning as the corresponding argument to the built-in <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> function. The returned file object reads or writes text strings rather than bytes.</p> <p>The <code>close</code> method returns <a class="reference internal" href="constants#None" title="None"><code>None</code></a> if the subprocess exited successfully, or the subprocess’s return code if there was an error. On POSIX systems, if the return code is positive it represents the return value of the process left-shifted by one byte. If the return code is negative, the process was terminated by the signal given by the negated value of the return code. (For example, the return value might be <code>- signal.SIGKILL</code> if the subprocess was killed.) On Windows systems, the return value contains the signed integer return code from the child process.</p> <p>On Unix, <a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the <code>close</code> method result (exit status) into an exit code if it is not <code>None</code>. On Windows, the <code>close</code> method result is directly the exit code (or <code>None</code>).</p> <p>This is implemented using <a class="reference internal" href="subprocess#subprocess.Popen" title="subprocess.Popen"><code>subprocess.Popen</code></a>; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The <a class="reference internal" href="#utf8-mode"><span class="std std-ref">Python UTF-8 Mode</span></a> affects encodings used for <em>cmd</em> and pipe contents.</p> <p><a class="reference internal" href="#os.popen" title="os.popen"><code>popen()</code></a> is a simple wrapper around <a class="reference internal" href="subprocess#subprocess.Popen" title="subprocess.Popen"><code>subprocess.Popen</code></a>. Use <a class="reference internal" href="subprocess#subprocess.Popen" title="subprocess.Popen"><code>subprocess.Popen</code></a> or <a class="reference internal" href="subprocess#subprocess.run" title="subprocess.run"><code>subprocess.run()</code></a> to control options like encodings.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.posix_spawn">
<code>os.posix_spawn(path, argv, env, *, file_actions=None, setpgroup=None, resetids=False, setsid=False, setsigmask=(), setsigdef=(), scheduler=None)</code> </dt> <dd>
<p>Wraps the <code>posix_spawn()</code> C library API for use from Python.</p> <p>Most users should use <a class="reference internal" href="subprocess#subprocess.run" title="subprocess.run"><code>subprocess.run()</code></a> instead of <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code>posix_spawn()</code></a>.</p> <p>The positional-only arguments <em>path</em>, <em>args</em>, and <em>env</em> are similar to <a class="reference internal" href="#os.execve" title="os.execve"><code>execve()</code></a>.</p> <p>The <em>path</em> parameter is the path to the executable file. The <em>path</em> should contain a directory. Use <a class="reference internal" href="#os.posix_spawnp" title="os.posix_spawnp"><code>posix_spawnp()</code></a> to pass an executable file without directory.</p> <p>The <em>file_actions</em> argument may be a sequence of tuples describing actions to take on specific file descriptors in the child process between the C library implementation’s <code>fork()</code> and <code>exec()</code> steps. The first item in each tuple must be one of the three type indicator listed below describing the remaining tuple elements:</p> <dl class="py data"> <dt class="sig sig-object py" id="os.POSIX_SPAWN_OPEN">
<code>os.POSIX_SPAWN_OPEN</code> </dt> <dd>
<p>(<code>os.POSIX_SPAWN_OPEN</code>, <em>fd</em>, <em>path</em>, <em>flags</em>, <em>mode</em>)</p> <p>Performs <code>os.dup2(os.open(path, flags, mode), fd)</code>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.POSIX_SPAWN_CLOSE">
<code>os.POSIX_SPAWN_CLOSE</code> </dt> <dd>
<p>(<code>os.POSIX_SPAWN_CLOSE</code>, <em>fd</em>)</p> <p>Performs <code>os.close(fd)</code>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.POSIX_SPAWN_DUP2">
<code>os.POSIX_SPAWN_DUP2</code> </dt> <dd>
<p>(<code>os.POSIX_SPAWN_DUP2</code>, <em>fd</em>, <em>new_fd</em>)</p> <p>Performs <code>os.dup2(fd, new_fd)</code>.</p> </dd>
</dl> <p>These tuples correspond to the C library <code>posix_spawn_file_actions_addopen()</code>, <code>posix_spawn_file_actions_addclose()</code>, and <code>posix_spawn_file_actions_adddup2()</code> API calls used to prepare for the <code>posix_spawn()</code> call itself.</p> <p>The <em>setpgroup</em> argument will set the process group of the child to the value specified. If the value specified is 0, the child’s process group ID will be made the same as its process ID. If the value of <em>setpgroup</em> is not set, the child will inherit the parent’s process group ID. This argument corresponds to the C library <code>POSIX_SPAWN_SETPGROUP</code> flag.</p> <p>If the <em>resetids</em> argument is <code>True</code> it will reset the effective UID and GID of the child to the real UID and GID of the parent process. If the argument is <code>False</code>, then the child retains the effective UID and GID of the parent. In either case, if the set-user-ID and set-group-ID permission bits are enabled on the executable file, their effect will override the setting of the effective UID and GID. This argument corresponds to the C library <code>POSIX_SPAWN_RESETIDS</code> flag.</p> <p>If the <em>setsid</em> argument is <code>True</code>, it will create a new session ID for <code>posix_spawn</code>. <em>setsid</em> requires <code>POSIX_SPAWN_SETSID</code> or <code>POSIX_SPAWN_SETSID_NP</code> flag. Otherwise, <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> is raised.</p> <p>The <em>setsigmask</em> argument will set the signal mask to the signal set specified. If the parameter is not used, then the child inherits the parent’s signal mask. This argument corresponds to the C library <code>POSIX_SPAWN_SETSIGMASK</code> flag.</p> <p>The <em>sigdef</em> argument will reset the disposition of all signals in the set specified. This argument corresponds to the C library <code>POSIX_SPAWN_SETSIGDEF</code> flag.</p> <p>The <em>scheduler</em> argument must be a tuple containing the (optional) scheduler policy and an instance of <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code>sched_param</code></a> with the scheduler parameters. A value of <code>None</code> in the place of the scheduler policy indicates that is not being provided. This argument is a combination of the C library <code>POSIX_SPAWN_SETSCHEDPARAM</code> and <code>POSIX_SPAWN_SETSCHEDULER</code> flags.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.posix_spawn</code> with arguments <code>path</code>, <code>argv</code>, <code>env</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.posix_spawnp">
<code>os.posix_spawnp(path, argv, env, *, file_actions=None, setpgroup=None, resetids=False, setsid=False, setsigmask=(), setsigdef=(), scheduler=None)</code> </dt> <dd>
<p>Wraps the <code>posix_spawnp()</code> C library API for use from Python.</p> <p>Similar to <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code>posix_spawn()</code></a> except that the system searches for the <em>executable</em> file in the list of directories specified by the <span class="target" id="index-39"></span><code>PATH</code> environment variable (in the same way as for <code>execvp(3)</code>).</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.posix_spawn</code> with arguments <code>path</code>, <code>argv</code>, <code>env</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: POSIX, not Emscripten, not WASI.</p> <p>See <a class="reference internal" href="#os.posix_spawn" title="os.posix_spawn"><code>posix_spawn()</code></a> documentation.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.register_at_fork">
<code>os.register_at_fork(*, before=None, after_in_parent=None, after_in_child=None)</code> </dt> <dd>
<p>Register callables to be executed when a new child process is forked using <a class="reference internal" href="#os.fork" title="os.fork"><code>os.fork()</code></a> or similar process cloning APIs. The parameters are optional and keyword-only. Each specifies a different call point.</p> <ul class="simple"> <li>
<em>before</em> is a function called before forking a child process.</li> <li>
<em>after_in_parent</em> is a function called from the parent process after forking a child process.</li> <li>
<em>after_in_child</em> is a function called from the child process.</li> </ul> <p>These calls are only made if control is expected to return to the Python interpreter. A typical <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> launch will not trigger them as the child is not going to re-enter the interpreter.</p> <p>Functions registered for execution before forking are called in reverse registration order. Functions registered for execution after forking (either in the parent or in the child) are called in registration order.</p> <p>Note that <code>fork()</code> calls made by third-party C code may not call those functions, unless it explicitly calls <a class="reference internal" href="../c-api/sys#c.PyOS_BeforeFork" title="PyOS_BeforeFork"><code>PyOS_BeforeFork()</code></a>, <a class="reference internal" href="../c-api/sys#c.PyOS_AfterFork_Parent" title="PyOS_AfterFork_Parent"><code>PyOS_AfterFork_Parent()</code></a> and <a class="reference internal" href="../c-api/sys#c.PyOS_AfterFork_Child" title="PyOS_AfterFork_Child"><code>PyOS_AfterFork_Child()</code></a>.</p> <p>There is no way to unregister a function.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.7.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.spawnl">
<code>os.spawnl(mode, path, ...)</code> </dt> <dt class="sig sig-object py" id="os.spawnle">
<code>os.spawnle(mode, path, ..., env)</code> </dt> <dt class="sig sig-object py" id="os.spawnlp">
<code>os.spawnlp(mode, file, ...)</code> </dt> <dt class="sig sig-object py" id="os.spawnlpe">
<code>os.spawnlpe(mode, file, ..., env)</code> </dt> <dt class="sig sig-object py" id="os.spawnv">
<code>os.spawnv(mode, path, args)</code> </dt> <dt class="sig sig-object py" id="os.spawnve">
<code>os.spawnve(mode, path, args, env)</code> </dt> <dt class="sig sig-object py" id="os.spawnvp">
<code>os.spawnvp(mode, file, args)</code> </dt> <dt class="sig sig-object py" id="os.spawnvpe">
<code>os.spawnvpe(mode, file, args, env)</code> </dt> <dd>
<p>Execute the program <em>path</em> in a new process.</p> <p>(Note that the <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions. Check especially the <a class="reference internal" href="subprocess#subprocess-replacements"><span class="std std-ref">Replacing Older Functions with the subprocess Module</span></a> section.)</p> <p>If <em>mode</em> is <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code>P_NOWAIT</code></a>, this function returns the process id of the new process; if <em>mode</em> is <a class="reference internal" href="#os.P_WAIT" title="os.P_WAIT"><code>P_WAIT</code></a>, returns the process’s exit code if it exits normally, or <code>-signal</code>, where <em>signal</em> is the signal that killed the process. On Windows, the process id will actually be the process handle, so can be used with the <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> function.</p> <p>Note on VxWorks, this function doesn’t return <code>-signal</code> when the new process is killed. Instead it raises OSError exception.</p> <p>The “l” and “v” variants of the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> functions differ in how command-line arguments are passed. The “l” variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the <code>spawnl*()</code> functions. The “v” variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the <em>args</em> parameter. In either case, the arguments to the child process must start with the name of the command being run.</p> <p>The variants which include a second “p” near the end (<a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code>spawnlp()</code></a>, <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code>spawnlpe()</code></a>, <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code>spawnvp()</code></a>, and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code>spawnvpe()</code></a>) will use the <span class="target" id="index-40"></span><code>PATH</code> environment variable to locate the program <em>file</em>. When the environment is being replaced (using one of the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*e</code></a> variants, discussed in the next paragraph), the new environment is used as the source of the <span class="target" id="index-41"></span><code>PATH</code> variable. The other variants, <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawnl()</code></a>, <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code>spawnle()</code></a>, <a class="reference internal" href="#os.spawnv" title="os.spawnv"><code>spawnv()</code></a>, and <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code>spawnve()</code></a>, will not use the <span class="target" id="index-42"></span><code>PATH</code> variable to locate the executable; <em>path</em> must contain an appropriate absolute or relative path.</p> <p>For <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code>spawnle()</code></a>, <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code>spawnlpe()</code></a>, <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code>spawnve()</code></a>, and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code>spawnvpe()</code></a> (note that these all end in “e”), the <em>env</em> parameter must be a mapping which is used to define the environment variables for the new process (they are used instead of the current process’ environment); the functions <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawnl()</code></a>, <a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code>spawnlp()</code></a>, <a class="reference internal" href="#os.spawnv" title="os.spawnv"><code>spawnv()</code></a>, and <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code>spawnvp()</code></a> all cause the new process to inherit the environment of the current process. Note that keys and values in the <em>env</em> dictionary must be strings; invalid keys or values will cause the function to fail, with a return value of <code>127</code>.</p> <p>As an example, the following calls to <a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code>spawnlp()</code></a> and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code>spawnvpe()</code></a> are equivalent:</p> <pre data-language="python">import os
os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
L = ['cp', 'index.html', '/dev/null']
os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
</pre> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.spawn</code> with arguments <code>mode</code>, <code>path</code>, <code>args</code>, <code>env</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> <p><a class="reference internal" href="#os.spawnlp" title="os.spawnlp"><code>spawnlp()</code></a>, <a class="reference internal" href="#os.spawnlpe" title="os.spawnlpe"><code>spawnlpe()</code></a>, <a class="reference internal" href="#os.spawnvp" title="os.spawnvp"><code>spawnvp()</code></a> and <a class="reference internal" href="#os.spawnvpe" title="os.spawnvpe"><code>spawnvpe()</code></a> are not available on Windows. <a class="reference internal" href="#os.spawnle" title="os.spawnle"><code>spawnle()</code></a> and <a class="reference internal" href="#os.spawnve" title="os.spawnve"><code>spawnve()</code></a> are not thread-safe on Windows; we advise you to use the <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> module instead.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.P_NOWAIT">
<code>os.P_NOWAIT</code> </dt> <dt class="sig sig-object py" id="os.P_NOWAITO">
<code>os.P_NOWAITO</code> </dt> <dd>
<p>Possible values for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> family of functions. If either of these values is given, the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> functions will return as soon as the new process has been created, with the process id as the return value.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.P_WAIT">
<code>os.P_WAIT</code> </dt> <dd>
<p>Possible value for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> family of functions. If this is given as <em>mode</em>, the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> functions will not return until the new process has run to completion and will return the exit code of the process the run is successful, or <code>-signal</code> if a signal kills the process.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.P_DETACH">
<code>os.P_DETACH</code> </dt> <dt class="sig sig-object py" id="os.P_OVERLAY">
<code>os.P_OVERLAY</code> </dt> <dd>
<p>Possible values for the <em>mode</em> parameter to the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> family of functions. These are less portable than those listed above. <a class="reference internal" href="#os.P_DETACH" title="os.P_DETACH"><code>P_DETACH</code></a> is similar to <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code>P_NOWAIT</code></a>, but the new process is detached from the console of the calling process. If <a class="reference internal" href="#os.P_OVERLAY" title="os.P_OVERLAY"><code>P_OVERLAY</code></a> is used, the current process will be replaced; the <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> function will not return.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.startfile">
<code>os.startfile(path[, operation][, arguments][, cwd][, show_cmd])</code> </dt> <dd>
<p>Start a file with its associated application.</p> <p>When <em>operation</em> is not specified, this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the <strong class="program">start</strong> command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.</p> <p>When another <em>operation</em> is given, it must be a “command verb” that specifies what should be done with the file. Common verbs documented by Microsoft are <code>'open'</code>, <code>'print'</code> and <code>'edit'</code> (to be used on files) as well as <code>'explore'</code> and <code>'find'</code> (to be used on directories).</p> <p>When launching an application, specify <em>arguments</em> to be passed as a single string. This argument may have no effect when using this function to launch a document.</p> <p>The default working directory is inherited, but may be overridden by the <em>cwd</em> argument. This should be an absolute path. A relative <em>path</em> will be resolved against this argument.</p> <p>Use <em>show_cmd</em> to override the default window style. Whether this has any effect will depend on the application being launched. Values are integers as supported by the Win32 <code>ShellExecute()</code> function.</p> <p><a class="reference internal" href="#os.startfile" title="os.startfile"><code>startfile()</code></a> returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application’s exit status. The <em>path</em> parameter is relative to the current directory or <em>cwd</em>. If you want to use an absolute path, make sure the first character is not a slash (<code>'/'</code>) Use <a class="reference internal" href="pathlib#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code>pathlib</code></a> or the <a class="reference internal" href="os.path#os.path.normpath" title="os.path.normpath"><code>os.path.normpath()</code></a> function to ensure that paths are properly encoded for Win32.</p> <p>To reduce interpreter startup overhead, the Win32 <code>ShellExecute()</code> function is not resolved until this function is first called. If the function cannot be resolved, <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> will be raised.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.startfile</code> with arguments <code>path</code>, <code>operation</code>.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.startfile/2</code> with arguments <code>path</code>, <code>operation</code>, <code>arguments</code>, <code>cwd</code>, <code>show_cmd</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>arguments</em>, <em>cwd</em> and <em>show_cmd</em> arguments, and the <code>os.startfile/2</code> audit event.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.system">
<code>os.system(command)</code> </dt> <dd>
<p>Execute the command (a string) in a subshell. This is implemented by calling the Standard C function <code>system()</code>, and has the same limitations. Changes to <a class="reference internal" href="sys#sys.stdin" title="sys.stdin"><code>sys.stdin</code></a>, etc. are not reflected in the environment of the executed command. If <em>command</em> generates any output, it will be sent to the interpreter standard output stream. The C standard does not specify the meaning of the return value of the C function, so the return value of the Python function is system-dependent.</p> <p>On Unix, the return value is the exit status of the process encoded in the format specified for <a class="reference internal" href="#os.wait" title="os.wait"><code>wait()</code></a>.</p> <p>On Windows, the return value is that returned by the system shell after running <em>command</em>. The shell is given by the Windows environment variable <span class="target" id="index-43"></span><code>COMSPEC</code>: it is usually <strong class="program">cmd.exe</strong>, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.</p> <p>The <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the <a class="reference internal" href="subprocess#subprocess-replacements"><span class="std std-ref">Replacing Older Functions with the subprocess Module</span></a> section in the <a class="reference internal" href="subprocess#module-subprocess" title="subprocess: Subprocess management."><code>subprocess</code></a> documentation for some helpful recipes.</p> <p>On Unix, <a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the result (exit status) into an exit code. On Windows, the result is directly the exit code.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>os.system</code> with argument <code>command</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.times">
<code>os.times()</code> </dt> <dd>
<p>Returns the current global process times. The return value is an object with five attributes:</p> <ul class="simple"> <li>
<code>user</code> - user time</li> <li>
<code>system</code> - system time</li> <li>
<code>children_user</code> - user time of all child processes</li> <li>
<code>children_system</code> - system time of all child processes</li> <li>
<code>elapsed</code> - elapsed real time since a fixed point in the past</li> </ul> <p>For backwards compatibility, this object also behaves like a five-tuple containing <code>user</code>, <code>system</code>, <code>children_user</code>, <code>children_system</code>, and <code>elapsed</code> in that order.</p> <p>See the Unix manual page <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/times(2)">times(2)</a></em> and <a class="reference external" href="https://man.freebsd.org/cgi/man.cgi?time(3)">times(3)</a> manual page on Unix or <a class="reference external" href="https://docs.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesstimes">the GetProcessTimes MSDN</a> on Windows. On Windows, only <code>user</code> and <code>system</code> are known; the other attributes are zero.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Return type changed from a tuple to a tuple-like object with named attributes.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.wait">
<code>os.wait()</code> </dt> <dd>
<p>Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced.</p> <p>If there are no children that could be waited for, <a class="reference internal" href="exceptions#ChildProcessError" title="ChildProcessError"><code>ChildProcessError</code></a> is raised.</p> <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the exit status into an exit code.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The other <code>wait*()</code> functions documented below can be used to wait for the completion of a specific child process and have more options. <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> is the only one also available on Windows.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.waitid">
<code>os.waitid(idtype, id, options, /)</code> </dt> <dd>
<p>Wait for the completion of a child process.</p> <p><em>idtype</em> can be <a class="reference internal" href="#os.P_PID" title="os.P_PID"><code>P_PID</code></a>, <a class="reference internal" href="#os.P_PGID" title="os.P_PGID"><code>P_PGID</code></a>, <a class="reference internal" href="#os.P_ALL" title="os.P_ALL"><code>P_ALL</code></a>, or (on Linux) <a class="reference internal" href="#os.P_PIDFD" title="os.P_PIDFD"><code>P_PIDFD</code></a>. The interpretation of <em>id</em> depends on it; see their individual descriptions.</p> <p><em>options</em> is an OR combination of flags. At least one of <a class="reference internal" href="#os.WEXITED" title="os.WEXITED"><code>WEXITED</code></a>, <a class="reference internal" href="#os.WSTOPPED" title="os.WSTOPPED"><code>WSTOPPED</code></a> or <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code>WCONTINUED</code></a> is required; <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code>WNOHANG</code></a> and <a class="reference internal" href="#os.WNOWAIT" title="os.WNOWAIT"><code>WNOWAIT</code></a> are additional optional flags.</p> <p>The return value is an object representing the data contained in the <code>siginfo_t</code> structure with the following attributes:</p> <ul class="simple"> <li>
<code>si_pid</code> (process ID)</li> <li>
<code>si_uid</code> (real user ID of the child)</li> <li>
<code>si_signo</code> (always <a class="reference internal" href="signal#signal.SIGCHLD" title="signal.SIGCHLD"><code>SIGCHLD</code></a>)</li> <li>
<code>si_status</code> (the exit status or signal number, depending on <code>si_code</code>)</li> <li>
<code>si_code</code> (see <a class="reference internal" href="#os.CLD_EXITED" title="os.CLD_EXITED"><code>CLD_EXITED</code></a> for possible values)</li> </ul> <p>If <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code>WNOHANG</code></a> is specified and there are no matching children in the requested state, <code>None</code> is returned. Otherwise, if there are no matching children that could be waited for, <a class="reference internal" href="exceptions#ChildProcessError" title="ChildProcessError"><code>ChildProcessError</code></a> is raised.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function is not available on macOS.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.waitpid">
<code>os.waitpid(pid, options, /)</code> </dt> <dd>
<p>The details of this function differ on Unix and Windows.</p> <p>On Unix: Wait for completion of a child process given by process id <em>pid</em>, and return a tuple containing its process id and exit status indication (encoded as for <a class="reference internal" href="#os.wait" title="os.wait"><code>wait()</code></a>). The semantics of the call are affected by the value of the integer <em>options</em>, which should be <code>0</code> for normal operation.</p> <p>If <em>pid</em> is greater than <code>0</code>, <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> requests status information for that specific process. If <em>pid</em> is <code>0</code>, the request is for the status of any child in the process group of the current process. If <em>pid</em> is <code>-1</code>, the request pertains to any child of the current process. If <em>pid</em> is less than <code>-1</code>, status is requested for any process in the process group <code>-pid</code> (the absolute value of <em>pid</em>).</p> <p><em>options</em> is an OR combination of flags. If it contains <a class="reference internal" href="#os.WNOHANG" title="os.WNOHANG"><code>WNOHANG</code></a> and there are no matching children in the requested state, <code>(0, 0)</code> is returned. Otherwise, if there are no matching children that could be waited for, <a class="reference internal" href="exceptions#ChildProcessError" title="ChildProcessError"><code>ChildProcessError</code></a> is raised. Other options that can be used are <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code>WUNTRACED</code></a> and <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code>WCONTINUED</code></a>.</p> <p>On Windows: Wait for completion of a process given by process handle <em>pid</em>, and return a tuple containing <em>pid</em>, and its exit status shifted left by 8 bits (shifting makes cross-platform use of the function easier). A <em>pid</em> less than or equal to <code>0</code> has no special meaning on Windows, and raises an exception. The value of integer <em>options</em> has no effect. <em>pid</em> can refer to any process whose id is known, not necessarily a child process. The <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*</code></a> functions called with <a class="reference internal" href="#os.P_NOWAIT" title="os.P_NOWAIT"><code>P_NOWAIT</code></a> return suitable process handles.</p> <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the exit status into an exit code.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an <a class="reference internal" href="exceptions#InterruptedError" title="InterruptedError"><code>InterruptedError</code></a> exception (see <span class="target" id="index-44"></span><a class="pep reference external" href="https://peps.python.org/pep-0475/"><strong>PEP 475</strong></a> for the rationale).</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.wait3">
<code>os.wait3(options)</code> </dt> <dd>
<p>Similar to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>, except no process id argument is given and a 3-element tuple containing the child’s process id, exit status indication, and resource usage information is returned. Refer to <a class="reference internal" href="resource#resource.getrusage" title="resource.getrusage"><code>resource.getrusage()</code></a> for details on resource usage information. The <em>options</em> argument is the same as that provided to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> and <a class="reference internal" href="#os.wait4" title="os.wait4"><code>wait4()</code></a>.</p> <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the exit status into an exitcode.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.wait4">
<code>os.wait4(pid, options)</code> </dt> <dd>
<p>Similar to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>, except a 3-element tuple, containing the child’s process id, exit status indication, and resource usage information is returned. Refer to <a class="reference internal" href="resource#resource.getrusage" title="resource.getrusage"><code>resource.getrusage()</code></a> for details on resource usage information. The arguments to <a class="reference internal" href="#os.wait4" title="os.wait4"><code>wait4()</code></a> are the same as those provided to <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>.</p> <p><a class="reference internal" href="#os.waitstatus_to_exitcode" title="os.waitstatus_to_exitcode"><code>waitstatus_to_exitcode()</code></a> can be used to convert the exit status into an exitcode.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.P_PID">
<code>os.P_PID</code> </dt> <dt class="sig sig-object py" id="os.P_PGID">
<code>os.P_PGID</code> </dt> <dt class="sig sig-object py" id="os.P_ALL">
<code>os.P_ALL</code> </dt> <dt class="sig sig-object py" id="os.P_PIDFD">
<code>os.P_PIDFD</code> </dt> <dd>
<p>These are the possible values for <em>idtype</em> in <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a>. They affect how <em>id</em> is interpreted:</p> <ul class="simple"> <li>
<code>P_PID</code> - wait for the child whose PID is <em>id</em>.</li> <li>
<code>P_PGID</code> - wait for any child whose progress group ID is <em>id</em>.</li> <li>
<code>P_ALL</code> - wait for any child; <em>id</em> is ignored.</li> <li>
<code>P_PIDFD</code> - wait for the child identified by the file descriptor <em>id</em> (a process file descriptor created with <a class="reference internal" href="#os.pidfd_open" title="os.pidfd_open"><code>pidfd_open()</code></a>).</li> </ul> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p><code>P_PIDFD</code> is only available on Linux >= 5.4.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9: </span>The <code>P_PIDFD</code> constant.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WCONTINUED">
<code>os.WCONTINUED</code> </dt> <dd>
<p>This <em>options</em> flag for <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code>wait3()</code></a>, <a class="reference internal" href="#os.wait4" title="os.wait4"><code>wait4()</code></a>, and <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a> causes child processes to be reported if they have been continued from a job control stop since they were last reported.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WEXITED">
<code>os.WEXITED</code> </dt> <dd>
<p>This <em>options</em> flag for <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a> causes child processes that have terminated to be reported.</p> <p>The other <code>wait*</code> functions always report children that have terminated, so this option is not available for them.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WSTOPPED">
<code>os.WSTOPPED</code> </dt> <dd>
<p>This <em>options</em> flag for <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a> causes child processes that have been stopped by the delivery of a signal to be reported.</p> <p>This option is not available for the other <code>wait*</code> functions.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WUNTRACED">
<code>os.WUNTRACED</code> </dt> <dd>
<p>This <em>options</em> flag for <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code>wait3()</code></a>, and <a class="reference internal" href="#os.wait4" title="os.wait4"><code>wait4()</code></a> causes child processes to also be reported if they have been stopped but their current state has not been reported since they were stopped.</p> <p>This option is not available for <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WNOHANG">
<code>os.WNOHANG</code> </dt> <dd>
<p>This <em>options</em> flag causes <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a>, <a class="reference internal" href="#os.wait3" title="os.wait3"><code>wait3()</code></a>, <a class="reference internal" href="#os.wait4" title="os.wait4"><code>wait4()</code></a>, and <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a> to return right away if no child process status is available immediately.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.WNOWAIT">
<code>os.WNOWAIT</code> </dt> <dd>
<p>This <em>options</em> flag causes <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a> to leave the child in a waitable state, so that a later <code>wait*()</code> call can be used to retrieve the child status information again.</p> <p>This option is not available for the other <code>wait*</code> functions.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.CLD_EXITED">
<code>os.CLD_EXITED</code> </dt> <dt class="sig sig-object py" id="os.CLD_KILLED">
<code>os.CLD_KILLED</code> </dt> <dt class="sig sig-object py" id="os.CLD_DUMPED">
<code>os.CLD_DUMPED</code> </dt> <dt class="sig sig-object py" id="os.CLD_TRAPPED">
<code>os.CLD_TRAPPED</code> </dt> <dt class="sig sig-object py" id="os.CLD_STOPPED">
<code>os.CLD_STOPPED</code> </dt> <dt class="sig sig-object py" id="os.CLD_CONTINUED">
<code>os.CLD_CONTINUED</code> </dt> <dd>
<p>These are the possible values for <code>si_code</code> in the result returned by <a class="reference internal" href="#os.waitid" title="os.waitid"><code>waitid()</code></a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Added <a class="reference internal" href="#os.CLD_KILLED" title="os.CLD_KILLED"><code>CLD_KILLED</code></a> and <a class="reference internal" href="#os.CLD_STOPPED" title="os.CLD_STOPPED"><code>CLD_STOPPED</code></a> values.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.waitstatus_to_exitcode">
<code>os.waitstatus_to_exitcode(status)</code> </dt> <dd>
<p>Convert a wait status to an exit code.</p> <p>On Unix:</p> <ul class="simple"> <li>If the process exited normally (if <code>WIFEXITED(status)</code> is true), return the process exit status (return <code>WEXITSTATUS(status)</code>): result greater than or equal to 0.</li> <li>If the process was terminated by a signal (if <code>WIFSIGNALED(status)</code> is true), return <code>-signum</code> where <em>signum</em> is the number of the signal that caused the process to terminate (return <code>-WTERMSIG(status)</code>): result less than 0.</li> <li>Otherwise, raise a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>.</li> </ul> <p>On Windows, return <em>status</em> shifted right by 8 bits.</p> <p>On Unix, if the process is being traced or if <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> was called with <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code>WUNTRACED</code></a> option, the caller must first check if <code>WIFSTOPPED(status)</code> is true. This function must not be called if <code>WIFSTOPPED(status)</code> is true.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p><a class="reference internal" href="#os.WIFEXITED" title="os.WIFEXITED"><code>WIFEXITED()</code></a>, <a class="reference internal" href="#os.WEXITSTATUS" title="os.WEXITSTATUS"><code>WEXITSTATUS()</code></a>, <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code>WIFSIGNALED()</code></a>, <a class="reference internal" href="#os.WTERMSIG" title="os.WTERMSIG"><code>WTERMSIG()</code></a>, <a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code>WIFSTOPPED()</code></a>, <a class="reference internal" href="#os.WSTOPSIG" title="os.WSTOPSIG"><code>WSTOPSIG()</code></a> functions.</p> </div> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, Windows, not Emscripten, not WASI.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.9.</span></p> </div> </dd>
</dl> <p>The following functions take a process status code as returned by <a class="reference internal" href="#os.system" title="os.system"><code>system()</code></a>, <a class="reference internal" href="#os.wait" title="os.wait"><code>wait()</code></a>, or <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> as a parameter. They may be used to determine the disposition of a process.</p> <dl class="py function"> <dt class="sig sig-object py" id="os.WCOREDUMP">
<code>os.WCOREDUMP(status, /)</code> </dt> <dd>
<p>Return <code>True</code> if a core dump was generated for the process, otherwise return <code>False</code>.</p> <p>This function should be employed only if <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code>WIFSIGNALED()</code></a> is true.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WIFCONTINUED">
<code>os.WIFCONTINUED(status)</code> </dt> <dd>
<p>Return <code>True</code> if a stopped child has been resumed by delivery of <a class="reference internal" href="signal#signal.SIGCONT" title="signal.SIGCONT"><code>SIGCONT</code></a> (if the process has been continued from a job control stop), otherwise return <code>False</code>.</p> <p>See <a class="reference internal" href="#os.WCONTINUED" title="os.WCONTINUED"><code>WCONTINUED</code></a> option.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WIFSTOPPED">
<code>os.WIFSTOPPED(status)</code> </dt> <dd>
<p>Return <code>True</code> if the process was stopped by delivery of a signal, otherwise return <code>False</code>.</p> <p><a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code>WIFSTOPPED()</code></a> only returns <code>True</code> if the <a class="reference internal" href="#os.waitpid" title="os.waitpid"><code>waitpid()</code></a> call was done using <a class="reference internal" href="#os.WUNTRACED" title="os.WUNTRACED"><code>WUNTRACED</code></a> option or when the process is being traced (see <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/ptrace(2)">ptrace(2)</a></em>).</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WIFSIGNALED">
<code>os.WIFSIGNALED(status)</code> </dt> <dd>
<p>Return <code>True</code> if the process was terminated by a signal, otherwise return <code>False</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WIFEXITED">
<code>os.WIFEXITED(status)</code> </dt> <dd>
<p>Return <code>True</code> if the process exited terminated normally, that is, by calling <code>exit()</code> or <code>_exit()</code>, or by returning from <code>main()</code>; otherwise return <code>False</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WEXITSTATUS">
<code>os.WEXITSTATUS(status)</code> </dt> <dd>
<p>Return the process exit status.</p> <p>This function should be employed only if <a class="reference internal" href="#os.WIFEXITED" title="os.WIFEXITED"><code>WIFEXITED()</code></a> is true.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WSTOPSIG">
<code>os.WSTOPSIG(status)</code> </dt> <dd>
<p>Return the signal which caused the process to stop.</p> <p>This function should be employed only if <a class="reference internal" href="#os.WIFSTOPPED" title="os.WIFSTOPPED"><code>WIFSTOPPED()</code></a> is true.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.WTERMSIG">
<code>os.WTERMSIG(status)</code> </dt> <dd>
<p>Return the number of the signal that caused the process to terminate.</p> <p>This function should be employed only if <a class="reference internal" href="#os.WIFSIGNALED" title="os.WIFSIGNALED"><code>WIFSIGNALED()</code></a> is true.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not Emscripten, not WASI.</p> </div> </dd>
</dl> </section> <section id="interface-to-the-scheduler"> <h2>Interface to the scheduler</h2> <p>These functions control how a process is allocated CPU time by the operating system. They are only available on some Unix platforms. For more detailed information, consult your Unix manpages.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <p>The following scheduling policies are exposed if they are supported by the operating system.</p> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_OTHER">
<code>os.SCHED_OTHER</code> </dt> <dd>
<p>The default scheduling policy.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_BATCH">
<code>os.SCHED_BATCH</code> </dt> <dd>
<p>Scheduling policy for CPU-intensive processes that tries to preserve interactivity on the rest of the computer.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_IDLE">
<code>os.SCHED_IDLE</code> </dt> <dd>
<p>Scheduling policy for extremely low priority background tasks.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_SPORADIC">
<code>os.SCHED_SPORADIC</code> </dt> <dd>
<p>Scheduling policy for sporadic server programs.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_FIFO">
<code>os.SCHED_FIFO</code> </dt> <dd>
<p>A First In First Out scheduling policy.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_RR">
<code>os.SCHED_RR</code> </dt> <dd>
<p>A round-robin scheduling policy.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.SCHED_RESET_ON_FORK">
<code>os.SCHED_RESET_ON_FORK</code> </dt> <dd>
<p>This flag can be OR’ed with any other scheduling policy. When a process with this flag set forks, its child’s scheduling policy and priority are reset to the default.</p> </dd>
</dl> <dl class="py class"> <dt class="sig sig-object py" id="os.sched_param">
<code>class os.sched_param(sched_priority)</code> </dt> <dd>
<p>This class represents tunable scheduling parameters used in <a class="reference internal" href="#os.sched_setparam" title="os.sched_setparam"><code>sched_setparam()</code></a>, <a class="reference internal" href="#os.sched_setscheduler" title="os.sched_setscheduler"><code>sched_setscheduler()</code></a>, and <a class="reference internal" href="#os.sched_getparam" title="os.sched_getparam"><code>sched_getparam()</code></a>. It is immutable.</p> <p>At the moment, there is only one possible parameter:</p> <dl class="py attribute"> <dt class="sig sig-object py" id="os.sched_param.sched_priority">
<code>sched_priority</code> </dt> <dd>
<p>The scheduling priority for a scheduling policy.</p> </dd>
</dl> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_get_priority_min">
<code>os.sched_get_priority_min(policy)</code> </dt> <dd>
<p>Get the minimum priority value for <em>policy</em>. <em>policy</em> is one of the scheduling policy constants above.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_get_priority_max">
<code>os.sched_get_priority_max(policy)</code> </dt> <dd>
<p>Get the maximum priority value for <em>policy</em>. <em>policy</em> is one of the scheduling policy constants above.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_setscheduler">
<code>os.sched_setscheduler(pid, policy, param, /)</code> </dt> <dd>
<p>Set the scheduling policy for the process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process. <em>policy</em> is one of the scheduling policy constants above. <em>param</em> is a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code>sched_param</code></a> instance.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_getscheduler">
<code>os.sched_getscheduler(pid, /)</code> </dt> <dd>
<p>Return the scheduling policy for the process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process. The result is one of the scheduling policy constants above.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_setparam">
<code>os.sched_setparam(pid, param, /)</code> </dt> <dd>
<p>Set the scheduling parameters for the process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process. <em>param</em> is a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code>sched_param</code></a> instance.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_getparam">
<code>os.sched_getparam(pid, /)</code> </dt> <dd>
<p>Return the scheduling parameters as a <a class="reference internal" href="#os.sched_param" title="os.sched_param"><code>sched_param</code></a> instance for the process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_rr_get_interval">
<code>os.sched_rr_get_interval(pid, /)</code> </dt> <dd>
<p>Return the round-robin quantum in seconds for the process with PID <em>pid</em>. A <em>pid</em> of 0 means the calling process.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_yield">
<code>os.sched_yield()</code> </dt> <dd>
<p>Voluntarily relinquish the CPU.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_setaffinity">
<code>os.sched_setaffinity(pid, mask, /)</code> </dt> <dd>
<p>Restrict the process with PID <em>pid</em> (or the current process if zero) to a set of CPUs. <em>mask</em> is an iterable of integers representing the set of CPUs to which the process should be restricted.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sched_getaffinity">
<code>os.sched_getaffinity(pid, /)</code> </dt> <dd>
<p>Return the set of CPUs the process with PID <em>pid</em> is restricted to.</p> <p>If <em>pid</em> is zero, return the set of CPUs the calling thread of the current process is restricted to.</p> </dd>
</dl> </section> <section id="miscellaneous-system-information"> <span id="os-path"></span><h2>Miscellaneous System Information</h2> <dl class="py function"> <dt class="sig sig-object py" id="os.confstr">
<code>os.confstr(name, /)</code> </dt> <dd>
<p>Return string-valued system configuration values. <em>name</em> specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given as the keys of the <code>confstr_names</code> dictionary. For configuration variables not included in that mapping, passing an integer for <em>name</em> is also accepted.</p> <p>If the configuration value specified by <em>name</em> isn’t defined, <code>None</code> is returned.</p> <p>If <em>name</em> is a string and is not known, <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised. If a specific value for <em>name</em> is not supported by the host system, even if it is included in <code>confstr_names</code>, an <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised with <a class="reference internal" href="errno#errno.EINVAL" title="errno.EINVAL"><code>errno.EINVAL</code></a> for the error number.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.confstr_names">
<code>os.confstr_names</code> </dt> <dd>
<p>Dictionary mapping names accepted by <a class="reference internal" href="#os.confstr" title="os.confstr"><code>confstr()</code></a> to the integer values defined for those names by the host operating system. This can be used to determine the set of names known to the system.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.cpu_count">
<code>os.cpu_count()</code> </dt> <dd>
<p>Return the number of logical CPUs in the system. Returns <code>None</code> if undetermined.</p> <p>This number is not equivalent to the number of logical CPUs the current process can use. <code>len(os.sched_getaffinity(0))</code> gets the number of logical CPUs the calling thread of the current process is restricted to</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.getloadavg">
<code>os.getloadavg()</code> </dt> <dd>
<p>Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes or raises <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> if the load average was unobtainable.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.sysconf">
<code>os.sysconf(name, /)</code> </dt> <dd>
<p>Return integer-valued system configuration values. If the configuration value specified by <em>name</em> isn’t defined, <code>-1</code> is returned. The comments regarding the <em>name</em> parameter for <a class="reference internal" href="#os.confstr" title="os.confstr"><code>confstr()</code></a> apply here as well; the dictionary that provides information on the known names is given by <code>sysconf_names</code>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.sysconf_names">
<code>os.sysconf_names</code> </dt> <dd>
<p>Dictionary mapping names accepted by <a class="reference internal" href="#os.sysconf" title="os.sysconf"><code>sysconf()</code></a> to the integer values defined for those names by the host operating system. This can be used to determine the set of names known to the system.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Add <code>'SC_MINSIGSTKSZ'</code> name.</p> </div> </dd>
</dl> <p>The following data values are used to support path manipulation operations. These are defined for all platforms.</p> <p>Higher-level operations on pathnames are defined in the <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a> module.</p> <span class="target" id="index-45"></span><dl class="py data"> <dt class="sig sig-object py" id="os.curdir">
<code>os.curdir</code> </dt> <dd>
<p>The constant string used by the operating system to refer to the current directory. This is <code>'.'</code> for Windows and POSIX. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <span class="target" id="index-46"></span><dl class="py data"> <dt class="sig sig-object py" id="os.pardir">
<code>os.pardir</code> </dt> <dd>
<p>The constant string used by the operating system to refer to the parent directory. This is <code>'..'</code> for Windows and POSIX. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <span class="target" id="index-47"></span><span class="target" id="index-48"></span><dl class="py data"> <dt class="sig sig-object py" id="os.sep">
<code>os.sep</code> </dt> <dd>
<p>The character used by the operating system to separate pathname components. This is <code>'/'</code> for POSIX and <code>'\\'</code> for Windows. Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use <a class="reference internal" href="os.path#os.path.split" title="os.path.split"><code>os.path.split()</code></a> and <a class="reference internal" href="os.path#os.path.join" title="os.path.join"><code>os.path.join()</code></a> — but it is occasionally useful. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <span class="target" id="index-49"></span><dl class="py data"> <dt class="sig sig-object py" id="os.altsep">
<code>os.altsep</code> </dt> <dd>
<p>An alternative character used by the operating system to separate pathname components, or <code>None</code> if only one separator character exists. This is set to <code>'/'</code> on Windows systems where <code>sep</code> is a backslash. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <span class="target" id="index-50"></span><dl class="py data"> <dt class="sig sig-object py" id="os.extsep">
<code>os.extsep</code> </dt> <dd>
<p>The character which separates the base filename from the extension; for example, the <code>'.'</code> in <code>os.py</code>. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <span class="target" id="index-51"></span><dl class="py data"> <dt class="sig sig-object py" id="os.pathsep">
<code>os.pathsep</code> </dt> <dd>
<p>The character conventionally used by the operating system to separate search path components (as in <span class="target" id="index-52"></span><code>PATH</code>), such as <code>':'</code> for POSIX or <code>';'</code> for Windows. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.defpath">
<code>os.defpath</code> </dt> <dd>
<p>The default search path used by <a class="reference internal" href="#os.execl" title="os.execl"><code>exec*p*</code></a> and <a class="reference internal" href="#os.spawnl" title="os.spawnl"><code>spawn*p*</code></a> if the environment doesn’t have a <code>'PATH'</code> key. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.linesep">
<code>os.linesep</code> </dt> <dd>
<p>The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as <code>'\n'</code> for POSIX, or multiple characters, for example, <code>'\r\n'</code> for Windows. Do not use <em>os.linesep</em> as a line terminator when writing files opened in text mode (the default); use a single <code>'\n'</code> instead, on all platforms.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.devnull">
<code>os.devnull</code> </dt> <dd>
<p>The file path of the null device. For example: <code>'/dev/null'</code> for POSIX, <code>'nul'</code> for Windows. Also available via <a class="reference internal" href="os.path#module-os.path" title="os.path: Operations on pathnames."><code>os.path</code></a>.</p> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.RTLD_LAZY">
<code>os.RTLD_LAZY</code> </dt> <dt class="sig sig-object py" id="os.RTLD_NOW">
<code>os.RTLD_NOW</code> </dt> <dt class="sig sig-object py" id="os.RTLD_GLOBAL">
<code>os.RTLD_GLOBAL</code> </dt> <dt class="sig sig-object py" id="os.RTLD_LOCAL">
<code>os.RTLD_LOCAL</code> </dt> <dt class="sig sig-object py" id="os.RTLD_NODELETE">
<code>os.RTLD_NODELETE</code> </dt> <dt class="sig sig-object py" id="os.RTLD_NOLOAD">
<code>os.RTLD_NOLOAD</code> </dt> <dt class="sig sig-object py" id="os.RTLD_DEEPBIND">
<code>os.RTLD_DEEPBIND</code> </dt> <dd>
<p>Flags for use with the <a class="reference internal" href="sys#sys.setdlopenflags" title="sys.setdlopenflags"><code>setdlopenflags()</code></a> and <a class="reference internal" href="sys#sys.getdlopenflags" title="sys.getdlopenflags"><code>getdlopenflags()</code></a> functions. See the Unix manual page <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/dlopen(3)">dlopen(3)</a></em> for what the different flags mean.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd>
</dl> </section> <section id="random-numbers"> <h2>Random numbers</h2> <dl class="py function"> <dt class="sig sig-object py" id="os.getrandom">
<code>os.getrandom(size, flags=0)</code> </dt> <dd>
<p>Get up to <em>size</em> random bytes. The function can return less bytes than requested.</p> <p>These bytes can be used to seed user-space random number generators or for cryptographic purposes.</p> <p><code>getrandom()</code> relies on entropy gathered from device drivers and other sources of environmental noise. Unnecessarily reading large quantities of data will have a negative impact on other users of the <code>/dev/random</code> and <code>/dev/urandom</code> devices.</p> <p>The flags argument is a bit mask that can contain zero or more of the following values ORed together: <a class="reference internal" href="#os.GRND_RANDOM" title="os.GRND_RANDOM"><code>os.GRND_RANDOM</code></a> and <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code>GRND_NONBLOCK</code></a>.</p> <p>See also the <a class="reference external" href="https://man7.org/linux/man-pages/man2/getrandom.2.html">Linux getrandom() manual page</a>.</p> <div class="availability docutils container"> <p><a class="reference internal" href="https://docs.python.org/3.12/library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Linux >= 3.17.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="os.urandom">
<code>os.urandom(size, /)</code> </dt> <dd>
<p>Return a bytestring of <em>size</em> random bytes suitable for cryptographic use.</p> <p>This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation.</p> <p>On Linux, if the <code>getrandom()</code> syscall is available, it is used in blocking mode: block until the system urandom entropy pool is initialized (128 bits of entropy are collected by the kernel). See the <span class="target" id="index-53"></span><a class="pep reference external" href="https://peps.python.org/pep-0524/"><strong>PEP 524</strong></a> for the rationale. On Linux, the <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code>getrandom()</code></a> function can be used to get random bytes in non-blocking mode (using the <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code>GRND_NONBLOCK</code></a> flag) or to poll until the system urandom entropy pool is initialized.</p> <p>On a Unix-like system, random bytes are read from the <code>/dev/urandom</code> device. If the <code>/dev/urandom</code> device is not available or not readable, the <a class="reference internal" href="exceptions#NotImplementedError" title="NotImplementedError"><code>NotImplementedError</code></a> exception is raised.</p> <p>On Windows, it will use <code>BCryptGenRandom()</code>.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference internal" href="secrets#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code>secrets</code></a> module provides higher level functions. For an easy-to-use interface to the random number generator provided by your platform, please see <a class="reference internal" href="random#random.SystemRandom" title="random.SystemRandom"><code>random.SystemRandom</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6.0: </span>On Linux, <code>getrandom()</code> is now used in blocking mode to increase the security.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5.2: </span>On Linux, if the <code>getrandom()</code> syscall blocks (the urandom entropy pool is not initialized yet), fall back on reading <code>/dev/urandom</code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>On Linux 3.17 and newer, the <code>getrandom()</code> syscall is now used when available. On OpenBSD 5.6 and newer, the C <code>getentropy()</code> function is now used. These functions avoid the usage of an internal file descriptor.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>On Windows, <code>BCryptGenRandom()</code> is used instead of <code>CryptGenRandom()</code> which is deprecated.</p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.GRND_NONBLOCK">
<code>os.GRND_NONBLOCK</code> </dt> <dd>
<p>By default, when reading from <code>/dev/random</code>, <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code>getrandom()</code></a> blocks if no random bytes are available, and when reading from <code>/dev/urandom</code>, it blocks if the entropy pool has not yet been initialized.</p> <p>If the <a class="reference internal" href="#os.GRND_NONBLOCK" title="os.GRND_NONBLOCK"><code>GRND_NONBLOCK</code></a> flag is set, then <a class="reference internal" href="#os.getrandom" title="os.getrandom"><code>getrandom()</code></a> does not block in these cases, but instead immediately raises <a class="reference internal" href="exceptions#BlockingIOError" title="BlockingIOError"><code>BlockingIOError</code></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> <dl class="py data"> <dt class="sig sig-object py" id="os.GRND_RANDOM">
<code>os.GRND_RANDOM</code> </dt> <dd>
<p>If this bit is set, then random bytes are drawn from the <code>/dev/random</code> pool instead of the <code>/dev/urandom</code> pool.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd>
</dl> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/os.html" class="_attribution-link">https://docs.python.org/3.12/library/os.html</a>
</p>
</div>
|