0
votes

I know the name of the file stored on my pc which I want to open in a web page application. I need the to send the filepath to a html file for the same. So I want to get filepath using the filename in Python or HTML.

I tried using the os.path.dirname(filename) in Python but it didn't work because the attribute is supposed to be a path for this function.

1
unclear what you are asking or what you triedAlex
So... Do you want a script to magically find a full path using only filename? Or are you in the directory with that file but can't get the full filepath?h4z3
use python to write script which will check all filename in all folders. You can use for root, dirs, files in os.walk("/"): for this.furas
on Linux is program locate filename which use database to keep "path/name" of all files and you can very fast find all files with this name (mostly there are more than one paht with this name)` You can use SQLite in Python to do the same.furas

1 Answers

1
votes
  • use os.path.abspath to get a normalized absolutized version of the pathname
  • use os.walk to get it's location

code :

import os
file_ = 'filename.txt'

for root, dirs, files in os.walk('C:/'):
    for name in files:
        if file_ in name:
            print (os.path.abspath(os.path.join(root, name)))

output:

C:\Users\User\Desktop\filename.txt