I have the following code, which is expected to do the following,
The motive is that once a file is identified (which satisfies a prefix), I want the absolute realpath of that file. I tried using the Path(libfile).resolve() option but its giving only the present working directory from where the script is run as against the realpath of that file. Could you explain where I am going wrong?
import os
from pathlib import Path
def directory(dirName, prefix):
process_dir = [name for name in os.listdir(dirName) if os.path.isdir(os.path.join(dirName, name))]
for entry in process_dir:
for dirname, directory, files in os.walk(os.path.join(dirName, entry)):
for libfile in files:
if libfile.startswith((prefix)):
return(Path(libfile).resolve())
Python version 3.6.2.
Change this line:
return(Path(libfile).resolve())
To this:
return (Path(dirname) / libfile).resolve()
I agree with the comment from dawg that using a recursive glob pattern would be a better choice here.