I was wondering if anyone can explain to me why I keep getting this error:
Traceback (most recent call last):
File "C:/Documents and Settings/William/Desktop/test.py", line 4, in <module>
value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
File "C:\Python25\lib\INIfile.py", line 8, in getinifile
if len(Value) == 0:
AttributeError: 'str' object has no attribute 'len'
Here is the code of the two files:
INIfile.py: - from ConfigParser import ConfigParser
-
class INI:
-
def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
-
p = ConfigParser()
-
p.read(FILE)
-
Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
-
if len(Value) == 0:
-
Value = DEFAULT
-
return Value
test.py - import INIfile
-
-
INI = INIfile.INI()
-
value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
-
print value
I just don't see what I'm doing wrong.
3 1817 bvdet 2,851
Expert Mod 2GB
I was wondering if anyone can explain to me why I keep getting this error:
Here is the code of the two files:
INIfile.py: - from ConfigParser import ConfigParser
-
class INI:
-
def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
-
p = ConfigParser()
-
p.read(FILE)
-
Value = p.get ( SECTION, ITEM ).lstrip().rstrip()
-
if len(Value) == 0:
-
Value = DEFAULT
-
return Value
test.py - import INIfile
-
-
INI = INIfile.INI()
-
value = INI.getinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption', 'Default Value' )
-
print value
I just don't see what I'm doing wrong.
William,
It looks like it should work. After 'p.read(FILE)', add these to see what data is available: - print p.sections()
-
print p.items(SECTION)
Add 'print Value' after Value is assigned to see what the object is.
Comment: str.strip() does the same thing as str.lstrip().rstrip().
Okay, It actually worked great after restarting IDLE, I guess a bad version of my script got caught in IDLE. Thanks.
Also, thanks for that last part. :)
I also had to change it a bit for the default value to work, but I got it working nicely.
Hmm...Now I am having a similar problem with the other function I need to finish the script, But restarting doesn't seem to help.
Traceback (most recent call last):
File "C:\Documents and Settings\William\Desktop\test.py", line 3, in <module>
INI.writeinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
File "C:\Python25\lib\INIfile.py", line 28, in writeinifile
p.write(FILE)
File "C:\Python25\lib\ConfigParser.py", line 369, in write
fp.write("[%s]\n" % section)
AttributeError: 'str' object has no attribute 'write'
>>>
test.py - import INIfile
-
INI = INIfile.INI()
-
INI.writeinifile ( 'P:/INI/test.ini', 'Testsection', 'Testoption2', 'koo koo ka choo' )
INIfile.py - from ConfigParser import ConfigParser
-
-
class INI:
-
def getinifile ( self, FILE, SECTION, ITEM, DEFAULT ):
-
p = ConfigParser()
-
p.read(FILE)
-
if p.has_section ( SECTION ):
-
print 'Has Section'
-
if p.has_option ( SECTION, ITEM ):
-
print 'Has Option'
-
Value = p.get ( SECTION, ITEM ).strip()
-
else:
-
Value = DEFAULT
-
else:
-
Value = DEFAULT
-
return Value
-
def writeinifile ( self, FILE, SECTION, ITEM, VALUE ):
-
p = ConfigParser()
-
p.read(FILE)
-
if p.has_section(SECTION):
-
pass
-
else:
-
p.add_section(SECTION)
-
if p.has_option(SECTION, ITEM):
-
pass
-
else:
-
p.set(SECTION, ITEM, VALUE)
-
p.write(FILE)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: qqcq6s59 |
last post by:
Hi all
I am a newbie and I just saw a ongoing thread on Fileprocessing which
talks abt config parser.
I have writen many pyhton program to parse many kind of text files by
using string module and...
|
by: n3crius |
last post by:
hi,
i just got a web host with asp.net , seemed really cool. aspx with the
c# or vb IN the actual main page run fine, but when i use codebehind and
make another source file ( a .cs) to go with...
|
by: Matt |
last post by:
The ASP.NET application connects to MS-SQL database, and I already disabled
Anonymous access in IIS, and add ASPNET user in it. When I run the
application, it throws the following error on windows...
|
by: tomerfiliba |
last post by:
hey
i've been seeing lots of config-file-readers for python. be it
ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the
like. seems like a trend to me.
i came to this conclusion...
|
by: Ryan |
last post by:
I've created a custom configuration section that inherits (naturally) from
System.Configuration.ConfigurationSection. The configuration section is
working 99% fine, however I keep coming across a...
|
by: =?Utf-8?B?YzY3NjIyOA==?= |
last post by:
Hi all,
I cut and paste the following code from msdn help page which it just
introduces view and multiview server controls.
Here is what I do:
in vs studio 2005, File --New Web Site, it...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |