Finding files

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 which is present on the PATH.

Syntax: 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 and 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

Python3




# importing shutil module 
import shutil 
   
# file search 
cmd = 'anaconda'
   
# Using shutil.which() method
locate = shutil.which(cmd)
   
# Print result
print(locate)


Output:

D:\Installation_bulk\Scripts\anaconda.EXE


Shutil Module in Python

Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.

Similar Reads

Copying Files to another directory

shutil.copy() method in Python is used to copy the content of the source file to the destination file or directory. It also preserves the file’s permission mode but other metadata of the file like the file’s creation and modification times is not preserved.The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied into the destination using the base filename from the source. Also, the destination must be writable. If the destination is a file and already exists then it will be replaced with the source file otherwise a new file will be created....

Copying the Metadata along with File

...

Copying the content of one file to another

...

Replicating complete Directory

shutil.copy2() method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy() method but it also tries to preserve the file’s metadata....

Removing a Directory

...

Finding files

...