Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to Access Linux Paths

Tags:

python

linux

path

Using Python, how does one parse/access files with Linux-specific features, like "~/.mozilla/firefox/*.default"? I've tried this, but it doesn't work.

Thanks

like image 375
Jonah Avatar asked May 22 '26 15:05

Jonah


2 Answers

This

import glob, os
glob.glob(os.path.expanduser('~/.mozilla/firefox/*.default'))

will give you a list of all files ending in ".default" in the current user's ~/.mozilla/firefox directory using os.path.expanduser to expand the ~ in the path and glob.glob to match the *.default file pattern.

like image 157
Will McCutchen Avatar answered May 24 '26 05:05

Will McCutchen


~ is expanded by the shell and not a real path. As such you have to navigate there manually.

import os

homeDir = os.environ['HOME']
f = open( homeDir + '/.mozilla/firefox/*.default' )
# ...
like image 27
poke Avatar answered May 24 '26 04:05

poke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!