I am trying to pass a string section to the the python function below it I am uncertain why I am seeing this error. My understanding of this is that it is not getting a string, where it is expected. I have tried casting, but that is not working either. How can I solve this or get more debug info?
section = str('[log]')
some_var = 'filename ='
edit_ini('./bench_config.ini', section, some_var, 'logs/ops_log_1')
The function causing the error
def edit_ini(filename, section, some_var, value):
section = False
flist = open(filename, 'r').readlines()
f = open(filename+'test', 'w')
for 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 I see the 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 a character buffer object
You overwrite the passed-in section
with section=False
. The error is because you cannot call string.startswith( False )
.
A way to debug python is to use pdb . This would have helped you find your problem here. You should read the spec, but heres a quick guide on how to use pdb.
import pdb
# your code ...
pdb.set_trace() # put this right before line.startswith(section)
Then when you run your code, you will execute up to right before the failure. Then you can print section
in pdb, and see that it is False
, and then try to figure out why it is False
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.