#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0

    Created file with read-only state?


    This is the code I'm using for logging and debugging, but when there is no config file, it creates one. The problem is that the file is read-only. Can someone help me with this, I can't seem to figure it out.

    Code:
    import configparser, logging, os
    
    Config = configparser.RawConfigParser()
    
    config_file_location = 'libs/config.ini'
    
    if(not os.path.isfile(config_file_location)):
    	config_file = open(config_file_location, 'w')
    	
    	# Create all of the default sections first
    	config_sections = ['main', 'model_date', 'model_mail', 'model_weather', 'model_music']
    	for section in config_sections:
    		Config.add_section(section)
    		
    	# Ask the user for the necessary information
    	config_properties = ({'gpio_warnings' : 0, 'refresh_rate' : 0.1, 'menu_on_startup' : 1, 'enable_console_debug' : 1},
    	{'notation_date' : '%a %d %b %y', 'notation_time' : '%H:%M'},
    	{'imap4_ssl' : 'imap.gmail.com', 'mail' : '', 'password' : ''},
    	{'key' : '', 'city' : '', 'use_celsius' : 1}, {'directory' : ''})
    	
    	for s, p in zip(config_sections, config_properties):
    		for prop, value in p.items():
    			nv = input('Created property "' + prop + '" in section "' + s + '" with a default value of "' + str(value) + '". Edit it or press enter to use the default value.\n')
    			if nv == '':
    				nv = value
    			Config.set(s, prop, nv)
    			
    	Config.write(config_file)
    	config_file.close()
    	
    
    Config.read(config_file_location)
    
    logging_levels = ['INFO', 'DEBUG']
    Logger = logging.getLogger('main')
    Logger.setLevel(logging.DEBUG)
    
    logger_filehandler = logging.FileHandler('debug.log')
    logger_filehandler.setLevel(logging.DEBUG)
    logger_streamhandler = logging.StreamHandler()
    logger_streamhandler.setLevel(getattr(logging, logging_levels[int(Config.get('main', 'enable_console_debug'))]))
    
    logger_formatter_filehandler = logging.Formatter('[%(levelname)s %(asctime)s] %(message)s')
    logger_formatter_streamhandler = logging.Formatter('\033[33;1m[%(levelname)s] %(message)s\033[0m')
    
    logger_filehandler.setFormatter(logger_formatter_filehandler)
    logger_streamhandler.setFormatter(logger_formatter_streamhandler)
    
    Logger.addHandler(logger_filehandler)
    Logger.addHandler(logger_streamhandler)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,889
    Rep Power
    481
    Code:
    >>> help(os.chmod)
    help(os.chmod)
    Help on built-in function chmod in module posix:
    
    chmod(...)
        chmod(path, mode)
        
        Change the access permissions of a file.
    >>> 
    >>> 
    >>> 
    >>> 
    >>> help(os.stat)
    Help on built-in function stat in module posix:
    
    stat(...)
        stat(path) -> stat result
        
        Perform a stat system call on the given path.
    
    >>>
    You could change the mode then reopen the file, create the file yourself if there isn't one,
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    28
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    >>> help(os.chmod)
    help(os.chmod)
    Help on built-in function chmod in module posix:
    
    chmod(...)
        chmod(path, mode)
        
        Change the access permissions of a file.
    >>> 
    >>> 
    >>> 
    >>> 
    >>> help(os.stat)
    Help on built-in function stat in module posix:
    
    stat(...)
        stat(path) -> stat result
        
        Perform a stat system call on the given path.
    
    >>>
    You could change the mode then reopen the file, create the file yourself if there isn't one,
    Thanks, I was thinking about chmod but I thought I might did something wrong because I never had this problem before.

IMN logo majestic logo threadwatch logo seochat tools logo