Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1048105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:24:19+00:00 2026-05-16T16:24:19+00:00

I am using ConfigParser to save simple settings to a .ini file, and one

  • 0

I am using ConfigParser to save simple settings to a .ini file, and one of these settings is a directory. Whenever I replace a directory string such as D:/Documents/Data, with a shorter directory string such as D:/, the remaining characters are placed two lines under the option. So the .ini file now looks like this:

[Settings]
directory = D:/

Documents/Data

What am I doing wrong? Here is my code:

import ConfigParser

class Settings():
    self.config = ConfigParser.ConfigParser()

    def SetDirectory(self, dir): #dir is the directory string
        self.config.readfp(open('settings.ini'))
        self.config.set('Settings', 'directory', dir)
        with open('settings.ini', 'r+') as configfile: self.config.write(configfile)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:24:19+00:00Added an answer on May 16, 2026 at 4:24 pm

    The r+ option (in the open in the with) is telling Python to keep the file’s previous contents, just overwriting the specific bytes that will be written to it but leaving all others alone. Use w to open a file for complete overwriting, which seems to be what you should be doing here. Overwriting just selected bytes inside an existing file is very rarely what you want to do, particularly for text files, which you’re more likely to want to see as sequence of lines of text, rather than bunches of bytes! (It can be useful in very specialized cases, mostly involving large binary files, where the by-byte view may make some sense).

    The “by-line organization” with which we like to view text files is not reflected in the underlying filesystem (on any OS that is currently popular, at least — back in the dark past some file organizations were meant to mimic packs of punched cards, for example, so each line had to be exactly 80 bytes, no more, no less… but that’s a far-off ancient memory, at most, for the vast majority of computer programmers and users today;-).

    So, “overwriting part of a file in-place” (where the file contains text lines of different lengths) becomes quite a problem. Should you ever need to do that, btw, consider the fileinput module of the standard Python library, which mimics this often-desired but-running-against-the-filesystem’s-grain operation quite competently. But, it wouldn’t help you much in this case, where simple total overwriting seems to be exactly right;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a config file using configParser : <br> [ section one ]<br> one
I have a python class which reads a config file using ConfigParser: Config file:
I'm using ConfigParser in Python 2.7 to read from a config file and I'm
I have a config.cfg which I parse using the python-module ConfigParser. In one section
I can use the ConfigParser module in python to create ini-files using the methods
I'm using Python's ConfigParser to create a configuration file. I want to check if
I have an INI file I need to modify using Python. I was looking
Using the ConfigParser module I am attempting to read in a configuration file so
Using import datetime in python, is it possible to take a formatted time/date string
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.