I’m using Python 2.6. Sometimes there become several instances of a certain process open, and that process causes some problems in itself. I want to be able to programatically detect that there are multiple instances of that process and to kill them.
For example, maybe in some cases there are 50 instances of make.exe open. I want to be able to tell that there are 20 instances open, and to kill them all. How is this accomplished?
asked Jun 8, 2011 at 12:42
cupofcupof
6131 gold badge5 silver badges6 bronze badges
1
I would think you could just use taskkill and the Python os.system()
import os
os.system("taskkill /im make.exe")
Note: I would just note you might have to fully qualify the taskkill path. I am using a Linux box so I can’t test…
djvg
10.3k4 gold badges64 silver badges94 bronze badges
answered Jun 8, 2011 at 12:50
NixNix
56.2k29 gold badges148 silver badges198 bronze badges
6
Yes,You can do it
import os
os.system("taskkill /f /im Your_Process_Name.exe")
- /f : Specifies that process(es) be forcefully terminated.
- /im (ImageName ): Specifies the image name of the process to be
terminated. - For more info regarding TaskKill
answered Jan 20, 2017 at 2:37
0
You can use the TerminateProcess
of the win32 api to kill a process. See the following example : http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/
You need to give it a process handle. If the process is started from your code, the process handle is returned by the CreateProcess or popen.
If the process was started by something else, you need to get this handle you can use EnumProcess or WMI to retrieve it.
answered Jun 8, 2011 at 13:21
lucluc
41.3k23 gold badges125 silver badges171 bronze badges
1
How about this, I tested it with ActiveState Python 2.7:
import sys, traceback, os
def pkill (process_name):
try:
killed = os.system('tskill ' + process_name)
except Exception, e:
killed = 0
return killed
call it with:
pkill("program_name")
answered Jul 18, 2014 at 19:26
I think the code is like this will work:
import os
def terminate(ProcessName):
os.system('taskkill /IM "' + ProcessName + '" /F')
terminate('chrome.exe')
answered Jan 12, 2021 at 10:10
0
You can kill a process via its process identifier, pid, via the os.kill() function.
In this tutorial you will discover how to kill a process via its pid.
Let’s get started.
Need To Kill a Process by PID
A process is a running instance of a computer program.
Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create new child processes in our program in order to execute code concurrently.
Python provides the ability to create and manage new processes via the multiprocessing.Process class.
You can learn more about multiprocessing in the tutorial:
- Multiprocessing in Python: The Complete Guide
In multiprocessing, we may need to kill a process by its process identifier or PID.
This may be for many reasons, such as:
- The task executed by the process is no longer required.
- The process has had an error or is out of control.
- The main program is closing down due to a user request.
How can we kill a process via its pid in Python?
How To Kill a Process via its PID
You can kill a process via its pid with the os.kill() function.
The os.kill function takes two arguments, the process identifier (pid) to kill, and the signal to send to kill the process.
The signal is a constant from the signal module, such as signal.SIGINT or signal.SIGKILL.
For example:
... # kill a process via pid os.kill(12345, signal.SIGKILL) |
We need to know the pid for the process that is to be killed.
This can be retrieved from the multiprocessing.Process instance for the process via the pid attribute.
For example:
... # get the pid pid = process.pid |
The multiprocessing.Process instance may be managed by the parent process when the child process is created, or accessed via a module function such as multiprocessing.active_children() or multiprocessing.parent_process().
You can learn more about getting the process pid in the tutorial:
- How to Get the Process PID in Python
The SIGINT or signal interrupt can be used to terminate the target process, which is equivalent to the user pressing CONTROL-C on the process. Alternately, the SIGKILL or signal kill process can be used to terminate the process forcefully.
The difference between SIGINT and SIGKILL is that it is possible for a process to detect and handle a SIGINT, whereas a SIGKILL cannot be handled.
Now that we know how to kill a process via pid, let’s look at some worked examples.
Confused by the multiprocessing module API?
Download my FREE PDF cheat sheet
Kill Current Process via PID
It is possible to kill the current process via pid.
This can be achieved by first getting the pid for the current process, then calling os.kill() with the pid and the signal to kill the process, such as SIGKILL.
First, we can get the pid for the current process using the os.getpid(), and report the result.
... # get the pid of the current process pid = getpid() # report a message print(f‘Running with pid: {pid}’) |
Next, we can kill the process using the signal.SIGKILL signal.
... # attempt to kill the current process kill(pid, SIGKILL) |
Tying this together, the complete example is listed below.
# SuperFastPython.com # example of killing the current process via pid from os import kill from os import getpid from signal import SIGKILL # get the pid of the current process pid = getpid() # report a message print(f‘Running with pid: {pid}’) # attempt to kill the current process kill(pid, SIGKILL) # report a message print(‘Skill running’) |
Running the example first gets the pid for the current process, then reports the value.
Note, the pid for the process will differ each time the program is run.
Next, the current process is killed via its pid, e.g, the process kills itself.
Next, let’s look at how we might kill a child process.
Free Python Multiprocessing Course
Download my multiprocessing API cheat sheet and as a bonus you will get FREE access to my 7-day email course.
Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.
Learn more
Kill Child Process via PID
We can kill a child process via its pid.
In this example, we will first start a child process that will block for an extended period to keep it occupied. The parent process will then access the pid for the child process and terminate it via the SIGKILL.
First, we can define a function used to run in the new child process.
The task() function below implements this, reporting a message and sleeping for ten seconds.
# function executed in a child process def task(): # report a message print(‘Child is running…’, flush=True) # block for a while sleep(10) |
Next, the main process will create and configure a new multiprocessing.Process instance to run the function in a new child process. Then the child process is started.
... # start a child process child = Process(target=task) child.start() |
The parent process then blocks a moment to allow the child process to start, then reports the pid for the child process via the multiprocessing.Process.pid attribute.
... # wait a moment sleep(1) # report a message print(f‘Child is running with pid: {child.pid}’) |
Next, the child process is killed via its process id and the SIGKILL signal.
... # attempt to kill the child process kill(child.pid, SIGKILL) |
The parent process then blocks until the child process is completely stopped and then reports the status of the child process.
... # wait for child to terminate child.join() # report status of the child print(f‘Child process: {child}’) |
Tying this together, the complete example is listed below.
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 |
# SuperFastPython.com # example of killing a child process by pid from time import sleep from multiprocessing import Process from os import kill from os import getpid from signal import SIGKILL # function executed in a child process def task(): # report a message print(‘Child is running…’, flush=True) # block for a while sleep(10) # protect the entry point if __name__ == ‘__main__’: # start a child process child = Process(target=task) child.start() # wait a moment sleep(1) # report a message print(f‘Child is running with pid: {child.pid}’) # attempt to kill the child process kill(child.pid, SIGKILL) # wait for child to terminate child.join() # report status of the child print(f‘Child process: {child}’) |
Running the example first configures and starts the child process.
The child process starts, reports a message, then blocks for ten seconds.
The parent process blocks for one second, to allow the child process to start up completely.
The parent process then reports the pid of the child process and kills it via the pid.
The parent process waits for the child process to shutdown completely, then reports the status of the child process.
We can see that the child process is not marked as “stopped” and that its exit code was the negative value of the signal used to terminate it, e.g. -SIGKILL.
Child is running… Child is running with pid: 21398 Child process: <Process name=’Process-1′ pid=21398 parent=21396 stopped exitcode=-SIGKILL> |
Next, let’s look at how we might kill the parent process via pid.
Kill Parent Process via PID
We can kill the parent process via pid.
In this example we can create a child process that will get the pid for the parent process, then kill the parent process via its pid.
First, we can define a function to run in a child process.
# function executed in a child process def task(): # … |
First, the child process will block for a second.
... # wait a moment sleep(1) |
Next, it will get a multiprocessing.Process instance for the parent process via the multiprocessing.parent_process() module function.
... # get the parent process parent = parent_process() |
Next, the pid of the parent process is reported.
... # report a message print(f‘Parent is running with pid: {parent.pid}’, flush=True) |
The parent process can then be killed via its pid.
... # attempt to kill the parent process kill(parent.pid, SIGKILL) |
The child process then blocks until the parent process has stopped completely, then reports the details of the parent process.
... # wait for parent to terminate parent.join() # report status of the parent print(f‘Parent process: {parent}’, flush=True) |
Tying this together, the complete task() function is listed below.
# function executed in a child process def task(): # wait a moment sleep(1) # get the parent process parent = parent_process() # report a message print(f‘Parent is running with pid: {parent.pid}’, flush=True) # attempt to kill the parent process kill(parent.pid, SIGKILL) # wait for parent to terminate parent.join() # report status of the parent print(f‘Parent process: {parent}’, flush=True) |
Next, the main process, the child process is configured and started.
... # start a child process child = Process(target=task) child.start() |
The parent process then blocks for ten seconds, waiting to be killed.
... # wait for a while sleep(10) # report a message print(‘Parent is still running’) |
Tying this together, the complete example is listed below.
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 |
# SuperFastPython.com # example of killing a parent process by pid from time import sleep from multiprocessing import parent_process from multiprocessing import Process from os import kill from signal import SIGKILL # function executed in a child process def task(): # wait a moment sleep(1) # get the parent process parent = parent_process() # report a message print(f‘Parent is running with pid: {parent.pid}’, flush=True) # attempt to kill the parent process kill(parent.pid, SIGKILL) # wait for parent to terminate parent.join() # report status of the parent print(f‘Parent process: {parent}’, flush=True) # protect the entry point if __name__ == ‘__main__’: # start a child process child = Process(target=task) child.start() # wait for a while sleep(10) # report a message print(‘Parent is still running’) |
Running the example first configures and starts a child process.
The parent process then blocks for ten seconds.
The child process blocks for a second, then gets the parent process and reports its pid.
The child process then kills the parent process via its pid.
Next, the child process blocks until the parent process terminates completely, then reports the details of the parent process.
We can see the details of the parent process, which is the main process.
Parent is running with pid: 21421 Parent process: <_ParentProcess name=’MainProcess’ parent=None unknown> |
Further Reading
This section provides additional resources that you may find helpful.
Books
- Python Multiprocessing Jump-Start, Jason Brownlee, 2022 (my book!).
- Multiprocessing API Interview Questions
- Multiprocessing Module API Cheat Sheet
I would also recommend specific chapters in the books:
- Effective Python, Brett Slatkin, 2019.
- See: Chapter 7: Concurrency and Parallelism
- High Performance Python, Ian Ozsvald and Micha Gorelick, 2020.
- See: Chapter 9: The multiprocessing Module
- Python in a Nutshell, Alex Martelli, et al., 2017.
- See: Chapter: 14: Threads and Processes
Guides
- Python Multiprocessing: The Complete Guide
APIs
- multiprocessing — Process-based parallelism
- PEP 371 — Addition of the multiprocessing package
Takeaways
You now know how to kill a process via its pid.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Gene Gallin on Unsplash
Послать сигнал процессу или группе процессов.
Синтаксис:
import os os.kill(pid, sig) os.killpg(pgid, sig)
Параметры:
pid
— сигнал,sig
—int
процесс,pgid
—int
группа процессов,
Возвращаемое значение:
- None
Вызывает событие аудита os.killpg с аргументами pgid, sig.
Описание:
Функция kill()
модуля os
посылает сигнал sig
процессу pid
. Константы для конкретных сигналов, доступных на хост-платформе, определяются в модуле signal
.
Функция os.kill()
событие аудита os.kill
с аргументами pid
, sig
.
Функция killpg()
посылает сигнал sig
в группу процессов pgid
. Константы для конкретных сигналов, доступных на хост-платформе, определяются в модуле signal
.
Функция os.killpg()
событие аудита os.killpg
с аргументами pgid
, sig
.
: сигналы signal.CTRL_C_EVENT
и signal.CTRL_BREAK_EVENT
являются специальными сигналами, которые могут отправляться только в консольные процессы, которые совместно используют общее окно консоли, например некоторые подпроцессы. Любое другое значение для sig
приведет к безоговорочному завершению процесса API-интерфейсом TerminateProcess
, а для кода выхода будет установлено значение sig
. Версия os.kill()
для Windows дополнительно использует дескрипторы процессов для уничтожения.
Примеры использования:
Родитель может отправлять сигналы дочернему процессу, используя функцию os.kill()
используя модуль signal
. В этом примере используется короткая пауза, чтобы дать дочернему процессу время для настройки обработчика сигнала. Реальное приложение, не будет нуждаться в вызове time.sleep()
. В дочернем процессе установим обработчик сигнала и сделаем небольшую паузу, чтобы предоставить родительскому процессу достаточно времени для отправки сигнала.
import os import signal import time def signal_usr1(signum, frame): "Обратный вызов вызывается при получении сигнала" pid = os.getpid() print(f'Получен USR1 в процессе {pid}') print('Forking...') child_pid = os.fork() if child_pid: print('PARENT: Пауза перед отправкой сигнала ...') time.sleep(1) print(f'PARENT: передача сигналов {child_pid}') os.kill(child_pid, signal.SIGUSR1) else: print('CHILD: Настройка обработчика сигнала') signal.signal(signal.SIGUSR1, signal_usr1) print('CHILD: Пауза в ожидании сигнала') time.sleep(5) # $ python3 test_kill.py # Forking... # PARENT: Пауза перед отправкой сигнала ... # CHILD: Настройка обработчика сигнала # CHILD: Пауза в ожидании сигнала # PARENT: передача сигналов 21168 # Получен USR1 в процессе 21168
Source code: Lib/os.py
This module provides a portable way of using operating system dependent
functionality. If you just want to read or write a file see open()
, if
you want to manipulate paths, see the os.path
module, and if you want to
read all the lines in all the files on the command line see the fileinput
module. For creating temporary files and directories see the tempfile
module, and for high-level file and directory handling see the shutil
module.
Notes on the availability of these functions:
- 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 functionos.stat(path)
returns stat
information about path in the same format (which happens to have originated
with the POSIX interface). - Extensions peculiar to a particular operating system are also available
through theos
module, but using them is of course a threat to
portability. - 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. - An “Availability: Unix” note means that this function is commonly found on
Unix systems. It does not make any claims about its existence on a specific
operating system. - If not separately noted, all functions that claim “Availability: Unix” are
supported on Mac OS X, which builds on a Unix core.
Note
All functions in this module raise OSError
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.
-
exception
os.
error
¶ -
An alias for the built-in
OSError
exception.
-
os.
name
¶ -
The name of the operating system dependent module imported. The following
names have currently been registered:'posix'
,'nt'
,
'java'
.See also
sys.platform
has a finer granularity.os.uname()
gives
system-dependent version information.The
platform
module provides detailed checks for the
system’s identity.
16.1.1. File Names, Command Line Arguments, and Environment Variables¶
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 file system encoding to perform this conversion (see
sys.getfilesystemencoding()
).
Changed in version 3.1: On some systems, conversion using the file system encoding may fail. In this
case, Python uses the surrogateescape encoding error handler, which means that undecodable bytes are replaced by a
Unicode character U+DCxx on decoding, and these are again translated to the
original byte on encoding.
The file system encoding must guarantee to successfully decode all bytes
below 128. If the file system encoding fails to provide this guarantee, API
functions may raise UnicodeErrors.
16.1.2. Process Parameters¶
These functions and data items provide information and operate on the current
process and user.
-
os.
ctermid
()¶ -
Return the filename corresponding to the controlling terminal of the process.
Availability: Unix.
-
os.
environ
¶ -
A mapping object representing the string environment. For example,
environ['HOME']
is the pathname of your home directory (on some platforms),
and is equivalent togetenv("HOME")
in C.This mapping is captured the first time the
os
module is imported,
typically during Python startup as part of processingsite.py
. Changes
to the environment made after this time are not reflected inos.environ
,
except for changes made by modifyingos.environ
directly.If the platform supports the
putenv()
function, this mapping may be used
to modify the environment as well as query the environment.putenv()
will
be called automatically when the mapping is modified.On Unix, keys and values use
sys.getfilesystemencoding()
and
'surrogateescape'
error handler. Useenvironb
if you would like
to use a different encoding.Note
Calling
putenv()
directly does not changeos.environ
, so it’s better
to modifyos.environ
.Note
On some platforms, including FreeBSD and Mac OS X, setting
environ
may
cause memory leaks. Refer to the system documentation for
putenv()
.If
putenv()
is not provided, a modified copy of this mapping may be
passed to the appropriate process-creation functions to cause child processes
to use a modified environment.If the platform supports the
unsetenv()
function, you can delete items in
this mapping to unset environment variables.unsetenv()
will be called
automatically when an item is deleted fromos.environ
, and when
one of thepop()
orclear()
methods is called.
-
os.
environb
¶ -
Bytes version of
environ
: a mapping object representing the
environment as byte strings.environ
andenvironb
are
synchronized (modifyenvironb
updatesenviron
, and vice
versa).environb
is only available ifsupports_bytes_environ
is
True.New in version 3.2.
-
os.
chdir
(path) -
os.
fchdir
(fd) -
os.
getcwd
() -
These functions are described in Files and Directories.
-
os.
fsencode
(filename)¶ -
Encode path-like filename to the filesystem
encoding with'surrogateescape'
error handler, or'strict'
on
Windows; returnbytes
unchanged.fsdecode()
is the reverse function.New in version 3.2.
Changed in version 3.6: Support added to accept objects implementing the
os.PathLike
interface.
-
os.
fsdecode
(filename)¶ -
Decode the path-like filename from the
filesystem encoding with'surrogateescape'
error handler, or'strict'
on Windows; returnstr
unchanged.fsencode()
is the reverse function.New in version 3.2.
Changed in version 3.6: Support added to accept objects implementing the
os.PathLike
interface.
-
os.
fspath
(path)¶ -
Return the file system representation of the path.
If
str
orbytes
is passed in, it is returned unchanged.
Otherwise__fspath__()
is called and its value is
returned as long as it is astr
orbytes
object.
In all other cases,TypeError
is raised.New in version 3.6.
-
class
os.
PathLike
¶ -
An abstract base class for objects representing a file system path,
e.g.pathlib.PurePath
.New in version 3.6.
-
abstractmethod
__fspath__
()¶ -
Return the file system path representation of the object.
The method should only return a
str
orbytes
object,
with the preference being forstr
.
-
abstractmethod
-
os.
getenv
(key, default=None)¶ -
Return the value of the environment variable key if it exists, or
default if it doesn’t. key, default and the result are str.On Unix, keys and values are decoded with
sys.getfilesystemencoding()
and'surrogateescape'
error handler. Useos.getenvb()
if you
would like to use a different encoding.Availability: most flavors of Unix, Windows.
-
os.
getenvb
(key, default=None)¶ -
Return the value of the environment variable key if it exists, or
default if it doesn’t. key, default and the result are bytes.getenvb()
is only available ifsupports_bytes_environ
is True.Availability: most flavors of Unix.
New in version 3.2.
-
os.
get_exec_path
(env=None)¶ -
Returns the list of directories that will be searched for a named
executable, similar to a shell, when launching a process.
env, when specified, should be an environment variable dictionary
to lookup the PATH in.
By default, when env isNone
,environ
is used.New in version 3.2.
-
os.
getegid
()¶ -
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.Availability: Unix.
-
os.
geteuid
()¶ -
Return the current process’s effective user id.
Availability: Unix.
-
os.
getgid
()¶ -
Return the real group id of the current process.
Availability: Unix.
-
os.
getgrouplist
(user, group)¶ -
Return list of group ids that user belongs to. If group is not in the
list, it is included; typically, group is specified as the group ID
field from the password record for user.Availability: Unix.
New in version 3.3.
-
os.
getgroups
()¶ -
Return list of supplemental group ids associated with the current process.
Availability: Unix.
Note
On Mac OS X,
getgroups()
behavior differs somewhat from
other Unix platforms. If the Python interpreter was built with a
deployment target of10.5
or earlier,getgroups()
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 tosetgroups()
if suitably privileged.
If built with a deployment target greater than10.5
,
getgroups()
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 tosetgroups()
, and its length is not limited to 16. The
deployment target value,MACOSX_DEPLOYMENT_TARGET
, can be
obtained withsysconfig.get_config_var()
.
-
os.
getlogin
()¶ -
Return the name of the user logged in on the controlling terminal of the
process. For most purposes, it is more useful to use the environment
variablesLOGNAME
orUSERNAME
to find out who the user
is, orpwd.getpwuid(os.getuid())[0]
to get the login name of the current
real user id.Availability: Unix, Windows.
-
os.
getpgid
(pid)¶ -
Return the process group id of the process with process id pid. If pid is 0,
the process group id of the current process is returned.Availability: Unix.
-
os.
getpgrp
()¶ -
Return the id of the current process group.
Availability: Unix.
-
os.
getpid
()¶ -
Return the current process id.
-
os.
getppid
()¶ -
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.Availability: Unix, Windows.
Changed in version 3.2: Added support for Windows.
-
os.
getpriority
(which, who)¶ -
Get program scheduling priority. The value which is one of
PRIO_PROCESS
,PRIO_PGRP
, orPRIO_USER
, and who
is interpreted relative to which (a process identifier for
PRIO_PROCESS
, process group identifier forPRIO_PGRP
, and a
user ID forPRIO_USER
). A zero value for who denotes
(respectively) the calling process, the process group of the calling process,
or the real user ID of the calling process.Availability: Unix.
New in version 3.3.
-
os.
PRIO_PROCESS
¶ -
os.
PRIO_PGRP
¶ -
os.
PRIO_USER
¶ -
Parameters for the
getpriority()
andsetpriority()
functions.Availability: Unix.
New in version 3.3.
-
os.
getresuid
()¶ -
Return a tuple (ruid, euid, suid) denoting the current process’s
real, effective, and saved user ids.Availability: Unix.
New in version 3.2.
-
os.
getresgid
()¶ -
Return a tuple (rgid, egid, sgid) denoting the current process’s
real, effective, and saved group ids.Availability: Unix.
New in version 3.2.
-
os.
getuid
()¶ -
Return the current process’s real user id.
Availability: Unix.
-
os.
initgroups
(username, gid)¶ -
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.Availability: Unix.
New in version 3.2.
-
os.
putenv
(key, value)¶ -
Set the environment variable named key to the string value. Such
changes to the environment affect subprocesses started withos.system()
,
popen()
orfork()
andexecv()
.Availability: most flavors of Unix, Windows.
Note
On some platforms, including FreeBSD and Mac OS X, setting
environ
may
cause memory leaks. Refer to the system documentation for putenv.When
putenv()
is supported, assignments to items inos.environ
are
automatically translated into corresponding calls toputenv()
; however,
calls toputenv()
don’t updateos.environ
, so it is actually
preferable to assign to items ofos.environ
.
-
os.
setegid
(egid)¶ -
Set the current process’s effective group id.
Availability: Unix.
-
os.
seteuid
(euid)¶ -
Set the current process’s effective user id.
Availability: Unix.
-
os.
setgid
(gid)¶ -
Set the current process’ group id.
Availability: Unix.
-
os.
setgroups
(groups)¶ -
Set the list of supplemental group ids associated with the current process to
groups. groups must be a sequence, and each element must be an integer
identifying a group. This operation is typically available only to the superuser.Availability: Unix.
Note
On Mac OS X, the length of groups may not exceed the
system-defined maximum number of effective group ids, typically 16.
See the documentation forgetgroups()
for cases where it may not
return the same group list set by calling setgroups().
-
os.
setpgrp
()¶ -
Call the system call
setpgrp()
orsetpgrp(0, 0)
depending on
which version is implemented (if any). See the Unix manual for the semantics.Availability: Unix.
-
os.
setpgid
(pid, pgrp)¶ -
Call the system call
setpgid()
to set the process group id of the
process with id pid to the process group with id pgrp. See the Unix manual
for the semantics.Availability: Unix.
-
os.
setpriority
(which, who, priority)¶ -
Set program scheduling priority. The value which is one of
PRIO_PROCESS
,PRIO_PGRP
, orPRIO_USER
, and who
is interpreted relative to which (a process identifier for
PRIO_PROCESS
, process group identifier forPRIO_PGRP
, and a
user ID forPRIO_USER
). A zero value for who denotes
(respectively) the calling process, the process group of the calling process,
or the real user ID of the calling process.
priority is a value in the range -20 to 19. The default priority is 0;
lower priorities cause more favorable scheduling.Availability: Unix
New in version 3.3.
-
os.
setregid
(rgid, egid)¶ -
Set the current process’s real and effective group ids.
Availability: Unix.
-
os.
setresgid
(rgid, egid, sgid)¶ -
Set the current process’s real, effective, and saved group ids.
Availability: Unix.
New in version 3.2.
-
os.
setresuid
(ruid, euid, suid)¶ -
Set the current process’s real, effective, and saved user ids.
Availability: Unix.
New in version 3.2.
-
os.
setreuid
(ruid, euid)¶ -
Set the current process’s real and effective user ids.
Availability: Unix.
-
os.
getsid
(pid)¶ -
Call the system call
getsid()
. See the Unix manual for the semantics.Availability: Unix.
-
os.
setsid
()¶ -
Call the system call
setsid()
. See the Unix manual for the semantics.Availability: Unix.
-
os.
setuid
(uid)¶ -
Set the current process’s user id.
Availability: Unix.
-
os.
strerror
(code)¶ -
Return the error message corresponding to the error code in code.
On platforms wherestrerror()
returnsNULL
when given an unknown
error number,ValueError
is raised.
-
os.
supports_bytes_environ
¶ -
True
if the native OS type of the environment is bytes (eg.False
on
Windows).New in version 3.2.
-
os.
umask
(mask)¶ -
Set the current numeric umask and return the previous umask.
-
os.
uname
()¶ -
Returns information identifying the current operating system.
The return value is an object with five attributes:sysname
— operating system namenodename
— name of machine on network (implementation-defined)release
— operating system releaseversion
— operating system versionmachine
— hardware identifier
For backwards compatibility, this object is also iterable, behaving
like a five-tuple containingsysname
,nodename
,
release
,version
, andmachine
in that order.Some systems truncate
nodename
to 8 characters or to the
leading component; a better way to get the hostname is
socket.gethostname()
or even
socket.gethostbyaddr(socket.gethostname())
.Availability: recent flavors of Unix.
Changed in version 3.3: Return type changed from a tuple to a tuple-like object
with named attributes.
-
os.
unsetenv
(key)¶ -
Unset (delete) the environment variable named key. Such changes to the
environment affect subprocesses started withos.system()
,popen()
or
fork()
andexecv()
.When
unsetenv()
is supported, deletion of items inos.environ
is
automatically translated into a corresponding call tounsetenv()
; however,
calls tounsetenv()
don’t updateos.environ
, so it is actually
preferable to delete items ofos.environ
.Availability: most flavors of Unix, Windows.
16.1.3. File Object Creation¶
This function creates new file objects. (See also
open()
for opening file descriptors.)
-
os.
fdopen
(fd, *args, **kwargs)¶ -
Return an open file object connected to the file descriptor fd. This is an
alias of theopen()
built-in function and accepts the same arguments.
The only difference is that the first argument offdopen()
must always
be an integer.
16.1.4. File Descriptor Operations¶
These functions operate on I/O streams referenced using file descriptors.
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.
The fileno()
method can be used to obtain the file descriptor
associated with a file object when required. Note that using the file
descriptor directly will bypass the file object methods, ignoring aspects such
as internal buffering of data.
-
os.
close
(fd)¶ -
Close file descriptor fd.
Note
This function is intended for low-level I/O and must be applied to a file
descriptor as returned byos.open()
orpipe()
. To close a “file
object” returned by the built-in functionopen()
or bypopen()
or
fdopen()
, use itsclose()
method.
-
os.
closerange
(fd_low, fd_high)¶ -
Close all file descriptors from fd_low (inclusive) to fd_high (exclusive),
ignoring errors. Equivalent to (but much faster than):for fd in range(fd_low, fd_high): try: os.close(fd) except OSError: pass
-
os.
device_encoding
(fd)¶ -
Return a string describing the encoding of the device associated with fd
if it is connected to a terminal; else returnNone
.
-
os.
dup
(fd)¶ -
Return a duplicate of file descriptor fd. The new file descriptor is
non-inheritable.On Windows, when duplicating a standard stream (0: stdin, 1: stdout,
2: stderr), the new file descriptor is inheritable.Changed in version 3.4: The new file descriptor is now non-inheritable.
-
os.
dup2
(fd, fd2, inheritable=True)¶ -
Duplicate file descriptor fd to fd2, closing the latter first if necessary.
The file descriptor fd2 is inheritable by default,
or non-inheritable if inheritable isFalse
.Changed in version 3.4: Add the optional inheritable parameter.
-
os.
fchmod
(fd, mode)¶ -
Change the mode of the file given by fd to the numeric mode. See the
docs forchmod()
for possible values of mode. As of Python 3.3, this
is equivalent toos.chmod(fd, mode)
.Availability: Unix.
-
os.
fchown
(fd, uid, gid)¶ -
Change the owner and group id of the file given by fd to the numeric uid
and gid. To leave one of the ids unchanged, set it to -1. See
chown()
. As of Python 3.3, this is equivalent toos.chown(fd, uid,
.
gid)Availability: Unix.
-
os.
fdatasync
(fd)¶ -
Force write of file with filedescriptor fd to disk. Does not force update of
metadata.Availability: Unix.
Note
This function is not available on MacOS.
-
os.
fpathconf
(fd, name)¶ -
Return system configuration information relevant to an open file. name
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 thepathconf_names
dictionary. For configuration variables not
included in that mapping, passing an integer for name is also accepted.If name is a string and is not known,
ValueError
is raised. If a
specific value for name is not supported by the host system, even if it is
included inpathconf_names
, anOSError
is raised with
errno.EINVAL
for the error number.As of Python 3.3, this is equivalent to
os.pathconf(fd, name)
.Availability: Unix.
-
os.
fstat
(fd)¶ -
Get the status of the file descriptor fd. Return a
stat_result
object.As of Python 3.3, this is equivalent to
os.stat(fd)
.See also
The
stat()
function.
-
os.
fstatvfs
(fd)¶ -
Return information about the filesystem containing the file associated with
file descriptor fd, likestatvfs()
. As of Python 3.3, this is
equivalent toos.statvfs(fd)
.Availability: Unix.
-
os.
fsync
(fd)¶ -
Force write of file with filedescriptor fd to disk. On Unix, this calls the
nativefsync()
function; on Windows, the MS_commit()
function.If you’re starting with a buffered Python file object f, first do
f.flush()
, and then doos.fsync(f.fileno())
, to ensure that all internal
buffers associated with f are written to disk.Availability: Unix, Windows.
-
os.
ftruncate
(fd, length)¶ -
Truncate the file corresponding to file descriptor fd, so that it is at
most length bytes in size. As of Python 3.3, this is equivalent to
os.truncate(fd, length)
.Availability: Unix, Windows.
Changed in version 3.5: Added support for Windows
-
os.
get_blocking
(fd)¶ -
Get the blocking mode of the file descriptor:
False
if the
O_NONBLOCK
flag is set,True
if the flag is cleared.See also
set_blocking()
andsocket.socket.setblocking()
.Availability: Unix.
New in version 3.5.
-
os.
isatty
(fd)¶ -
Return
True
if the file descriptor fd is open and connected to a
tty(-like) device, elseFalse
.
-
os.
lockf
(fd, cmd, len)¶ -
Apply, test or remove a POSIX lock on an open file descriptor.
fd is an open file descriptor.
cmd specifies the command to use — one ofF_LOCK
,F_TLOCK
,
F_ULOCK
orF_TEST
.
len specifies the section of the file to lock.Availability: Unix.
New in version 3.3.
-
os.
F_LOCK
¶ -
os.
F_TLOCK
¶ -
os.
F_ULOCK
¶ -
os.
F_TEST
¶ -
Flags that specify what action
lockf()
will take.Availability: Unix.
New in version 3.3.
-
os.
lseek
(fd, pos, how)¶ -
Set the current position of file descriptor fd to position pos, modified
by how:SEEK_SET
or0
to set the position relative to the
beginning of the file;SEEK_CUR
or1
to set it relative to the
current position;SEEK_END
or2
to set it relative to the end of
the file. Return the new cursor position in bytes, starting from the beginning.
-
os.
SEEK_SET
¶ -
os.
SEEK_CUR
¶ -
os.
SEEK_END
¶ -
Parameters to the
lseek()
function. Their values are 0, 1, and 2,
respectively.New in version 3.3: Some operating systems could support additional values, like
os.SEEK_HOLE
oros.SEEK_DATA
.
-
os.
open
(path, flags, mode=0o777, *, dir_fd=None)¶ -
Open the file path and set various flags according to flags and possibly
its mode according to mode. When computing mode, the current umask value
is first masked out. Return the file descriptor for the newly opened file.
The new file descriptor is non-inheritable.For a description of the flag and mode values, see the C run-time documentation;
flag constants (likeO_RDONLY
andO_WRONLY
) are defined in
theos
module. In particular, on Windows adding
O_BINARY
is needed to open files in binary mode.This function can support paths relative to directory descriptors with the dir_fd parameter.
Changed in version 3.4: The new file descriptor is now non-inheritable.
Note
This function is intended for low-level I/O. For normal usage, use the
built-in functionopen()
, which returns a file object with
read()
andwrite()
methods (and many more). To
wrap a file descriptor in a file object, usefdopen()
.New in version 3.3: The dir_fd argument.
Changed in version 3.5: 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
InterruptedError
exception (see PEP 475 for the rationale).
The following constants are options for the flags parameter to the
open()
function. They can be combined using the bitwise OR operator
|
. Some of them are not available on all platforms. For descriptions of
their availability and use, consult the open(2) manual page on Unix
or the MSDN on Windows.
-
os.
O_RDONLY
¶ -
os.
O_WRONLY
¶ -
os.
O_RDWR
¶ -
os.
O_APPEND
¶ -
os.
O_CREAT
¶ -
os.
O_EXCL
¶ -
os.
O_TRUNC
¶ -
The above constants are available on Unix and Windows.
-
os.
O_DSYNC
¶ -
os.
O_RSYNC
¶ -
os.
O_SYNC
¶ -
os.
O_NDELAY
¶ -
os.
O_NONBLOCK
¶ -
os.
O_NOCTTY
¶ -
os.
O_CLOEXEC
¶ -
The above constants are only available on Unix.
Changed in version 3.3: Add
O_CLOEXEC
constant.
-
os.
O_BINARY
¶ -
os.
O_NOINHERIT
¶ -
os.
O_SHORT_LIVED
¶ -
os.
O_TEMPORARY
¶ -
os.
O_RANDOM
¶ -
os.
O_SEQUENTIAL
¶ -
os.
O_TEXT
¶ -
The above constants are only available on Windows.
-
os.
O_ASYNC
¶ -
os.
O_DIRECT
¶ -
os.
O_DIRECTORY
¶ -
os.
O_NOFOLLOW
¶ -
os.
O_NOATIME
¶ -
os.
O_PATH
¶ -
os.
O_TMPFILE
¶ -
os.
O_SHLOCK
¶ -
os.
O_EXLOCK
¶ -
The above constants are extensions and not present if they are not defined by
the C library.Changed in version 3.4: Add
O_PATH
on systems that support it.
AddO_TMPFILE
, only available on Linux Kernel 3.11
or newer.
-
os.
openpty
()¶ -
Open a new pseudo-terminal pair. Return a pair of file descriptors
(master, slave)
for the pty and the tty, respectively. The new file
descriptors are non-inheritable. For a (slightly) more
portable approach, use thepty
module.Availability: some flavors of Unix.
Changed in version 3.4: The new file descriptors are now non-inheritable.
-
os.
pipe
()¶ -
Create a pipe. Return a pair of file descriptors
(r, w)
usable for
reading and writing, respectively. The new file descriptor is
non-inheritable.Availability: Unix, Windows.
Changed in version 3.4: The new file descriptors are now non-inheritable.
-
os.
pipe2
(flags)¶ -
Create a pipe with flags set atomically.
flags can be constructed by ORing together one or more of these values:
O_NONBLOCK
,O_CLOEXEC
.
Return a pair of file descriptors(r, w)
usable for reading and writing,
respectively.Availability: some flavors of Unix.
New in version 3.3.
-
os.
posix_fallocate
(fd, offset, len)¶ -
Ensures that enough disk space is allocated for the file specified by fd
starting from offset and continuing for len bytes.Availability: Unix.
New in version 3.3.
-
os.
posix_fadvise
(fd, offset, len, advice)¶ -
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 fd starting at
offset and continuing for len bytes.
advice is one ofPOSIX_FADV_NORMAL
,POSIX_FADV_SEQUENTIAL
,
POSIX_FADV_RANDOM
,POSIX_FADV_NOREUSE
,
POSIX_FADV_WILLNEED
orPOSIX_FADV_DONTNEED
.Availability: Unix.
New in version 3.3.
-
os.
POSIX_FADV_NORMAL
¶ -
os.
POSIX_FADV_SEQUENTIAL
¶ -
os.
POSIX_FADV_RANDOM
¶ -
os.
POSIX_FADV_NOREUSE
¶ -
os.
POSIX_FADV_WILLNEED
¶ -
os.
POSIX_FADV_DONTNEED
¶ -
Flags that can be used in advice in
posix_fadvise()
that specify
the access pattern that is likely to be used.Availability: Unix.
New in version 3.3.
-
os.
pread
(fd, buffersize, offset)¶ -
Read from a file descriptor, fd, at a position of offset. It will read up
to buffersize number of bytes. The file offset remains unchanged.Availability: Unix.
New in version 3.3.
-
os.
pwrite
(fd, str, offset)¶ -
Write bytestring to a file descriptor, fd, from offset,
leaving the file offset unchanged.Availability: Unix.
New in version 3.3.
-
os.
read
(fd, n)¶ -
Read at most n bytes from file descriptor fd. Return a bytestring containing the
bytes read. If the end of the file referred to by fd has been reached, an
empty bytes object is returned.Note
This function is intended for low-level I/O and must be applied to a file
descriptor as returned byos.open()
orpipe()
. To read a
“file object” returned by the built-in functionopen()
or by
popen()
orfdopen()
, orsys.stdin
, use its
read()
orreadline()
methods.Changed in version 3.5: 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
InterruptedError
exception (see PEP 475 for the rationale).
-
os.
sendfile
(out, in, offset, count)¶ -
os.
sendfile
(out, in, offset, count, [headers, ][trailers, ]flags=0) -
Copy count bytes from file descriptor in to file descriptor out
starting at offset.
Return the number of bytes sent. When EOF is reached return 0.The first function notation is supported by all platforms that define
sendfile()
.On Linux, if offset is given as
None
, the bytes are read from the
current position of in and the position of in is updated.The second case may be used on Mac OS X and FreeBSD where headers and
trailers are arbitrary sequences of buffers that are written before and
after the data from in is written. It returns the same as the first case.On Mac OS X and FreeBSD, a value of 0 for count specifies to send until
the end of in is reached.All platforms support sockets as out file descriptor, and some platforms
allow other types (e.g. regular file, pipe) as well.Cross-platform applications should not use headers, trailers and flags
arguments.Availability: Unix.
New in version 3.3.
-
os.
set_blocking
(fd, blocking)¶ -
Set the blocking mode of the specified file descriptor. Set the
O_NONBLOCK
flag if blocking isFalse
, clear the flag otherwise.See also
get_blocking()
andsocket.socket.setblocking()
.Availability: Unix.
New in version 3.5.
-
os.
SF_NODISKIO
¶ -
os.
SF_MNOWAIT
¶ -
os.
SF_SYNC
¶ -
Parameters to the
sendfile()
function, if the implementation supports
them.Availability: Unix.
New in version 3.3.
-
os.
readv
(fd, buffers)¶ -
Read from a file descriptor fd into a number of mutable bytes-like
objects buffers.readv()
will 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.readv()
returns the total
number of bytes read (which may be less than the total capacity of all the
objects).Availability: Unix.
New in version 3.3.
-
os.
tcgetpgrp
(fd)¶ -
Return the process group associated with the terminal given by fd (an open
file descriptor as returned byos.open()
).Availability: Unix.
-
os.
tcsetpgrp
(fd, pg)¶ -
Set the process group associated with the terminal given by fd (an open file
descriptor as returned byos.open()
) to pg.Availability: Unix.
-
os.
ttyname
(fd)¶ -
Return a string which specifies the terminal device associated with
file descriptor fd. If fd is not associated with a terminal device, an
exception is raised.Availability: Unix.
-
os.
write
(fd, str)¶ -
Write the bytestring in str to file descriptor fd. Return the number of
bytes actually written.Note
This function is intended for low-level I/O and must be applied to a file
descriptor as returned byos.open()
orpipe()
. To write a “file
object” returned by the built-in functionopen()
or bypopen()
or
fdopen()
, orsys.stdout
orsys.stderr
, use its
write()
method.Changed in version 3.5: 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
InterruptedError
exception (see PEP 475 for the rationale).
-
os.
writev
(fd, buffers)¶ -
Write the contents of buffers to file descriptor fd. buffers must be a
sequence of bytes-like objects. Buffers are
processed in array order. Entire contents of first buffer is written before
proceeding to second, and so on. The operating system may set a limit
(sysconf() value SC_IOV_MAX) on the number of buffers that can be used.writev()
writes the contents of each object to the file descriptor
and returns the total number of bytes written.Availability: Unix.
New in version 3.3.
16.1.4.1. Querying the size of a terminal¶
New in version 3.3.
-
os.
get_terminal_size
(fd=STDOUT_FILENO)¶ -
Return the size of the terminal window as
(columns, lines)
,
tuple of typeterminal_size
.The optional argument
fd
(defaultSTDOUT_FILENO
, or standard
output) specifies which file descriptor should be queried.If the file descriptor is not connected to a terminal, an
OSError
is raised.shutil.get_terminal_size()
is the high-level function which
should normally be used,os.get_terminal_size
is the low-level
implementation.Availability: Unix, Windows.
-
class
os.
terminal_size
¶ -
A subclass of tuple, holding
(columns, lines)
of the terminal window size.-
columns
¶ -
Width of the terminal window in characters.
-
lines
¶ -
Height of the terminal window in characters.
-
16.1.4.2. Inheritance of File Descriptors¶
New in version 3.4.
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.
On UNIX, non-inheritable file descriptors are closed in child processes at the
execution of a new program, other file descriptors are inherited.
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 spawn*
functions,
all inheritable handles and all inheritable file descriptors are inherited.
Using the subprocess
module, all file descriptors except standard
streams are closed, and inheritable handles are only inherited if the
close_fds parameter is False
.
-
os.
get_inheritable
(fd)¶ -
Get the “inheritable” flag of the specified file descriptor (a boolean).
-
os.
set_inheritable
(fd, inheritable)¶ -
Set the “inheritable” flag of the specified file descriptor.
-
os.
get_handle_inheritable
(handle)¶ -
Get the “inheritable” flag of the specified handle (a boolean).
Availability: Windows.
-
os.
set_handle_inheritable
(handle, inheritable)¶ -
Set the “inheritable” flag of the specified handle.
Availability: Windows.
16.1.5. Files and Directories¶
On some Unix platforms, many of these functions support one or more of these
features:
-
specifying a file descriptor:
For some functions, the path argument can be not only a string giving a path
name, but also a file descriptor. The function will then operate on the file
referred to by the descriptor. (For POSIX systems, Python will call the
f...
version of the function.)You can check whether or not path can be specified as a file descriptor on
your platform usingos.supports_fd
. If it is unavailable, using it
will raise aNotImplementedError
.If the function also supports dir_fd or follow_symlinks arguments, it is
an error to specify one of those when supplying path as a file descriptor.
-
paths relative to directory descriptors: If dir_fd is not
None
, 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, dir_fd is ignored. (For POSIX systems, Python will call
the...at
orf...at
version of the function.)You can check whether or not dir_fd is supported on your platform using
os.supports_dir_fd
. If it is unavailable, using it will raise a
NotImplementedError
.
-
not following symlinks: If follow_symlinks is
False
, and the last element of the path to operate on is a symbolic link,
the function will operate on the symbolic link itself instead of the file the
link points to. (For POSIX systems, Python will call thel...
version of
the function.)You can check whether or not follow_symlinks is supported on your platform
usingos.supports_follow_symlinks
. If it is unavailable, using it
will raise aNotImplementedError
.
-
os.
access
(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)¶ -
Use the real uid/gid to test for access to path. 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
path. mode should beF_OK
to test the existence of path, or it
can be the inclusive OR of one or more ofR_OK
,W_OK
, and
X_OK
to test permissions. ReturnTrue
if access is allowed,
False
if not. See the Unix man page access(2) for more
information.This function can support specifying paths relative to directory
descriptors and not following symlinks.If effective_ids is
True
,access()
will perform its access
checks using the effective uid/gid instead of the real uid/gid.
effective_ids may not be supported on your platform; you can check whether
or not it is available usingos.supports_effective_ids
. If it is
unavailable, using it will raise aNotImplementedError
.Note
Using
access()
to check if a user is authorized to e.g. open a file
before actually doing so usingopen()
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 EAFP
techniques. For example:if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"
is better written as:
try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()
Note
I/O operations may fail even when
access()
indicates that they would
succeed, particularly for operations on network filesystems which may have
permissions semantics beyond the usual POSIX permission-bit model.Changed in version 3.3: Added the dir_fd, effective_ids, and follow_symlinks parameters.
-
os.
F_OK
¶ -
os.
R_OK
¶ -
os.
W_OK
¶ -
os.
X_OK
¶ -
Values to pass as the mode parameter of
access()
to test the
existence, readability, writability and executability of path,
respectively.
-
os.
chdir
(path)¶ -
Change the current working directory to path.
This function can support specifying a file descriptor. The
descriptor must refer to an opened directory, not an open file.New in version 3.3: Added support for specifying path as a file descriptor
on some platforms.
-
os.
chflags
(path, flags, *, follow_symlinks=True)¶ -
Set the flags of path to the numeric flags. flags may take a combination
(bitwise OR) of the following values (as defined in thestat
module):stat.UF_NODUMP
stat.UF_IMMUTABLE
stat.UF_APPEND
stat.UF_OPAQUE
stat.UF_NOUNLINK
stat.UF_COMPRESSED
stat.UF_HIDDEN
stat.SF_ARCHIVED
stat.SF_IMMUTABLE
stat.SF_APPEND
stat.SF_NOUNLINK
stat.SF_SNAPSHOT
This function can support not following symlinks.
Availability: Unix.
New in version 3.3: The follow_symlinks argument.
-
os.
chmod
(path, mode, *, dir_fd=None, follow_symlinks=True)¶ -
Change the mode of path to the numeric mode. mode may take one of the
following values (as defined in thestat
module) or bitwise ORed
combinations of them:stat.S_ISUID
stat.S_ISGID
stat.S_ENFMT
stat.S_ISVTX
stat.S_IREAD
stat.S_IWRITE
stat.S_IEXEC
stat.S_IRWXU
stat.S_IRUSR
stat.S_IWUSR
stat.S_IXUSR
stat.S_IRWXG
stat.S_IRGRP
stat.S_IWGRP
stat.S_IXGRP
stat.S_IRWXO
stat.S_IROTH
stat.S_IWOTH
stat.S_IXOTH
This function can support specifying a file descriptor,
paths relative to directory descriptors and not
following symlinks.Note
Although Windows supports
chmod()
, you can only set the file’s
read-only flag with it (via thestat.S_IWRITE
andstat.S_IREAD
constants or a corresponding integer value). All other bits are ignored.New in version 3.3: Added support for specifying path as an open file descriptor,
and the dir_fd and follow_symlinks arguments.
-
os.
chown
(path, uid, gid, *, dir_fd=None, follow_symlinks=True)¶ -
Change the owner and group id of path to the numeric uid and gid. To
leave one of the ids unchanged, set it to -1.This function can support specifying a file descriptor,
paths relative to directory descriptors and not
following symlinks.See
shutil.chown()
for a higher-level function that accepts names in
addition to numeric ids.Availability: Unix.
New in version 3.3: Added support for specifying an open file descriptor for path,
and the dir_fd and follow_symlinks arguments.
-
os.
chroot
(path)¶ -
Change the root directory of the current process to path.
Availability: Unix.
-
os.
fchdir
(fd)¶ -
Change the current working directory to the directory represented by the file
descriptor fd. The descriptor must refer to an opened directory, not an
open file. As of Python 3.3, this is equivalent toos.chdir(fd)
.Availability: Unix.
-
os.
getcwd
()¶ -
Return a string representing the current working directory.
-
os.
getcwdb
()¶ -
Return a bytestring representing the current working directory.
-
os.
lchflags
(path, flags)¶ -
Set the flags of path to the numeric flags, like
chflags()
, but do
not follow symbolic links. As of Python 3.3, this is equivalent to
os.chflags(path, flags, follow_symlinks=False)
.Availability: Unix.
-
os.
lchmod
(path, mode)¶ -
Change the mode of path to the numeric mode. If path is a symlink, this
affects the symlink rather than the target. See the docs forchmod()
for possible values of mode. As of Python 3.3, this is equivalent to
os.chmod(path, mode, follow_symlinks=False)
.Availability: Unix.
-
os.
lchown
(path, uid, gid)¶ -
Change the owner and group id of path to the numeric uid and gid. This
function will not follow symbolic links. As of Python 3.3, this is equivalent
toos.chown(path, uid, gid, follow_symlinks=False)
.Availability: Unix.
-
os.
link
(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)¶ -
Create a hard link pointing to src named dst.
This function can support specifying src_dir_fd and/or dst_dir_fd to
supply paths relative to directory descriptors, and not
following symlinks.Availability: Unix, Windows.
Changed in version 3.2: Added Windows support.
New in version 3.3: Added the src_dir_fd, dst_dir_fd, and follow_symlinks arguments.
Changed in version 3.6: Accepts a path-like object for src and dst.
-
os.
listdir
(path=’.’)¶ -
Return a list containing the names of the entries in the directory given by
path. The list is in arbitrary order, and does not include the special
entries'.'
and'..'
even if they are present in the directory.path may be a path-like object. If path is of type
bytes
(directly or indirectly through thePathLike
interface),
the filenames returned will also be of typebytes
;
in all other circumstances, they will be of typestr
.This function can also support specifying a file descriptor; the file descriptor must refer to a directory.
Note
To encode
str
filenames tobytes
, usefsencode()
.See also
The
scandir()
function returns directory entries along with
file attribute information, giving better performance for many
common use cases.Changed in version 3.2: The path parameter became optional.
New in version 3.3: Added support for specifying an open file descriptor for path.
-
os.
lstat
(path, *, dir_fd=None)¶ -
Perform the equivalent of an
lstat()
system call on the given path.
Similar tostat()
, but does not follow symbolic links. Return a
stat_result
object.On platforms that do not support symbolic links, this is an alias for
stat()
.As of Python 3.3, this is equivalent to
os.stat(path, dir_fd=dir_fd,
.
follow_symlinks=False)This function can also support paths relative to directory descriptors.
See also
The
stat()
function.Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
Changed in version 3.3: Added the dir_fd parameter.
Changed in version 3.6: Accepts a path-like object for src and dst.
-
os.
mkdir
(path, mode=0o777, *, dir_fd=None)¶ -
Create a directory named path with numeric mode mode.
If the directory already exists,
FileExistsError
is raised.On some systems, mode 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 mode) are set, their meaning is
platform-dependent. On some platforms, they are ignored and you should call
chmod()
explicitly to set them.This function can also support paths relative to directory descriptors.
It is also possible to create temporary directories; see the
tempfile
module’stempfile.mkdtemp()
function.New in version 3.3: The dir_fd argument.
-
os.
makedirs
(name, mode=0o777, exist_ok=False)¶ -
Recursive directory creation function. Like
mkdir()
, but makes all
intermediate-level directories needed to contain the leaf directory.The mode parameter is passed to
mkdir()
for creating the leaf
directory; see the mkdir() description for how it
is interpreted. To set the file permission bits of any newly-created parent
directories you can set the umask before invokingmakedirs()
. The
file permission bits of existing parent directories are not changed.If exist_ok is
False
(the default), anOSError
is raised if the
target directory already exists.Note
makedirs()
will become confused if the path elements to create
includepardir
(eg. ”..” on UNIX systems).This function handles UNC paths correctly.
New in version 3.2: The exist_ok parameter.
Changed in version 3.4.1: Before Python 3.4.1, if exist_ok was
True
and the directory existed,
makedirs()
would still raise an error if mode 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 bpo-21082.Changed in version 3.7: The mode argument no longer affects the file permission bits of
newly-created intermediate-level directories.
-
os.
mkfifo
(path, mode=0o666, *, dir_fd=None)¶ -
Create a FIFO (a named pipe) named path with numeric mode mode.
The current umask value is first masked out from the mode.This function can also support paths relative to directory descriptors.
FIFOs are pipes that can be accessed like regular files. FIFOs exist until they
are deleted (for example withos.unlink()
). 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 thatmkfifo()
doesn’t open the FIFO — it just creates the rendezvous point.Availability: Unix.
New in version 3.3: The dir_fd argument.
-
os.
mknod
(path, mode=0o600, device=0, *, dir_fd=None)¶ -
Create a filesystem node (file, device special file or named pipe) named
path. mode specifies both the permissions to use and the type of node
to be created, being combined (bitwise OR) with one ofstat.S_IFREG
,
stat.S_IFCHR
,stat.S_IFBLK
, andstat.S_IFIFO
(those constants are
available instat
). Forstat.S_IFCHR
andstat.S_IFBLK
,
device defines the newly created device special file (probably using
os.makedev()
), otherwise it is ignored.This function can also support paths relative to directory descriptors.
Availability: Unix.
New in version 3.3: The dir_fd argument.
-
os.
major
(device)¶ -
Extract the device major number from a raw device number (usually the
st_dev
orst_rdev
field fromstat
).
-
os.
minor
(device)¶ -
Extract the device minor number from a raw device number (usually the
st_dev
orst_rdev
field fromstat
).
-
os.
makedev
(major, minor)¶ -
Compose a raw device number from the major and minor device numbers.
-
os.
pathconf
(path, name)¶ -
Return system configuration information relevant to a named file. name
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 thepathconf_names
dictionary. For configuration variables not
included in that mapping, passing an integer for name is also accepted.If name is a string and is not known,
ValueError
is raised. If a
specific value for name is not supported by the host system, even if it is
included inpathconf_names
, anOSError
is raised with
errno.EINVAL
for the error number.This function can support specifying a file descriptor.
Availability: Unix.
-
os.
pathconf_names
¶ -
Dictionary mapping names accepted by
pathconf()
andfpathconf()
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.Availability: Unix.
-
os.
readlink
(path, *, dir_fd=None)¶ -
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
os.path.join(os.path.dirname(path), result)
.If the path is a string object (directly or indirectly through a
PathLike
interface), the result will also be a string object,
and the call may raise a UnicodeDecodeError. If the path is a bytes
object (direct or indirectly), the result will be a bytes object.This function can also support paths relative to directory descriptors.
Availability: Unix, Windows
Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
New in version 3.3: The dir_fd argument.
-
os.
remove
(path, *, dir_fd=None)¶ -
Remove (delete) the file path. If path is a directory,
OSError
is
raised. Usermdir()
to remove directories.This function can support paths relative to directory descriptors.
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.This function is semantically identical to
unlink()
.New in version 3.3: The dir_fd argument.
-
os.
removedirs
(name)¶ -
Remove directories recursively. Works like
rmdir()
except that, if the
leaf directory is successfully removed,removedirs()
tries to
successively remove every parent directory mentioned in path until an error
is raised (which is ignored, because it generally means that a parent directory
is not empty). For example,os.removedirs('foo/bar/baz')
will first remove
the directory'foo/bar/baz'
, and then remove'foo/bar'
and'foo'
if
they are empty. RaisesOSError
if the leaf directory could not be
successfully removed.
-
os.
rename
(src, dst, *, src_dir_fd=None, dst_dir_fd=None)¶ -
Rename the file or directory src to dst. If dst is a directory,
OSError
will be raised. On Unix, if dst exists and is a file, it will
be replaced silently if the user has permission. The operation may fail on some
Unix flavors if src and dst are on different filesystems. If successful,
the renaming will be an atomic operation (this is a POSIX requirement). On
Windows, if dst already exists,OSError
will be raised even if it is a
file.This function can support specifying src_dir_fd and/or dst_dir_fd to
supply paths relative to directory descriptors.If you want cross-platform overwriting of the destination, use
replace()
.New in version 3.3: The src_dir_fd and dst_dir_fd arguments.
Changed in version 3.6: Accepts a path-like object for src and dst.
-
os.
renames
(old, new)¶ -
Recursive directory or file renaming function. Works like
rename()
, 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 usingremovedirs()
.Note
This function can fail with the new directory structure made if you lack
permissions needed to remove the leaf directory or file.Changed in version 3.6: Accepts a path-like object for old and new.
-
os.
replace
(src, dst, *, src_dir_fd=None, dst_dir_fd=None)¶ -
Rename the file or directory src to dst. If dst is a directory,
OSError
will be raised. If dst exists and is a file, it will
be replaced silently if the user has permission. The operation may fail
if src and dst are on different filesystems. If successful,
the renaming will be an atomic operation (this is a POSIX requirement).This function can support specifying src_dir_fd and/or dst_dir_fd to
supply paths relative to directory descriptors.New in version 3.3.
Changed in version 3.6: Accepts a path-like object for src and dst.
-
os.
rmdir
(path, *, dir_fd=None)¶ -
Remove (delete) the directory path. Only works when the directory is
empty, otherwise,OSError
is raised. In order to remove whole
directory trees,shutil.rmtree()
can be used.This function can support paths relative to directory descriptors.
New in version 3.3: The dir_fd parameter.
-
os.
scandir
(path=’.’)¶ -
Return an iterator of
os.DirEntry
objects corresponding to the
entries in the directory given by path. The entries are yielded in
arbitrary order, and the special entries'.'
and'..'
are not
included.Using
scandir()
instead oflistdir()
can significantly
increase the performance of code that also needs file type or file
attribute information, becauseos.DirEntry
objects expose this
information if the operating system provides it when scanning a directory.
Allos.DirEntry
methods may perform a system call, but
is_dir()
andis_file()
usually only
require a system call for symbolic links;os.DirEntry.stat()
always requires a system call on Unix but only requires one for
symbolic links on Windows.path may be a path-like object. If path is of type
bytes
(directly or indirectly through thePathLike
interface),
the type of thename
andpath
attributes of eachos.DirEntry
will bebytes
; in all other
circumstances, they will be of typestr
.This function can also support specifying a file descriptor; the file descriptor must refer to a directory.
The
scandir()
iterator supports the context manager protocol
and has the following method:-
scandir.
close
()¶ -
Close the iterator and free acquired resources.
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 thewith
statement.New in version 3.6.
The following example shows a simple use of
scandir()
to display all
the files (excluding directories) in the given path that don’t start with
'.'
. Theentry.is_file()
call will generally not make an additional
system call:with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)
New in version 3.5.
Changed in version 3.7: Added support for file descriptors on Unix.
-
-
class
os.
DirEntry
¶ -
Object yielded by
scandir()
to expose the file path and other file
attributes of a directory entry.scandir()
will provide as much of this information as possible without
making additional system calls. When astat()
orlstat()
system call
is made, theos.DirEntry
object will cache the result.os.DirEntry
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 callingscandir()
, callos.stat(entry.path)
to fetch
up-to-date information.Because the
os.DirEntry
methods can make operating system calls, they may
also raiseOSError
. If you need very fine-grained
control over errors, you can catchOSError
when calling one of the
os.DirEntry
methods and handle as appropriate.To be directly usable as a path-like object,
os.DirEntry
implements thePathLike
interface.Attributes and methods on a
os.DirEntry
instance are as follows:-
name
¶ -
The entry’s base filename, relative to the
scandir()
path
argument.The
name
attribute will bebytes
if thescandir()
path argument is of typebytes
andstr
otherwise. Use
fsdecode()
to decode byte filenames.
-
path
¶ -
The entry’s full path name: equivalent to
os.path.join(scandir_path,
where scandir_path is the
entry.name)scandir()
path
argument. The path is only absolute if thescandir()
path
argument was absolute. If thescandir()
path
argument was a file descriptor, thepath
attribute is the same as thename
attribute.The
path
attribute will bebytes
if thescandir()
path argument is of typebytes
andstr
otherwise. Use
fsdecode()
to decode byte filenames.
-
inode
()¶ -
Return the inode number of the entry.
The result is cached on the
os.DirEntry
object. Use
os.stat(entry.path, follow_symlinks=False).st_ino
to fetch up-to-date
information.On the first, uncached call, a system call is required on Windows but
not on Unix.
-
is_dir
(*, follow_symlinks=True)¶ -
Return
True
if this entry is a directory or a symbolic link pointing
to a directory; returnFalse
if the entry is or points to any other
kind of file, or if it doesn’t exist anymore.If follow_symlinks is
False
, returnTrue
only if this entry
is a directory (without following symlinks); returnFalse
if the
entry is any other kind of file or if it doesn’t exist anymore.The result is cached on the
os.DirEntry
object, with a separate cache
for follow_symlinksTrue
andFalse
. Callos.stat()
along
withstat.S_ISDIR()
to fetch up-to-date information.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 returndirent.d_type == DT_UNKNOWN
. If the entry is a symlink,
a system call will be required to follow the symlink unless
follow_symlinks isFalse
.This method can raise
OSError
, such asPermissionError
,
butFileNotFoundError
is caught and not raised.
-
is_file
(*, follow_symlinks=True)¶ -
Return
True
if this entry is a file or a symbolic link pointing to a
file; returnFalse
if the entry is or points to a directory or other
non-file entry, or if it doesn’t exist anymore.If follow_symlinks is
False
, returnTrue
only if this entry
is a file (without following symlinks); returnFalse
if the entry is
a directory or other non-file entry, or if it doesn’t exist anymore.The result is cached on the
os.DirEntry
object. Caching, system calls
made, and exceptions raised are as peris_dir()
.
-
is_symlink
()¶ -
Return
True
if this entry is a symbolic link (even if broken);
returnFalse
if the entry points to a directory or any kind of file,
or if it doesn’t exist anymore.The result is cached on the
os.DirEntry
object. Call
os.path.islink()
to fetch up-to-date information.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
dirent.d_type == DT_UNKNOWN
.This method can raise
OSError
, such asPermissionError
,
butFileNotFoundError
is caught and not raised.
-
stat
(*, follow_symlinks=True)¶ -
Return a
stat_result
object for this entry. This method
follows symbolic links by default; to stat a symbolic link add the
follow_symlinks=False
argument.On Unix, this method always requires a system call. On Windows, it
only requires a system call if follow_symlinks isTrue
and the
entry is a symbolic link.On Windows, the
st_ino
,st_dev
andst_nlink
attributes of the
stat_result
are always set to zero. Callos.stat()
to
get these attributes.The result is cached on the
os.DirEntry
object, with a separate cache
for follow_symlinksTrue
andFalse
. Callos.stat()
to
fetch up-to-date information.
Note that there is a nice correspondence between several attributes
and methods ofos.DirEntry
and ofpathlib.Path
. In
particular, thename
attribute has the same
meaning, as do theis_dir()
,is_file()
,is_symlink()
andstat()
methods.New in version 3.5.
Changed in version 3.6: Added support for the
PathLike
interface. Added support
forbytes
paths on Windows. -
-
os.
stat
(path, *, dir_fd=None, follow_symlinks=True)¶ -
Get the status of a file or a file descriptor. Perform the equivalent of a
stat()
system call on the given path. path may be specified as
either a string or bytes – directly or indirectly through thePathLike
interface – or as an open file descriptor. Return astat_result
object.This function normally follows symlinks; to stat a symlink add the argument
follow_symlinks=False
, or uselstat()
.This function can support specifying a file descriptor and
not following symlinks.Example:
>>> 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
See also
fstat()
andlstat()
functions.New in version 3.3: Added the dir_fd and follow_symlinks arguments, specifying a file
descriptor instead of a path.
-
class
os.
stat_result
¶ -
Object whose attributes correspond roughly to the members of the
stat
structure. It is used for the result ofos.stat()
,
os.fstat()
andos.lstat()
.Attributes:
-
st_mode
¶ -
File mode: file type and file mode bits (permissions).
-
st_ino
¶ -
Inode number.
-
st_dev
¶ -
Identifier of the device on which this file resides.
-
st_nlink
¶ -
Number of hard links.
-
st_uid
¶ -
User identifier of the file owner.
-
st_gid
¶ -
Group identifier of the file owner.
-
st_size
¶ -
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.
Timestamps:
-
st_atime
¶ -
Time of most recent access expressed in seconds.
-
st_mtime
¶ -
Time of most recent content modification expressed in seconds.
-
st_ctime
¶ -
Platform dependent:
- the time of most recent metadata change on Unix,
- the time of creation on Windows, expressed in seconds.
-
st_atime_ns
¶ -
Time of most recent access expressed in nanoseconds as an integer.
-
st_mtime_ns
¶ -
Time of most recent content modification expressed in nanoseconds as an
integer.
-
st_ctime_ns
¶ -
Platform dependent:
- the time of most recent metadata change on Unix,
- the time of creation on Windows, expressed in nanoseconds as an
integer.
Note
The exact meaning and resolution of the
st_atime
,
st_mtime
, andst_ctime
attributes depend on the operating
system and the file system. For example, on Windows systems using the FAT
or FAT32 file systems,st_mtime
has 2-second resolution, and
st_atime
has only 1-day resolution. See your operating system
documentation for details.Similarly, although
st_atime_ns
,st_mtime_ns
,
andst_ctime_ns
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
storest_atime
,st_mtime
, andst_ctime
cannot preserve all of it, and as such will be slightly inexact.
If you need the exact timestamps you should always use
st_atime_ns
,st_mtime_ns
, andst_ctime_ns
.On some Unix systems (such as Linux), the following attributes may also be
available:-
st_blocks
¶ -
Number of 512-byte blocks allocated for file.
This may be smaller thanst_size
/512 when the file has holes.
-
st_blksize
¶ -
“Preferred” blocksize for efficient file system I/O. Writing to a file in
smaller chunks may cause an inefficient read-modify-rewrite.
-
st_rdev
¶ -
Type of device if an inode device.
-
st_flags
¶ -
User defined flags for file.
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):-
st_gen
¶ -
File generation number.
-
st_birthtime
¶ -
Time of file creation.
On Mac OS systems, the following attributes may also be available:
-
st_rsize
¶ -
Real size of the file.
-
st_creator
¶ -
Creator of the file.
-
st_type
¶ -
File type.
On Windows systems, the following attribute is also available:
-
st_file_attributes
¶ -
Windows file attributes:
dwFileAttributes
member of the
BY_HANDLE_FILE_INFORMATION
structure returned by
GetFileInformationByHandle()
. See theFILE_ATTRIBUTE_*
constants in thestat
module.
The standard module
stat
defines functions and constants that are
useful for extracting information from astat
structure. (On
Windows, some items are filled with dummy values.)For backward compatibility, a
stat_result
instance is also
accessible as a tuple of at least 10 integers giving the most important (and
portable) members of thestat
structure, in the order
st_mode
,st_ino
,st_dev
,st_nlink
,
st_uid
,st_gid
,st_size
,st_atime
,
st_mtime
,st_ctime
. More items may be added at the end by
some implementations. For compatibility with older Python versions,
accessingstat_result
as a tuple always returns integers.New in version 3.3: Added the
st_atime_ns
,st_mtime_ns
, and
st_ctime_ns
members.New in version 3.5: Added the
st_file_attributes
member on Windows. -
-
os.
statvfs
(path)¶ -
Perform a
statvfs()
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 thestatvfs
structure, namely:
f_bsize
,f_frsize
,f_blocks
,f_bfree
,
f_bavail
,f_files
,f_ffree
,f_favail
,
f_flag
,f_namemax
.Two module-level constants are defined for the
f_flag
attribute’s
bit-flags: ifST_RDONLY
is set, the filesystem is mounted
read-only, and ifST_NOSUID
is set, the semantics of
setuid/setgid bits are disabled or not supported.Additional module-level constants are defined for GNU/glibc based systems.
These areST_NODEV
(disallow access to device special files),
ST_NOEXEC
(disallow program execution),ST_SYNCHRONOUS
(writes are synced at once),ST_MANDLOCK
(allow mandatory locks on an FS),
ST_WRITE
(write on file/directory/symlink),ST_APPEND
(append-only file),ST_IMMUTABLE
(immutable file),ST_NOATIME
(do not update access times),ST_NODIRATIME
(do not update directory access
times),ST_RELATIME
(update atime relative to mtime/ctime).This function can support specifying a file descriptor.
Availability: Unix.
Changed in version 3.2: The
ST_RDONLY
andST_NOSUID
constants were added.New in version 3.3: Added support for specifying an open file descriptor for path.
Changed in version 3.4: The
ST_NODEV
,ST_NOEXEC
,ST_SYNCHRONOUS
,
ST_MANDLOCK
,ST_WRITE
,ST_APPEND
,
ST_IMMUTABLE
,ST_NOATIME
,ST_NODIRATIME
,
andST_RELATIME
constants were added.
-
os.
supports_dir_fd
¶ -
A
Set
object indicating which functions in the
os
module permit use of their dir_fd parameter. Different platforms
provide different functionality, and an option that might work on one might
be unsupported on another. For consistency’s sakes, functions that support
dir_fd always allow specifying the parameter, but will raise an exception
if the functionality is not actually available.To check whether a particular function permits use of its dir_fd
parameter, use thein
operator onsupports_dir_fd
. As an example,
this expression determines whether the dir_fd parameter ofos.stat()
is locally available:os.stat in os.supports_dir_fd
Currently dir_fd parameters only work on Unix platforms; none of them work
on Windows.New in version 3.3.
-
os.
supports_effective_ids
¶ -
A
Set
object indicating which functions in the
os
module permit use of the effective_ids parameter for
os.access()
. If the local platform supports it, the collection will
containos.access()
, otherwise it will be empty.To check whether you can use the effective_ids parameter for
os.access()
, use thein
operator onsupports_effective_ids
,
like so:os.access in os.supports_effective_ids
Currently effective_ids only works on Unix platforms; it does not work on
Windows.New in version 3.3.
-
os.
supports_fd
¶ -
A
Set
object indicating which functions in the
os
module permit specifying their path parameter as an open file
descriptor. Different platforms provide different functionality, and an
option that might work on one might be unsupported on another. For
consistency’s sakes, functions that support fd always allow specifying
the parameter, but will raise an exception if the functionality is not
actually available.To check whether a particular function permits specifying an open file
descriptor for its path parameter, use thein
operator on
supports_fd
. As an example, this expression determines whether
os.chdir()
accepts open file descriptors when called on your local
platform:os.chdir in os.supports_fd
New in version 3.3.
-
os.
supports_follow_symlinks
¶ -
A
Set
object indicating which functions in the
os
module permit use of their follow_symlinks parameter. Different
platforms provide different functionality, and an option that might work on
one might be unsupported on another. For consistency’s sakes, functions that
support follow_symlinks always allow specifying the parameter, but will
raise an exception if the functionality is not actually available.To check whether a particular function permits use of its follow_symlinks
parameter, use thein
operator onsupports_follow_symlinks
. As an
example, this expression determines whether the follow_symlinks parameter
ofos.stat()
is locally available:os.stat in os.supports_follow_symlinks
New in version 3.3.
-
os.
symlink
(src, dst, target_is_directory=False, *, dir_fd=None)¶ -
Create a symbolic link pointing to src named dst.
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 target_is_directory isTrue
or a file symlink (the
default) otherwise. On non-Window platforms, target_is_directory is ignored.Symbolic link support was introduced in Windows 6.0 (Vista).
symlink()
will raise aNotImplementedError
on Windows versions earlier than 6.0.This function can support paths relative to directory descriptors.
Note
On Windows, the SeCreateSymbolicLinkPrivilege is required in order to
successfully create symlinks. This privilege is not typically granted to
regular users but is available to accounts which can escalate privileges
to the administrator level. Either obtaining the privilege or running your
application as an administrator are ways to successfully create symlinks.OSError
is raised when the function is called by an unprivileged
user.Availability: Unix, Windows.
Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
New in version 3.3: Added the dir_fd argument, and now allow target_is_directory
on non-Windows platforms.Changed in version 3.6: Accepts a path-like object for src and dst.
-
os.
sync
()¶ -
Force write of everything to disk.
Availability: Unix.
New in version 3.3.
-
os.
truncate
(path, length)¶ -
Truncate the file corresponding to path, so that it is at most
length bytes in size.This function can support specifying a file descriptor.
Availability: Unix, Windows.
New in version 3.3.
Changed in version 3.5: Added support for Windows
-
os.
unlink
(path, *, dir_fd=None)¶ -
Remove (delete) the file path. This function is semantically
identical toremove()
; theunlink
name is its
traditional Unix name. Please see the documentation for
remove()
for further information.New in version 3.3: The dir_fd parameter.
-
os.
utime
(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True)¶ -
Set the access and modified times of the file specified by path.
utime()
takes two optional parameters, times and ns.
These specify the times set on path and are used as follows:- If ns is specified,
it must be a 2-tuple of the form(atime_ns, mtime_ns)
where each member is an int expressing nanoseconds. - If times is not
None
,
it must be a 2-tuple of the form(atime, mtime)
where each member is an int or float expressing seconds. - If times is
None
and ns is unspecified,
this is equivalent to specifyingns=(atime_ns, mtime_ns)
where both times are the current time.
It is an error to specify tuples for both times and ns.
Whether a directory can be given for path
depends on whether the operating system implements directories as files
(for example, Windows does not). Note that the exact times you set here may
not be returned by a subsequentstat()
call, depending on the
resolution with which your operating system records access and modification
times; seestat()
. The best way to preserve exact times is to
use the st_atime_ns and st_mtime_ns fields from theos.stat()
result object with the ns parameter to utime.This function can support specifying a file descriptor,
paths relative to directory descriptors and not
following symlinks.New in version 3.3: Added support for specifying an open file descriptor for path,
and the dir_fd, follow_symlinks, and ns parameters. - If ns is specified,
-
os.
walk
(top, topdown=True, onerror=None, followlinks=False)¶ -
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
top (including top itself), it yields a 3-tuple(dirpath, dirnames,
.
filenames)dirpath is a string, the path to the directory. dirnames is a list of the
names of the subdirectories in dirpath (excluding'.'
and'..'
).
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists contain no path components. To get a full path
(which begins with top) to a file or directory in dirpath, do
os.path.join(dirpath, name)
.If optional argument topdown is
True
or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top-down). If topdown isFalse
, 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 topdown, the
list of subdirectories is retrieved before the tuples for the directory and
its subdirectories are generated.When topdown is
True
, the caller can modify the dirnames list in-place
(perhaps usingdel
or slice assignment), andwalk()
will only
recurse into the subdirectories whose names remain in dirnames; this can be
used to prune the search, impose a specific order of visiting, or even to inform
walk()
about directories the caller creates or renames before it resumes
walk()
again. Modifying dirnames when topdown isFalse
has
no effect on the behavior of the walk, because in bottom-up mode the directories
in dirnames are generated before dirpath itself is generated.By default, errors from the
listdir()
call are ignored. If optional
argument onerror is specified, it should be a function; it will be called with
one argument, anOSError
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 thefilename
attribute of the exception object.By default,
walk()
will not walk down into symbolic links that resolve to
directories. Set followlinks toTrue
to visit directories pointed to by
symlinks, on systems that support them.Note
Be aware that setting followlinks to
True
can lead to infinite
recursion if a link points to a parent directory of itself.walk()
does not keep track of the directories it visited already.Note
If you pass a relative pathname, don’t change the current working directory
between resumptions ofwalk()
.walk()
never changes the current
directory, and assumes that its caller doesn’t either.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: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
In the next example (simple implementation of
shutil.rmtree()
),
walking the tree bottom-up is essential,rmdir()
doesn’t allow
deleting a directory before the directory is empty:# 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))
Changed in version 3.5: This function now calls
os.scandir()
instead ofos.listdir()
,
making it faster by reducing the number of calls toos.stat()
.
-
os.
fwalk
(top=’.’, topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)¶ -
This behaves exactly like
walk()
, except that it yields a 4-tuple
(dirpath, dirnames, filenames, dirfd)
, and it supportsdir_fd
.dirpath, dirnames and filenames are identical to
walk()
output,
and dirfd is a file descriptor referring to the directory dirpath.This function always supports paths relative to directory descriptors and not following symlinks. Note however
that, unlike other functions, thefwalk()
default value for
follow_symlinks isFalse
.Note
Since
fwalk()
yields file descriptors, those are only valid until
the next iteration step, so you should duplicate them (e.g. with
dup()
) if you want to keep them longer.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: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
In the next example, walking the tree bottom-up is essential:
rmdir()
doesn’t allow deleting a directory before the directory is
empty:# 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)
Availability: Unix.
New in version 3.3.
Changed in version 3.7: Added support for
bytes
paths.
16.1.5.1. Linux extended attributes¶
New in version 3.3.
These functions are all available on Linux only.
-
os.
getxattr
(path, attribute, *, follow_symlinks=True)¶ -
Return the value of the extended filesystem attribute attribute for
path. attribute can be bytes or str (directly or indirectly through the
PathLike
interface). If it is str, it is encoded with the filesystem
encoding.This function can support specifying a file descriptor and
not following symlinks.Changed in version 3.6: Accepts a path-like object for path and attribute.
-
os.
listxattr
(path=None, *, follow_symlinks=True)¶ -
Return a list of the extended filesystem attributes on path. The
attributes in the list are represented as strings decoded with the filesystem
encoding. If path isNone
,listxattr()
will examine the current
directory.This function can support specifying a file descriptor and
not following symlinks.
-
os.
removexattr
(path, attribute, *, follow_symlinks=True)¶ -
Removes the extended filesystem attribute attribute from path.
attribute should be bytes or str (directly or indirectly through the
PathLike
interface). If it is a string, it is encoded
with the filesystem encoding.This function can support specifying a file descriptor and
not following symlinks.Changed in version 3.6: Accepts a path-like object for path and attribute.
-
os.
setxattr
(path, attribute, value, flags=0, *, follow_symlinks=True)¶ -
Set the extended filesystem attribute attribute on path to value.
attribute must be a bytes or str with no embedded NULs (directly or
indirectly through thePathLike
interface). If it is a str,
it is encoded with the filesystem encoding. flags may be
XATTR_REPLACE
orXATTR_CREATE
. IfXATTR_REPLACE
is
given and the attribute does not exist,EEXISTS
will be raised.
IfXATTR_CREATE
is given and the attribute already exists, the
attribute will not be created andENODATA
will be raised.This function can support specifying a file descriptor and
not following symlinks.Note
A bug in Linux kernel versions less than 2.6.39 caused the flags argument
to be ignored on some filesystems.Changed in version 3.6: Accepts a path-like object for path and attribute.
-
os.
XATTR_SIZE_MAX
¶ -
The maximum size the value of an extended attribute can be. Currently, this
is 64 KiB on Linux.
-
os.
XATTR_CREATE
¶ -
This is a possible value for the flags argument in
setxattr()
. It
indicates the operation must create an attribute.
-
os.
XATTR_REPLACE
¶ -
This is a possible value for the flags argument in
setxattr()
. It
indicates the operation must replace an existing attribute.
16.1.6. Process Management¶
These functions may be used to create and manage processes.
The various exec*
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 argv[0]
passed to a program’s main()
. For example, os.execv('/bin/echo',
will only print
['foo', 'bar'])bar
on standard output; foo
will seem
to be ignored.
-
os.
abort
()¶ -
Generate a
SIGABRT
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 of3
. Be aware that calling this function will not call the
Python signal handler registered forSIGABRT
with
signal.signal()
.
-
os.
execl
(path, arg0, arg1, …)¶ -
os.
execle
(path, arg0, arg1, …, env)¶ -
os.
execlp
(file, arg0, arg1, …)¶ -
os.
execlpe
(file, arg0, arg1, …, env)¶ -
os.
execv
(path, args)¶ -
os.
execve
(path, args, env)¶ -
os.
execvp
(file, args)¶ -
os.
execvpe
(file, args, env)¶ -
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
OSError
exceptions.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
sys.stdout.flush()
oros.fsync()
before calling an
exec*
function.The “l” and “v” variants of the
exec*
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 theexecl*()
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 args
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.The variants which include a “p” near the end (
execlp()
,
execlpe()
,execvp()
, andexecvpe()
) will use the
PATH
environment variable to locate the program file. When the
environment is being replaced (using one of theexec*e
variants,
discussed in the next paragraph), the new environment is used as the source of
thePATH
variable. The other variants,execl()
,execle()
,
execv()
, andexecve()
, will not use thePATH
variable to
locate the executable; path must contain an appropriate absolute or relative
path.For
execle()
,execlpe()
,execve()
, andexecvpe()
(note
that these all end in “e”), the env 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 functionsexecl()
,
execlp()
,execv()
, andexecvp()
all cause the new process to
inherit the environment of the current process.For
execve()
on some platforms, path 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 usingos.supports_fd
.
If it is unavailable, using it will raise aNotImplementedError
.Availability: Unix, Windows.
New in version 3.3: Added support for specifying an open file descriptor for path
forexecve()
.
-
os.
_exit
(n)¶ -
Exit the process with status n, without calling cleanup handlers, flushing
stdio buffers, etc.Note
The standard way to exit is
sys.exit(n)
._exit()
should
normally only be used in the child process after afork()
.
The following exit codes are defined and can be used with _exit()
,
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.
Note
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.
-
os.
EX_OK
¶ -
Exit code that means no error occurred.
Availability: Unix.
-
os.
EX_USAGE
¶ -
Exit code that means the command was used incorrectly, such as when the wrong
number of arguments are given.Availability: Unix.
-
os.
EX_DATAERR
¶ -
Exit code that means the input data was incorrect.
Availability: Unix.
-
os.
EX_NOINPUT
¶ -
Exit code that means an input file did not exist or was not readable.
Availability: Unix.
-
os.
EX_NOUSER
¶ -
Exit code that means a specified user did not exist.
Availability: Unix.
-
os.
EX_NOHOST
¶ -
Exit code that means a specified host did not exist.
Availability: Unix.
-
os.
EX_UNAVAILABLE
¶ -
Exit code that means that a required service is unavailable.
Availability: Unix.
-
os.
EX_SOFTWARE
¶ -
Exit code that means an internal software error was detected.
Availability: Unix.
-
os.
EX_OSERR
¶ -
Exit code that means an operating system error was detected, such as the
inability to fork or create a pipe.Availability: Unix.
-
os.
EX_OSFILE
¶ -
Exit code that means some system file did not exist, could not be opened, or had
some other kind of error.Availability: Unix.
-
os.
EX_CANTCREAT
¶ -
Exit code that means a user specified output file could not be created.
Availability: Unix.
-
os.
EX_IOERR
¶ -
Exit code that means that an error occurred while doing I/O on some file.
Availability: Unix.
-
os.
EX_TEMPFAIL
¶ -
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.Availability: Unix.
-
os.
EX_PROTOCOL
¶ -
Exit code that means that a protocol exchange was illegal, invalid, or not
understood.Availability: Unix.
-
os.
EX_NOPERM
¶ -
Exit code that means that there were insufficient permissions to perform the
operation (but not intended for file system problems).Availability: Unix.
-
os.
EX_CONFIG
¶ -
Exit code that means that some kind of configuration error occurred.
Availability: Unix.
-
os.
EX_NOTFOUND
¶ -
Exit code that means something like “an entry was not found”.
Availability: Unix.
-
os.
fork
()¶ -
Fork a child process. Return
0
in the child and the child’s process id in the
parent. If an error occursOSError
is raised.Note that some platforms including FreeBSD <= 6.3 and Cygwin have
known issues when using fork() from a thread.Warning
See
ssl
for applications that use the SSL module with fork().Availability: Unix.
-
os.
forkpty
()¶ -
Fork a child process, using a new pseudo-terminal as the child’s controlling
terminal. Return a pair of(pid, fd)
, where pid is0
in the child, the
new child’s process id in the parent, and fd is the file descriptor of the
master end of the pseudo-terminal. For a more portable approach, use the
pty
module. If an error occursOSError
is raised.Availability: some flavors of Unix.
-
os.
kill
(pid, sig)¶ -
Send signal sig to the process pid. Constants for the specific signals
available on the host platform are defined in thesignal
module.Windows: The
signal.CTRL_C_EVENT
and
signal.CTRL_BREAK_EVENT
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 sig will cause the process
to be unconditionally killed by the TerminateProcess API, and the exit code
will be set to sig. The Windows version ofkill()
additionally takes
process handles to be killed.See also
signal.pthread_kill()
.New in version 3.2: Windows support.
-
os.
killpg
(pgid, sig)¶ -
Send the signal sig to the process group pgid.
Availability: Unix.
-
os.
nice
(increment)¶ -
Add increment to the process’s “niceness”. Return the new niceness.
Availability: Unix.
-
os.
plock
(op)¶ -
Lock program segments into memory. The value of op (defined in
<sys/lock.h>
) determines which segments are locked.Availability: Unix.
-
os.
popen
(cmd, mode=’r’, buffering=-1)¶ -
Open a pipe to or from command cmd.
The return value is an open file object
connected to the pipe, which can be read or written depending on whether mode
is'r'
(default) or'w'
. The buffering argument has the same meaning as
the corresponding argument to the built-inopen()
function. The
returned file object reads or writes text strings rather than bytes.The
close
method returnsNone
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- signal.SIGKILL
if the
subprocess was killed.) On Windows systems, the return value
contains the signed integer return code from the child process.This is implemented using
subprocess.Popen
; see that class’s
documentation for more powerful ways to manage and communicate with
subprocesses.
-
os.
register_at_fork
(*, before=None, after_in_parent=None, after_in_child=None)¶ -
Register callables to be executed when a new child process is forked
usingos.fork()
or similar process cloning APIs.
The parameters are optional and keyword-only.
Each specifies a different call point.- before is a function called before forking a child process.
- after_in_parent is a function called from the parent process
after forking a child process. - after_in_child is a function called from the child process.
These calls are only made if control is expected to return to the
Python interpreter. A typicalsubprocess
launch will not
trigger them as the child is not going to re-enter the interpreter.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.Note that
fork()
calls made by third-party C code may not
call those functions, unless it explicitly callsPyOS_BeforeFork()
,
PyOS_AfterFork_Parent()
andPyOS_AfterFork_Child()
.There is no way to unregister a function.
Availability: Unix.
New in version 3.7.
-
os.
spawnl
(mode, path, …)¶ -
os.
spawnle
(mode, path, …, env)¶ -
os.
spawnlp
(mode, file, …)¶ -
os.
spawnlpe
(mode, file, …, env)¶ -
os.
spawnv
(mode, path, args)¶ -
os.
spawnve
(mode, path, args, env)¶ -
os.
spawnvp
(mode, file, args)¶ -
os.
spawnvpe
(mode, file, args, env)¶ -
Execute the program path in a new process.
(Note that the
subprocess
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
Replacing Older Functions with the subprocess Module section.)If mode is
P_NOWAIT
, this function returns the process id of the new
process; if mode isP_WAIT
, returns the process’s exit code if it
exits normally, or-signal
, where signal is the signal that killed the
process. On Windows, the process id will actually be the process handle, so can
be used with thewaitpid()
function.The “l” and “v” variants of the
spawn*
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
spawnl*()
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 args parameter. In either case, the arguments to the child process must
start with the name of the command being run.The variants which include a second “p” near the end (
spawnlp()
,
spawnlpe()
,spawnvp()
, andspawnvpe()
) will use the
PATH
environment variable to locate the program file. When the
environment is being replaced (using one of thespawn*e
variants,
discussed in the next paragraph), the new environment is used as the source of
thePATH
variable. The other variants,spawnl()
,
spawnle()
,spawnv()
, andspawnve()
, will not use the
PATH
variable to locate the executable; path must contain an
appropriate absolute or relative path.For
spawnle()
,spawnlpe()
,spawnve()
, andspawnvpe()
(note that these all end in “e”), the env 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
spawnl()
,spawnlp()
,spawnv()
, andspawnvp()
all cause
the new process to inherit the environment of the current process. Note that
keys and values in the env dictionary must be strings; invalid keys or
values will cause the function to fail, with a return value of127
.As an example, the following calls to
spawnlp()
andspawnvpe()
are
equivalent: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)
Availability: Unix, Windows.
spawnlp()
,spawnlpe()
,spawnvp()
andspawnvpe()
are not available on Windows.spawnle()
and
spawnve()
are not thread-safe on Windows; we advise you to use the
subprocess
module instead.
-
os.
P_NOWAIT
¶ -
os.
P_NOWAITO
¶ -
Possible values for the mode parameter to the
spawn*
family of
functions. If either of these values is given, thespawn*()
functions
will return as soon as the new process has been created, with the process id as
the return value.Availability: Unix, Windows.
-
os.
P_WAIT
¶ -
Possible value for the mode parameter to the
spawn*
family of
functions. If this is given as mode, thespawn*()
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-signal
if a signal kills the
process.Availability: Unix, Windows.
-
os.
P_DETACH
¶ -
os.
P_OVERLAY
¶ -
Possible values for the mode parameter to the
spawn*
family of
functions. These are less portable than those listed above.P_DETACH
is similar toP_NOWAIT
, but the new process is detached from the
console of the calling process. IfP_OVERLAY
is used, the current
process will be replaced; thespawn*
function will not return.Availability: Windows.
-
os.
startfile
(path[, operation])¶ -
Start a file with its associated application.
When operation is not specified or
'open'
, this acts like double-clicking
the file in Windows Explorer, or giving the file name as an argument to the
start command from the interactive command shell: the file is opened
with whatever application (if any) its extension is associated.When another operation is given, it must be a “command verb” that specifies
what should be done with the file. Common verbs documented by Microsoft are
'print'
and'edit'
(to be used on files) as well as'explore'
and
'find'
(to be used on directories).startfile()
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 path parameter is relative to the current
directory. If you want to use an absolute path, make sure the first character
is not a slash ('/'
); the underlying Win32ShellExecute()
function
doesn’t work if it is. Use theos.path.normpath()
function to ensure that
the path is properly encoded for Win32.To reduce interpreter startup overhead, the Win32
ShellExecute()
function is not resolved until this function is first called. If the function
cannot be resolved,NotImplementedError
will be raised.Availability: Windows.
-
os.
system
(command)¶ -
Execute the command (a string) in a subshell. This is implemented by calling
the Standard C functionsystem()
, and has the same limitations.
Changes tosys.stdin
, etc. are not reflected in the environment of
the executed command. If command generates any output, it will be sent to
the interpreter standard output stream.On Unix, the return value is the exit status of the process encoded in the
format specified forwait()
. Note that POSIX does not specify the
meaning of the return value of the Csystem()
function, so the return
value of the Python function is system-dependent.On Windows, the return value is that returned by the system shell after
running command. The shell is given by the Windows environment variable
COMSPEC
: it is usually cmd.exe, which returns the exit
status of the command run; on systems using a non-native shell, consult your
shell documentation.The
subprocess
module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is preferable
to using this function. See the Replacing Older Functions with the subprocess Module section in
thesubprocess
documentation for some helpful recipes.Availability: Unix, Windows.
-
os.
times
()¶ -
Returns the current global process times.
The return value is an object with five attributes:user
— user timesystem
— system timechildren_user
— user time of all child processeschildren_system
— system time of all child processeselapsed
— elapsed real time since a fixed point in the past
For backwards compatibility, this object also behaves like a five-tuple
containinguser
,system
,children_user
,
children_system
, andelapsed
in that order.See the Unix manual page
times(2) or the corresponding Windows Platform API documentation.
On Windows, onlyuser
andsystem
are known; the other
attributes are zero.Availability: Unix, Windows.
Changed in version 3.3: Return type changed from a tuple to a tuple-like object
with named attributes.
-
os.
wait
()¶ -
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.Availability: Unix.
-
os.
waitid
(idtype, id, options)¶ -
Wait for the completion of one or more child processes.
idtype can beP_PID
,P_PGID
orP_ALL
.
id specifies the pid to wait on.
options is constructed from the ORing of one or more ofWEXITED
,
WSTOPPED
orWCONTINUED
and additionally may be ORed with
WNOHANG
orWNOWAIT
. The return value is an object
representing the data contained in thesiginfo_t
structure, namely:
si_pid
,si_uid
,si_signo
,si_status
,
si_code
orNone
ifWNOHANG
is specified and there are no
children in a waitable state.Availability: Unix.
New in version 3.3.
-
os.
P_PID
¶ -
os.
P_PGID
¶ -
os.
P_ALL
¶ -
These are the possible values for idtype in
waitid()
. They affect
how id is interpreted.Availability: Unix.
New in version 3.3.
-
os.
WEXITED
¶ -
os.
WSTOPPED
¶ -
os.
WNOWAIT
¶ -
Flags that can be used in options in
waitid()
that specify what
child signal to wait for.Availability: Unix.
New in version 3.3.
-
os.
CLD_EXITED
¶ -
os.
CLD_DUMPED
¶ -
os.
CLD_TRAPPED
¶ -
os.
CLD_CONTINUED
¶ -
These are the possible values for
si_code
in the result returned by
waitid()
.Availability: Unix.
New in version 3.3.
-
os.
waitpid
(pid, options)¶ -
The details of this function differ on Unix and Windows.
On Unix: Wait for completion of a child process given by process id pid, and
return a tuple containing its process id and exit status indication (encoded as
forwait()
). The semantics of the call are affected by the value of the
integer options, which should be0
for normal operation.If pid is greater than
0
,waitpid()
requests status information for
that specific process. If pid is0
, the request is for the status of any
child in the process group of the current process. If pid is-1
, the
request pertains to any child of the current process. If pid is less than
-1
, status is requested for any process in the process group-pid
(the
absolute value of pid).An
OSError
is raised with the value of errno when the syscall
returns -1.On Windows: Wait for completion of a process given by process handle pid, and
return a tuple containing pid, and its exit status shifted left by 8 bits
(shifting makes cross-platform use of the function easier). A pid less than or
equal to0
has no special meaning on Windows, and raises an exception. The
value of integer options has no effect. pid can refer to any process whose
id is known, not necessarily a child process. Thespawn*
functions called withP_NOWAIT
return suitable process handles.Changed in version 3.5: 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
InterruptedError
exception (see PEP 475 for the rationale).
-
os.
wait3
(options)¶ -
Similar to
waitpid()
, 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 toresource
.getrusage()
for details on resource usage information. The
option argument is the same as that provided towaitpid()
and
wait4()
.Availability: Unix.
-
os.
wait4
(pid, options)¶ -
Similar to
waitpid()
, except a 3-element tuple, containing the child’s
process id, exit status indication, and resource usage information is returned.
Refer toresource
.getrusage()
for details on
resource usage information. The arguments towait4()
are the same
as those provided towaitpid()
.Availability: Unix.
-
os.
WNOHANG
¶ -
The option for
waitpid()
to return immediately if no child process status
is available immediately. The function returns(0, 0)
in this case.Availability: Unix.
-
os.
WCONTINUED
¶ -
This option causes child processes to be reported if they have been continued
from a job control stop since their status was last reported.Availability: some Unix systems.
-
os.
WUNTRACED
¶ -
This option causes child processes to be reported if they have been stopped but
their current state has not been reported since they were stopped.Availability: Unix.
The following functions take a process status code as returned by
system()
, wait()
, or waitpid()
as a parameter. They may be
used to determine the disposition of a process.
-
os.
WCOREDUMP
(status)¶ -
Return
True
if a core dump was generated for the process, otherwise
returnFalse
.Availability: Unix.
-
os.
WIFCONTINUED
(status)¶ -
Return
True
if the process has been continued from a job control stop,
otherwise returnFalse
.Availability: Unix.
-
os.
WIFSTOPPED
(status)¶ -
Return
True
if the process has been stopped, otherwise return
False
.Availability: Unix.
-
os.
WIFSIGNALED
(status)¶ -
Return
True
if the process exited due to a signal, otherwise return
False
.Availability: Unix.
-
os.
WIFEXITED
(status)¶ -
Return
True
if the process exited using the exit(2) system call,
otherwise returnFalse
.Availability: Unix.
-
os.
WEXITSTATUS
(status)¶ -
If
WIFEXITED(status)
is true, return the integer parameter to the
exit(2) system call. Otherwise, the return value is meaningless.Availability: Unix.
-
os.
WSTOPSIG
(status)¶ -
Return the signal which caused the process to stop.
Availability: Unix.
-
os.
WTERMSIG
(status)¶ -
Return the signal which caused the process to exit.
Availability: Unix.
16.1.7. Interface to the scheduler¶
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.
New in version 3.3.
The following scheduling policies are exposed if they are supported by the
operating system.
-
os.
SCHED_OTHER
¶ -
The default scheduling policy.
-
os.
SCHED_BATCH
¶ -
Scheduling policy for CPU-intensive processes that tries to preserve
interactivity on the rest of the computer.
-
os.
SCHED_IDLE
¶ -
Scheduling policy for extremely low priority background tasks.
-
os.
SCHED_SPORADIC
¶ -
Scheduling policy for sporadic server programs.
-
os.
SCHED_FIFO
¶ -
A First In First Out scheduling policy.
-
os.
SCHED_RR
¶ -
A round-robin scheduling policy.
-
os.
SCHED_RESET_ON_FORK
¶ -
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.
-
class
os.
sched_param
(sched_priority)¶ -
This class represents tunable scheduling parameters used in
sched_setparam()
,sched_setscheduler()
, and
sched_getparam()
. It is immutable.At the moment, there is only one possible parameter:
-
sched_priority
¶ -
The scheduling priority for a scheduling policy.
-
-
os.
sched_get_priority_min
(policy)¶ -
Get the minimum priority value for policy. policy is one of the
scheduling policy constants above.
-
os.
sched_get_priority_max
(policy)¶ -
Get the maximum priority value for policy. policy is one of the
scheduling policy constants above.
-
os.
sched_setscheduler
(pid, policy, param)¶ -
Set the scheduling policy for the process with PID pid. A pid of 0 means
the calling process. policy is one of the scheduling policy constants
above. param is asched_param
instance.
-
os.
sched_getscheduler
(pid)¶ -
Return the scheduling policy for the process with PID pid. A pid of 0
means the calling process. The result is one of the scheduling policy
constants above.
-
os.
sched_setparam
(pid, param)¶ -
Set a scheduling parameters for the process with PID pid. A pid of 0 means
the calling process. param is asched_param
instance.
-
os.
sched_getparam
(pid)¶ -
Return the scheduling parameters as a
sched_param
instance for the
process with PID pid. A pid of 0 means the calling process.
-
os.
sched_rr_get_interval
(pid)¶ -
Return the round-robin quantum in seconds for the process with PID pid. A
pid of 0 means the calling process.
-
os.
sched_yield
()¶ -
Voluntarily relinquish the CPU.
-
os.
sched_setaffinity
(pid, mask)¶ -
Restrict the process with PID pid (or the current process if zero) to a
set of CPUs. mask is an iterable of integers representing the set of
CPUs to which the process should be restricted.
-
os.
sched_getaffinity
(pid)¶ -
Return the set of CPUs the process with PID pid (or the current process
if zero) is restricted to.
16.1.8. Miscellaneous System Information¶
-
os.
confstr
(name)¶ -
Return string-valued system configuration values. name 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
confstr_names
dictionary. For configuration variables not included in that
mapping, passing an integer for name is also accepted.If the configuration value specified by name isn’t defined,
None
is
returned.If name is a string and is not known,
ValueError
is raised. If a
specific value for name is not supported by the host system, even if it is
included inconfstr_names
, anOSError
is raised with
errno.EINVAL
for the error number.Availability: Unix.
-
os.
confstr_names
¶ -
Dictionary mapping names accepted by
confstr()
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.Availability: Unix.
-
os.
cpu_count
()¶ -
Return the number of CPUs in the system. Returns
None
if undetermined.This number is not equivalent to the number of CPUs the current process can
use. The number of usable CPUs can be obtained with
len(os.sched_getaffinity(0))
New in version 3.4.
-
os.
getloadavg
()¶ -
Return the number of processes in the system run queue averaged over the last
1, 5, and 15 minutes or raisesOSError
if the load average was
unobtainable.Availability: Unix.
-
os.
sysconf
(name)¶ -
Return integer-valued system configuration values. If the configuration value
specified by name isn’t defined,-1
is returned. The comments regarding
the name parameter forconfstr()
apply here as well; the dictionary that
provides information on the known names is given bysysconf_names
.Availability: Unix.
-
os.
sysconf_names
¶ -
Dictionary mapping names accepted by
sysconf()
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.Availability: Unix.
The following data values are used to support path manipulation operations. These
are defined for all platforms.
Higher-level operations on pathnames are defined in the os.path
module.
-
os.
curdir
¶ -
The constant string used by the operating system to refer to the current
directory. This is'.'
for Windows and POSIX. Also available via
os.path
.
-
os.
pardir
¶ -
The constant string used by the operating system to refer to the parent
directory. This is'..'
for Windows and POSIX. Also available via
os.path
.
-
os.
sep
¶ -
The character used by the operating system to separate pathname components.
This is'/'
for POSIX and'\'
for Windows. Note that knowing this
is not sufficient to be able to parse or concatenate pathnames — use
os.path.split()
andos.path.join()
— but it is occasionally
useful. Also available viaos.path
.
-
os.
altsep
¶ -
An alternative character used by the operating system to separate pathname
components, orNone
if only one separator character exists. This is set to
'/'
on Windows systems wheresep
is a backslash. Also available via
os.path
.
-
os.
extsep
¶ -
The character which separates the base filename from the extension; for example,
the'.'
inos.py
. Also available viaos.path
.
-
os.
pathsep
¶ -
The character conventionally used by the operating system to separate search
path components (as inPATH
), such as':'
for POSIX or';'
for
Windows. Also available viaos.path
.
-
os.
defpath
¶ -
The default search path used by
exec*p*
and
spawn*p*
if the environment doesn’t have a'PATH'
key. Also available viaos.path
.
-
os.
linesep
¶ -
The string used to separate (or, rather, terminate) lines on the current
platform. This may be a single character, such as'n'
for POSIX, or
multiple characters, for example,'rn'
for Windows. Do not use
os.linesep as a line terminator when writing files opened in text mode (the
default); use a single'n'
instead, on all platforms.
-
os.
devnull
¶ -
The file path of the null device. For example:
'/dev/null'
for
POSIX,'nul'
for Windows. Also available viaos.path
.
-
os.
RTLD_LAZY
¶ -
os.
RTLD_NOW
¶ -
os.
RTLD_GLOBAL
¶ -
os.
RTLD_LOCAL
¶ -
os.
RTLD_NODELETE
¶ -
os.
RTLD_NOLOAD
¶ -
os.
RTLD_DEEPBIND
¶ -
Flags for use with the
setdlopenflags()
and
getdlopenflags()
functions. See the Unix manual page
dlopen(3) for what the different flags mean.New in version 3.3.
16.1.9. Random numbers¶
-
os.
getrandom
(size, flags=0)¶ -
Get up to size random bytes. The function can return less bytes than
requested.These bytes can be used to seed user-space random number generators or for
cryptographic purposes.getrandom()
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/dev/random
and
/dev/urandom
devices.The flags argument is a bit mask that can contain zero or more of the
following values ORed together:os.GRND_RANDOM
and
GRND_NONBLOCK
.See also the Linux getrandom() manual page.
Availability: Linux 3.17 and newer.
New in version 3.6.
-
os.
urandom
(size)¶ -
Return a string of size random bytes suitable for cryptographic use.
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.On Linux, if the
getrandom()
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 PEP 524 for
the rationale. On Linux, thegetrandom()
function can be used to get
random bytes in non-blocking mode (using theGRND_NONBLOCK
flag) or
to poll until the system urandom entropy pool is initialized.On a Unix-like system, random bytes are read from the
/dev/urandom
device. If the/dev/urandom
device is not available or not readable, the
NotImplementedError
exception is raised.On Windows, it will use
CryptGenRandom()
.See also
The
secrets
module provides higher level functions. For an
easy-to-use interface to the random number generator provided by your
platform, please seerandom.SystemRandom
.Changed in version 3.6.0: On Linux,
getrandom()
is now used in blocking mode to increase the
security.Changed in version 3.5.2: On Linux, if the
getrandom()
syscall blocks (the urandom entropy pool
is not initialized yet), fall back on reading/dev/urandom
.Changed in version 3.5: On Linux 3.17 and newer, the
getrandom()
syscall is now used
when available. On OpenBSD 5.6 and newer, the Cgetentropy()
function is now used. These functions avoid the usage of an internal file
descriptor.
-
os.
GRND_NONBLOCK
¶ -
By default, when reading from
/dev/random
,getrandom()
blocks if
no random bytes are available, and when reading from/dev/urandom
, it blocks
if the entropy pool has not yet been initialized.If the
GRND_NONBLOCK
flag is set, thengetrandom()
does not
block in these cases, but instead immediately raisesBlockingIOError
.New in version 3.6.
-
os.
GRND_RANDOM
¶ -
If this bit is set, then random bytes are drawn from the
/dev/random
pool instead of the/dev/urandom
pool.New in version 3.6.
- Reference
- Python Functions
- Python os.kill() Method
- Syntax of the
os.kill()
Method - Example Codes: Working With the
os.kill()
Method in Python - Example Codes: Understanding the
os.kill()
Method in Python
Python os.kill()
method is an efficient way of killing or terminating a process using its process identifier (pid
).
Syntax of the os.kill()
Method
Parameters
pid |
It is an integer representing an ID of the process we are addressing. |
signal |
It is an integer representing a signal number needed to send to the process. |
Return
In the execution process, this method does not return any value.
Example Codes: Working With the os.kill()
Method in Python
import os, signal
pID = os.fork()
if pID :
print("---PARENT PROCESS---")
os.kill(pID, signal.SIGSTOP)
print("The parent process has been killed.")
os.kill(pID, signal.SIGCONT)
else :
print("Hello, World!")
Output:
---PARENT PROCESS---
The parent process has been killed.
Hello, World!
Python offers different signal modules. In the above code, variables signal.SIGSTOP
are used to stop the current process.
Another variable, signal.SIGCONT
, is used, which continues the current process.
Example Codes: Understanding the os.kill()
Method in Python
import os
import signal
def process():
print ("The process ID:", os.getpid())
os.kill(os.getpid(), signal.SIGKILL)
process()
Output:
Every time we run the above code, a new process executes; thus, different process IDs are obtained. In the above code, the variable signal.SIGKILL
is used to kill the current process.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
Related Article — Python OS
July 4, 2020May 26, 2021
When automating some tasks in Windows OS, you may wonder how to automatically close Windows process if you do not have the direct control of the running application or when the application is just running for too long time. In this article, I will be sharing with you how to close the Windows process with some python library, to be more specific, the pywin32 library.
Prerequisites
You will need to install the pywin32 library if you have not yet installed:
Find the process name from Windows Task Manager
You will need to first find out the application name which you intend to close, the application name can be found from the Windows task manager. E.g. If you expand the “Windows Command Processor” process, you can see the running process is “cmd.exe”.
Let’s get started with the code!
Import the below modules that we will be using later:
from win32com.client import GetObject from datetime import datetime import os
And we need to get the WMI (Windows Management Instrumentation) service via the below code, where we can further access the window processes. For more information about WMI, please check this.
WMI = GetObject('winmgmts:')
Next, we will use the WMI SQL query to get the processes from the Win32_Process table by passing in the application name. Remember we have already found the application name earlier from the task manager.
for p in WMI.ExecQuery('select * from Win32_Process where Name="cmd.exe"'): #the date format is something like this 20200613144903.166769+480 create_dt, *_ = p.CreationDate.split('.') diff = datetime.now() - datetime.strptime(create_dt,'%Y%m%d%H%M%S')
There are other properties such as Description, Status, Executable Path, etc. You can check the full list of the process properties from this win32-process documentation. Here we want to base on the creation date to calculate how much time the application has been running to determine if we want to kill it.
Assuming we need to close windows process after it is running for 5 minutes.
if diff.seconds/60 > 5: print("Terminating PID:", p.ProcessId) os.system("taskkill /pid "+str(p.ProcessId))
With this taskkill command, we will be able to terminate all the threads under this Windows process peacefully.
Conclusion
The pywin32 is super powerful python library especially when dealing with the Windows applications. You can use it to read & save attachments from outlook, send emails via outlook , open excel files and some more. Do have a check on these articles.
As per always, welcome any comments or questions.
Post tagspythonpywin32Win32_Process
You may also like
Sometimes you need to kill processes running on your machine, for example to clean up after an application has hung, or when you want to wipe out some background tasks in a hurry. Killing processes, particularly on Windows, can be a bit of a manual process. There are command line programs like taskill.exe, but it’s useful to be able to combine process killing with a programming language like Python which lets you easily add pattern matching to selectively pick the processes.
Let’s go through the steps of listing, matching, and killing processes running on the machine.
How to list processes
The first step is to list the processes that are running. This can be accomplished using the psutil Python library – a cross-platform library for process and system monitoring.
'''list running processes''' import psutil for proc in psutil.process_iter(): print(proc.name())
When you run this code on Windows, the list of process names will not always match the display names listed in Task Manager. You can see the process names in Task Manager when you right-click and select properties. This is important when it comes to matching strings to kill a process, and why listing the processes first is useful.
How to kill a process
Once you have selected a process the command to kill it using psutil looks like this:
proc.kill()
However, if you didn’t create the process in the first place, or it wasn’t created on your behalf, you won’t have permission to kill it, unless you’re running the code as “Administrator” or “superuser”. For example on Windows you could start a PowerShell window by right-clicking and selecting “Run as Administrator”, or on Linux run using sudo.
How to kill processes based on partial strings
In this example, a friend of mine who spends too much time gaming wants to run a game that is highly demanding of CPU and memory, so they want to kill the Adobe Creative Cloud processes that are running and don’t currently need, (but also want to leave it all installed so they can use the video editing when they need it).
After checking the process list, the process names to kill contain the strings: “Adobe”, “CCXProcess”, “CoreSync” and “Creative Cloud”. You could match the strings with a set of “or” statements, but another way to match a name from a list of strings in Python is to use the built-in function: any.
A simple Python 3 program to match and kill processes looks like this:
'''pykill.py - selective process kill prog''' import psutil def main(): '''Process kill function''' for proc in psutil.process_iter(): # check whether the process name matches # print(proc.name()) if any(procstr in proc.name() for procstr in ['Adobe', 'CCXProcess', 'CoreSync', 'Creative Cloud']): print(f'Killing {proc.name()}') proc.kill() if __name__ == "__main__": main()
Before running your own version of this and breaking your OS, it’s a good idea to test it. E.g. start something like notepad and replace the string list with [‘notepad’]. You wouldn’t need to run a notepad killer as Administrator (unless other people are also logged in and you wanted to kill their notepads).
Python kills the Windows process
tags: LQ-Other python windows Operation and maintenance
1 Batch kill the same process:
CMD = 'Taskkill / F / IM' + Process Name
os.system(cmd)
2 Kill a process by PID:
CMD = 'taskkill / f / pid' + PID value
os.system(cmd)
1 Query all PIDs:
list_process = list()
pid_list = psutil.pids()
for sub_pid in pid_list:
try:
process_info = psutil.Process(sub_pid)
IF process name == Process_info.name ():
list_process.append(sub_pid)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
Intelligent Recommendation
Python3 kills the background process in Windows
Python has a lightweight timing task scheduled library: Schedule. He can complete the timed task per minute, hourly, week, week, a specific date. Therefore, it is very convenient to implement some lig…
Windows kills the specified port process
1. Check the PID of the occupied port The result is as shown below The last digit is the process of occupying the port (here is 4364) 2. View the process name of the process number The result of the q…
Python kills its own process
Sometimes we need to interrupt the execution of the program, such as when the execution of the following code fails. At this time we can kill the process and re-execute the above code …
More Recommendation
Windows system kills the port number process
1, open the cmd window to execute the netstat -ano | findstr 80 command, you can specific to a port such as: 8069 The corresponding process number (PID) is 9684 2, tasklist | findstr 9684 can view the…