-1

I've got a bunch of Python webdriver runs that I've converted from Selenium IDE. These runs have 1 or 2 settings that I would like to be able to change using a configuration file that would be created by running a script that collects user input that would set these 2 variables.

Here is my attempt at using the ConfigParser module:

import ConfigParser

file_path_input = raw_input("Enter path to 'webdriver' directory ex: '/home/user/': ")
print "you entered", file_path_input

url_input = raw_input("Enter url that the application will point to, ex: 172.31.13.56 or vtm55.example.com: ")
print "you entered", url_input

def createConfig(file_path_input):
"""
Create a config file
"""
config = ConfigParser.ConfigParser()
config.add_section("application_settings")
config.set("application_settings", "file_path_to_use", "file_path_input")
config.set("application_settings", "url_to_use", "url_input")
config.set("application_settings", "settings_info",
    "Your application directory is in %(file_path_to_use)s and your application url is %(url_to_use)s")

with open(file_path_input, "wb") as config_file:
    config.write(config_file)

The raw_input() and print() lines work, but the configuration file doesn't appear to be generated at all. Once the file has been created, I'd like to be able to insert the variables file_path_to_use and url_to_use in my various Python webdriver runs.

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
edszr
  • 63
  • 2
  • 10
  • Could you add a few print lines to show what's being executed? Particularly just after the `with(open)` print the file_path_input and config_file vars? Can might show something wrong. Heck, maybe add all the output of when you run it. Alternatively, remove the binary ('b') flag from the open-file statement and try writing it as plain text. – TCAllen07 Mar 18 '15 at 20:19
  • It does not write what you expect though – Padraic Cunningham Mar 18 '15 at 20:26

4 Answers4

1
  1. Your indentation is problematic, particularly in the createConfig function.
  2. "file_path_input" is different from file_path_input.
  3. Use 'w' (a normal write) instead of 'wb' (write bytes).
  4. You have config and config_file backwards - call write on config_file, then pass it the content you want to write. If you have a list of strings, simply loop through them and write each one with basic file I/O:

    configs = ['file_path_to_use:' + file_path_input,
    'url_to_use:' + url_input]
    with open(file_path_input, 'w') as config_file:
        for line in configs:
            config_file.write(line + '\n')
    

    Sample result:

    file_path_to_use:/home/user/
    url_input:vtm.example.com
    

    I didn't include the "settings_info" portion because it's just a recap.

  5. You can then split or partition on ':' when reading the file back in.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

I would recommend writing your output as text, not using ConfigParser. I've had odd issues with using to create config files in the past (though better luck using it to read them). If you convert to write just a text file with standard file-i/o, you have more specificity in how it's written, while still writing it in a format that ConfigParser can later read in.

Hayk
  • 23
  • 5
0

You could scrap using ConfigParser and instead create a .py file with the variables saved within in the format KEY=VALUE in the same way you are creating a config file.

Then within your code, write your own function that opens the .py file (using with syntax, so that it automatically closes the file without .close()) and reads it, saving it as a string.

You can then use string manipulation to get the string you want and assign it to a new variable (remember to convert it to the data type you want to use, i.e. int() for an integer). Use type() to check the type of the variable if you aren't sure.

Alice
  • 588
  • 1
  • 8
  • 25
0

It actually starts with lowercase configparser and configparser.ConfigParser(). Please update.

JAbr
  • 312
  • 2
  • 12