[Tutor] inserting path to open a file from a variable

Brandon McCaig bamccaig at gmail.com
Fri May 29 17:27:27 CEST 2015


Richard:

On Thu, May 28, 2015 at 1:39 PM, richard kappler <richkappler at gmail.com> wrote:
> # line two is the absolute path to the log you are parsing data from
> # keep 'rdfile:' as is, path starts after it, no spaces
> rdfile:Documents/MyScripts/fileMonitor/log.txt
> # line 4 is the absolute path to the log you are appending the parsed data
> too
> # keep 'wrtfile:' as is, path starts after it, no spaces
> wrtfile:Documents/MyScripts/fileMonitor/newlog.txt

Your problem traces all the way back to here. The comments describe
the configuration file as expecting absolute paths, but you have only
entered relative paths (absolute paths need to be "rooted"; in *nix
that typically means a prefix of /). A good option then is to validate
your configuration file when you read it in.

> # read the config file to get file locations for a script
> conf = open('fileMonitor.conf', 'r')
> read_it = conf.read()
>
> for line in read_it.splitlines():
>     if line.startswith('rdfile:'):
>         rd = line
>     elif line.startswith('wrtfile:'):
>         wrt = line
>
> rd1 = rd.replace('rdfile:',"",1)
> wrt1 = wrt.replace('wrtfile:',"",1)

import os.path
import sys

for x in [(rd1, 'Source file', 'rdfile'),
          (wrt1, 'Destination file', 'wrtfile')]:
    if not os.path.isabs(x[0]):
        print >> sys.stderr, \
                'Configuration error:', \
                '%s path (%s) must be absolute!' % \
                        x[1:]
        sys.exit(1)

Regards,


-- 
Brandon McCaig <bamccaig at gmail.com> <bamccaig at castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


More information about the Tutor mailing list