Problem
To get the Firefox default profile, I am using the following program.
import glob
from pathlib import Path
import re
x = glob.glob(str(Path.home()) + '/.mozilla/firefox/*/')
r = re.compile(".*default-release.*")
firefox_default_profile = list(filter(r.match, x))[0]
print(firefox_default_profile)
The logic is simple. The path looks like ~/.mozilla/firefox/5ierwkox.default-release
.
In the whole path the only variable is the 5ierwkox
. It varies from person to person.
The program works fine, but I am wondering if I have over-complicated the code or not.
Solution
- You don’t need to import
glob
;pathlib.Path
has aPath.glob
method. - You don’t need to import
re
; you can merge the regex into the glob –.*default-release.*
become*default-release*
. - You can use
next(...)
rather thanlist(...)[0]
. - By passing
None
as a second argument tonext
we can prevent the code from erroring if the user doesn’t have a Firefox profile.
import pathlib
profiles = pathlib.Path.home().glob(".mozilla/firefox/*default-release*/")
firefox_default_profile = next(profiles, None)
print(firefox_default_profile)
None of my profiles have default-release
in the name.
To find the default profile, we need to parse ~/.mozilla/firefox/profiles.ini
and find the section with Default=1
set.