Python | shutil.which() method Last Updated : 12 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps to automate the process of copying and removal of files and directories. shutil.which() method tells the path to an executable application that would be run if the given cmd was called. This method can be used to find a file on a computer that is present on the PATH. shutil.which() Method Syntax in PythonSyntax: shutil.which(cmd, mode = os.F_OK | os.X_OK, path = None) Parameters: cmd: A string representing the file. mode: This parameter specifies mode by which method should execute. os.F_OK tests existence of the path .os.X_OK Checks if path can be executed or we can say mode determines if the file exists and executable. path: This parameter specifies the path to be used, if no path is specified then the results of os.environ() are used Return Value: This method returns the path to an executable application Python shutil.which() method Example Below are some examples of shutil.which() function of Shutil module by which we can check if a file is executable using os.X_OK and in Python: Get Python Location Using shutil.which() MethodIn this example, the code employs shutil.which() to locate the full path of the 'python' executable in the system. The result is printed, showing the executable's path or None if not found. Python3 import os import shutil # cmd cmd = 'python' # Using shutil.which() method locate = shutil.which(cmd) print(locate) Output: /usr/bin/pythonCheck Path is Executable Using shutil.which()In this example, the function is_executable() takes a file path as an argument, uses shutil.which() to get the executable path, and then checks if the path is not None and is executable using os.access() with the os.X_OK flag. If the file is executable, the function returns True; otherwise, it returns False. Python3 import os import shutil def is_executable(file_path): # Using shutil.which() to get the executable path executable_path = shutil.which(file_path) # Check if the executable path is not None and is executable if executable_path and os.access(executable_path, os.X_OK): return True else: return False # Example usage file_to_check = "python" if is_executable(file_to_check): print(f"{file_to_check} is executable.") else: print(f"{file_to_check} is not executable or not found in the PATH.") Output: python is executable. Comment More infoAdvertise with us Next Article Python | shutil.which() method R Rajnis09 Follow Improve Article Tags : Python Python-shutil Practice Tags : python Similar Reads Python | shutil.move() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating the process of copying and removing files and directories. The shutil.move() method moves a file or directory 2 min read Python | shutil.copy() method Python3 # Python program to explain shutil.copy() method # importing shutil module import shutil # Source path source = "/home/User/Documents/file.txt" # Destination path destination = "/home/User/Documents/file.txt" # Copy the content of # source to destination try: shutil.copy( 5 min read Python | shutil.chown() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of chowning and removal of files and directories.shutil.chown() method in Python is used to change the 3 min read Python | shutil.copy2() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copy2() method in Python is used to copy the co 4 min read Python | shutil.disk_usage() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories. shutil.disk_usage() method tells the disk usage stati 2 min read Python | shutil.disk_usage() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories. shutil.disk_usage() method in Python is to get disk u 2 min read Python | shutil.copyfile() method Shutil module in Python helps automate the process of copying and removing files and directories. It comes under Pythonâs standard utility modules. Shutil(short for shell utility) module also provides many functions of high-level operations on files and collections of files. What is Shutil.copyfile 4 min read Python | shutil.copystat() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copystat() method in Python is used to copy the 3 min read Python | shutil.copymode() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating the process of copying and removal of files and directories.shutil.copymode() method in Python is used to copy 2 min read Python | shutil.unpack_archive() method Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Pythonâs standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.unpack_archive() method in Python is used to un 2 min read Like