473,748 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error in ConfigParser

Hello everyone:

I came across the module ConfigParser and can use it correctly.

import ConfigParser
fp = open('test.cfg' ,'w+')
config = ConfigParser.Co nfigParser()
config.readfp(f p)
config.add_sect ion('test')
config.set('tes t', 'haha', 'hehe')
print config.sections ()
config.write(fp )

['test']
Traceback (most recent call last):
File "configparser.p y", line 8, in ?
config.write(fp )
File "C:\Python24\li b\ConfigParser. py", line 369, in write
fp.write("[%s]\n" % section)
IOError: (0, 'Error')

I trace into the module and when executing the line "fp.write("[%s]\n"
% section)", I find every parameter is correct: fp is still a open file
object and section is 'test', I may miss something, but I just cannot
figure it out. Can someone tell me what's going wrong here?

Thanks

Jun 28 '06 #1
1 2018
pipehappy wrote:
Hello everyone:

I came across the module ConfigParser and can use it correctly.

import ConfigParser
fp = open('test.cfg' ,'w+')
config = ConfigParser.Co nfigParser()
config.readfp(f p)
config.add_sect ion('test')
config.set('tes t', 'haha', 'hehe')
print config.sections ()
config.write(fp )

['test']
Traceback (most recent call last):
File "configparser.p y", line 8, in ?
config.write(fp )
File "C:\Python24\li b\ConfigParser. py", line 369, in write
fp.write("[%s]\n" % section)
IOError: (0, 'Error')

I trace into the module and when executing the line "fp.write("[%s]\n"
% section)", I find every parameter is correct: fp is still a open file
object and section is 'test', I may miss something, but I just cannot
figure it out. Can someone tell me what's going wrong here?

What is the exact intent of your code? Note that the "w+" mode is
documented as truncating the file, which means that your starting
configuration will always be null. My first thoughts were therefore
related to file position,

For what it's worth, however, note that on my system (Cygwin 2.4.3) this
code only breaks the *second* time:
import ConfigParser
fp = open('test.cfg' ,'w+')
config = ConfigParser.Co nfigParser()
config.readfp(f p)
config.add_sect ion('test')
config.set('tes t', 'haha', 'hehe')
print config.sections () ['test'] config.write(fp )
### should really have closed fp here?
import ConfigParser
fp = open('test.cfg' ,'w+')
config = ConfigParser.Co nfigParser()
config.readfp(f p)
config.add_sect ion('test') Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/ConfigParser.py ", line 226, in add_section
raise DuplicateSectio nError(section)
ConfigParser.Du plicateSectionE rror: Section 'test' already exists config.set('tes t', 'haha', 'hehe')
print config.sections () ['test'] config.write(fp )


Adding a seel to the start of the file before the failing statement
seems to fix the problem, so I guess my conjecture was right. Try

import ConfigParser
fp = open('test.cfg' ,'w+')
config = ConfigParser.Co nfigParser()
config.readfp(f p)
config.add_sect ion('test')
config.set('tes t', 'haha', 'hehe')
print config.sections ()
fp.seek(0)
config.write(fp )
fp.close()

But then fix that "w+" if this is more than just a test app!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2623
by: Greg Krohn | last post by:
I'm trying to subclass ConfigParser so I can use a custom __read method (the custom format doesn't conform to RFC 822) when needed. Needless to say, it's not working as expected. In the following code, I bind __read_ini to __read and override __read so it can choose between __read_ini and __read_custom. But it seems that __read_custom never gets called when I expect it to aways be called. I have a feeling this has to do with me not...
1
1824
by: Martin Maney | last post by:
Summary: with batteries like these, get a longer extension cord? <transcript from interactive session> Python 2.2.1 (#1, Sep 7 2002, 14:34:30) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from ConfigParser import ConfigParser >>> cp = ConfigParser()
2
4302
by: Roy H. Berger | last post by:
If I want to subclass ConfigParser and changed the optionxform method to not return things in lower case wouldn't I just need the following code in my subclasss module? from ConfigParser import * class MyConfigParser(ConfigParser): def __init__(self, defaults=None): ConfigParser.__init__(self, defaults)
6
6784
by: Matthew Barnes | last post by:
I'm considering submitting a patch for Python 2.4 to allow environment variable expansion in ConfigParser files. The use cases for this should be obvious. I'd like to be able to specify something like the following in a configuration file: data_file=${HOME}/mydata.dat ....(where HOME=/home/matt) and have ConfigParser automatically expand it to:
11
4902
by: Manlio Perillo | last post by:
Regards. Since sections in CongiParser files are delimited by , why there is not an escape (and unescape) function for escaping &, characters to &amp;, &lbrack; and &rbrack; ? Thanks Manlio Perillo
2
1700
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons, I'd rather use a syntax that ConfigParser can't handle. I know I can maintain two separate configuration files, and if I have to I will, but I'd rather avoid that, if possible, and a solution that suits my purposes is quite straightforward. I...
10
11850
by: Terry Carroll | last post by:
It looks like ConfigParser will accept a list to be writing to the *.ini file; but when reading it back in, it treats it as a string. Example: ############################### import ConfigParser def whatzit(thingname, thing): print thingname, "value:", thing print thingname, "length:", len(thing)
4
1873
by: Danil Dotsenko | last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's SuperKaramba widget. (http://www.kde-look.org/content/show.php?content=32185) I was using 2.4.x python docs as reference and ConfigParser.read('non-existent-filename') returns in 2.4.x One user with 2.3.x reported an error stemming from my use of len(cfgObject.read('potentially-non-existent-filename'))
4
14267
by: Phoe6 | last post by:
Hi, I have a configfile, in fact, I am providing a configfile in the format: Name: Foo Author: Bar Testcases: tct123
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.