0

I know you can use the module os for finding out if a file (or directory, w/e) exists within a certain path, e.g.

import os
os.chdir("C:\\Users\\Casper\\Documents")
os.path.exists("file_name.png")

and if file_name.png exists, it will return True. However, how do I check if the file exists just anywhere on the computer. This could be helpful for apps compiled to .exe files that others may use, where the app searches for this one file instead of going into directories and such.

Cazo
  • 151
  • 7
  • 1
    I don't see how that's helpful. The file should surely be in a predictable location? Scanning the entire system for a file seems excessive, not to mention the fact that files can share a name but be in different locations, doing different things – roganjosh Sep 15 '18 at 14:02
  • 3
    If you don't know where it is - you have to search *all directories*... (or a subset of ones that makes sense) – Jon Clements Sep 15 '18 at 14:02
  • If you're looking for executable files it might make sense to focus on your system's `PATH` environment variable. – ChrisGPT was on strike Sep 15 '18 at 14:05
  • The reason I am asking is because I want to create an exe file for my friend which creates LaTeX documents for him. But for this app, made in tkinter, I want the window icon to be a TeX-icon and not the default feather icon that tkinter offers as default. Therefore, in the program I could set the TeX-icon as the window icon on my app, and give my friend the app and imagefile to download. Since I do not know where he will put the imagefile on his computer, I find it helpful to just make the program search for the file in all directories. How do I accomplish this? – Cazo Sep 15 '18 at 14:06
  • 1
    You want the os.walk method to search the file. I think you'll find what you're looking for here: https://stackoverflow.com/questions/1724693/find-a-file-in-python – nocturnal_beast Sep 15 '18 at 14:41
  • I have seen that, and it needs a path, but I want the program to have no path but to look at the computer itself. Maybe I am looking for a path that looks at every directory in the computer. I just don't know what that path is. – Cazo Sep 15 '18 at 14:50
  • The concept of "the computer itself" I think you're looking for is the root directory, `C:\` on your OS, though I don't recommend crawling through all subdirectories of it. – Quinn Mortimer Sep 15 '18 at 15:15

0 Answers0