0

I have a file on my computer that I would like to locate the full path of.

The only way I can think of to find the path of that file is to use os.walk until I eventually find the file. This method can take a long time, as there are many many files.

The file is User Data, here is basically the sub path I am looking for \Google\Chrome\User Data.

Is there any faster way to get the full path of the file?

EDIT: The file is generally found under \AppData\Local\. If there is a shortcut to get to this path, that would suffice.

cccnrc
  • 1,195
  • 11
  • 27
Bbolt
  • 37
  • 7
  • Do you know the relative position of the file? If not, do you mean to simply search for the location of a particular file on your computer without knowing in advance? I don't think there's any faster way to do this than simply "`os.walk` and check literally everything" - or to invoke an equivalent shell command like `find` on Unix or `GetChildItem` on Windows (see [this question](https://stackoverflow.com/questions/762816/get-powershell-to-display-all-paths-where-a-certain-file-can-be-found-on-a-drive)) – Green Cloak Guy Aug 06 '19 at 16:42
  • Do you have any information about the file other than its name? Eg is it in the current working directory or do you have an open handle for it, or is it an imported module? Or are you just trying to search the whole disk for a file with that name? – Matthias Fripp Aug 06 '19 at 16:42
  • @GreenCloakGuy in general, the sub path is located under "\AppData\Local\". But yes, I want to find a file without knowing its location in advance. It seems like os.walk is still the best option. – Bbolt Aug 06 '19 at 18:24
  • @MatthiasFripp I have no information on the file other than it generally being under "\AppData\Local\". The full path for me looks like this "C:\Users\owner\AppData\Local\Google\Chrome\User Data" – Bbolt Aug 06 '19 at 18:26
  • If 'C:\Users\owner' refers to the current user's home directory, then you can first find that, then append the rest of the path. See my answer for how to do that. – Matthias Fripp Aug 06 '19 at 19:33

1 Answers1

0

As noted here, you can get there via something like this:

import os
home_dir = os.path.expanduser('~')
file_path = os.path.join(
    home_dir, 'AppData', 'Local', 
    'Google', 'Chrome', 'User Data'
)
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45