473,856 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using ConfigParser wrong?

I have a file test.cfg:

[options]
option1=foo
option2=bar
I have a program testconfig.py:

import ConfigParser

config = ConfigParser.Co nfigParser()
f = open('test.cfg' )
config.readfp(f )
configList = config.items('o ptions')
print configList
config.set('opt ions', 'option3', 'baz')
config.write(f)
When I run this program, I get an error:

Traceback (most recent call last):
File "./testconfig.py", line 11, in ?
config.write(f)
File "/usr/lib/python2.3/ConfigParser.py ", line 363, in write
fp.write("[%s]\n" % section)
IOError: [Errno 9] Bad file descriptor
Is .write() not right? If not, how do I save the changed config to file?
Jul 18 '05 #1
1 1638
NM, I am an idiot. Need to open the file in write mode.

David Rysdam wrote:
I have a file test.cfg:

[options]
option1=foo
option2=bar
I have a program testconfig.py:

import ConfigParser

config = ConfigParser.Co nfigParser()
f = open('test.cfg' )
config.readfp(f )
configList = config.items('o ptions')
print configList
config.set('opt ions', 'option3', 'baz')
config.write(f)
When I run this program, I get an error:

Traceback (most recent call last):
File "./testconfig.py", line 11, in ?
config.write(f)
File "/usr/lib/python2.3/ConfigParser.py ", line 363, in write
fp.write("[%s]\n" % section)
IOError: [Errno 9] Bad file descriptor
Is .write() not right? If not, how do I save the changed config to file?

Jul 18 '05 #2

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

Similar topics

2
4308
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)
11
4911
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 &, [ and ] ? Thanks Manlio Perillo
2
1705
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...
1
3591
by: Nexu | last post by:
Hello, I'm not sure exactly what i'm doing wrong here. I asked around on IRC and i was told the code is correct. The purpose of Settings() is that whenever Settings() or any of its methods are called. It should pick up the latest settings from file instead of returning what was in the buffer. This allow other scripts to change the value and my program and pick these changes up. Everything else should work exact same as ConfigParser()....
1
2026
by: pipehappy | last post by:
Hello everyone: I came across the module ConfigParser and can use it correctly. import ConfigParser fp = open('test.cfg','w+') config = ConfigParser.ConfigParser() config.readfp(fp) config.add_section('test') config.set('test', 'haha', 'hehe')
7
1940
by: Pupeno | last post by:
Hello, I have a class called MyConfig, it is based on Python's ConfigParser.ConfigParser. It implements add_section(self, section), which is also implemented on ConfigParser.ConfigParser, which I want to call. So, reducing the problem to the bare minimum, the class (with a useless add_section that shows the problem): .... def add_section(self, section): .... super(MyConfig, self).add_section(section)
0
1581
by: Phoe6 | last post by:
Hi All, I am able to use urlib2 through proxy. I give proxy credentials and use # Set the Proxy Address proxy_ip = "10.0.1.1:80" proxy_user = 'senthil_or' proxy_password_orig='password'
6
15194
by: William Manley | last post by:
According to what I read in the ConfigParser module, this should work, but it doesn't. Can anyone point out what I'm doing wrong? def writeinifile ( self, FILE='P:/INI/test.ini', SECTION='Testsection', OPTION='Item', VALUE='wheeee' ): p = ConfigParser() p.read(FILE) if not p.has_section ( SECTION ): p.add_section(SECTION) p.set(SECTION, OPTION, VALUE) p.write(FILE)
4
14315
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
9766
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
11065
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10797
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
10391
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
7098
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
5763
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
4583
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
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
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.