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")
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.