-1

How do I find the path of a file? I know how to find the file but what about the path?

from PIL import Image
user_path = "/Users/" + getpass.getuser())

for folder, sub_folder, files in os.walk(user_path):
    for sub_fold in sub_folder:
        for f in files:
            if FileName == f:
                print("file found")
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
richmail
  • 11
  • 6
  • if you have the name of the file, you can just concatenate the directory path to the file, look at this [question](https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory) – Aven Desta Jan 06 '21 at 00:21
  • 1
    Does this answer your question? [How can I find path to given file?](https://stackoverflow.com/questions/1124810/how-can-i-find-path-to-given-file) – Sekomer Jan 06 '21 at 00:22

1 Answers1

-1

os.walk yields a 3-tuple (dirpath, dirnames, filenames), where dirpath is the path of the current directory, meaning that you can just join with the filename:

from PIL import Image
user_path = "/Users/" + getpass.getuser())
for folder, sub_folders, files in os.walk(user_path):
    for f in files:
        if FileName == f:
            print("file found", os.path.join(folder,f))
SuperStormer
  • 4,997
  • 5
  • 25
  • 35