'Python: How to Access Linux Paths

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



Solution 1:[1]

~ 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' )
# ...

Solution 2:[2]

It's important to remember:

  • use of the tilde ~ expands the home directory as per Poke's answer
  • use of the forward slash / is the separator for linux / *nix directories
  • by default, *nix systems such as linux for example has a wild card globbing in the shell, for instance echo *.* will return back all files that match the asterisk dot asterisk (as per Will McCutcheon's answer!)

Solution 3:[3]

http://docs.python.org/library/os.html Gives a complete reference if you would like to change directory or give paths.

You can for example give relative paths and access specific files.

If you would like to execute commands then http://docs.python.org/library/commands.html provides nice wrappers for the os.popen() function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 poke
Solution 2
Solution 3 anijhaw