0

I would like to search and locate a file path :: that may be in a different place on each user's machine. Is there a way to implement a search for the file, without pre defining path ?? A way that I can pass the file's name .. All required path, but each user's path is different ?? I attached a pre code::

Any help please

import os 
import os.path 
path = os.getcwd()
xy=os.path.dirname(path)
name = "NAMEOFFILEIAMSEARCHING.exe"

for root, dirs, files in os.walk(xy):
    if name in files:
         location=os.path.join(root)
print(location )
msci
  • 89
  • 6
  • You'll need different approaches for Unix and Windows systems. On Unix, you would have to start your search from the root directory but that could take a **very** long time if you don't take care of linked (hard or symbolic) directories. On Windows, you'll need to find out what drives exist. Your example shows the start from your current working directory but is that going to fulfil the requirement? I doubt it. –  Nov 29 '21 at 16:42
  • @DarkKnight I am working with windows, Any suggestion, how to do that in windows ?? Thanks, – msci Nov 29 '21 at 16:49
  • Take a look at [this](https://stackoverflow.com/questions/827371/is-there-a-way-to-list-all-the-available-windows-drives) –  Nov 29 '21 at 16:57
  • @DarkKnight Thank you for your comment, However, it's lists the directories, but not linked with searching or looking for a specific file Can not I use os.walk ?? – msci Nov 29 '21 at 17:11
  • Yes you can use os.walk(). The link I gave you explains how you can figure out what drive letters exist on your Windows system –  Nov 29 '21 at 17:19

0 Answers0