python backup script

Peter Otten __peter__ at web.de
Tue May 7 02:18:11 EDT 2013


Chris Angelico wrote:

> It's also worth noting that the ~/ notation is a shell feature. You
> may or may not be able to use it in config.read().

The latter. Combined with

"""
read(self, filenames) method of ConfigParser.ConfigParser instance
    Read and parse a filename or a list of filenames.
    
    Files that cannot be opened are silently ignored; this is
...
"""

you'll get no configuration at all. You have to apply

os.path.expanduser()

to get a valid path.

$ cd
$ touch tmp.config
$ cd /
$ python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ConfigParser import ConfigParser
>>> p = ConfigParser()
>>> p.read("~/tmp.config")
[]
>>> import os
>>> p.read(os.path.expanduser("~/tmp.config"))
['/home/peter/tmp.config']





More information about the Python-list mailing list