python - Better debugging: expected a character buffer object -
i trying pass string section the python function below uncertain why seeing error. understanding of is not getting string, expected. have tried casting, not working either. how can solve or more debug info?
section = str('[log]') some_var = 'filename =' edit_ini('./bench_config.ini', section, some_var, 'logs/ops_log_1')
the function causing error
def edit_ini(filename, section, some_var, value): section = false flist = open(filename, 'r').readlines() f = open(filename+'test', 'w') line in flist: line = str(line) print line if line.startswith(section): section = true if( section == true ): if( line.startswith(some_var) ): modified = "%s = $s", variable, value print >> f, modified section = false else: print >> f, line f.close()
however see error:
traceback (most recent call last): file "bench.py", line 89, in <module> edit_ini('./config.ini', section, some_var, 'logs/log_1') file "bench.py", line 68, in edit_ini if line.startswith(section): typeerror: expected character buffer object
you overwrite passed-in section
section=false
. error because cannot call string.startswith( false )
.
a way debug python use pdb. have helped find problem here. should read spec, heres quick guide on how use pdb.
import pdb # code ... pdb.set_trace() # put right before line.startswith(section)
then when run code, execute right before failure. can print section
in pdb, , see false
, , try figure out why false
.
Comments
Post a Comment