Best way to accept permanent input from a user?

yayu

I have a script which has a global variable that must be set by the user once and for all, the variable is a string containing a pathname, and each time the script runs it needs it. I don't want to prompt the user each time for this pathname.

Currently, I am considering asking the user to a set an environment variable permanently, by adding it to his /etc/profile or .bash_profile, and access it with sys.environ dictionary. The other option would be to have a config file and ask the user to edit the relevant line, then use configparser to read it.

Is there a recommended method for doing this?

skrrgwasme

Use the Python ConfigParser module, or configparser in Python 3.

It follows the standard *.ini format and allows you to store information from one run to the next in an easily readable format. The format is essentially self-documenting because you can name your keys in the file, and you can add comments to the configuration file too.

It also provides more flexibility over the environment variable method because it is easier to modify a configuration file, and the file can easily be passed from one computer to the next along with your script regardless of platform or other environment settings.

Your use case is exactly what configuration files are intended for, and you could accomplish your task with only a handful of lines of code:

cfg_parser = ConfigParser.ConfigParser() # Python 2.x
if cfg_parser.read('config_file_name.ini'):
    path = cfg_parser.get('SECTION_NAME', 'path')
else:
    print("No config file found")

This gives you your path, and all you have to ask your user to do is edit one line of a text file instead of making any system changes.

Additionally, this gives you a lot of room to expand in the future. If you ever want more options added to your script, modifying a configuration file is a lot easier than coming up with new environment variables.

Lastly, the ConfigParser library allows you to edit configuration files programmatically as well. You could add a command line option (perhaps with argparse) that allows your user to specify a path, and have your script automagically write its own config file with the path. Now your user never has to touch the configuration file manually, and will never have to add the path on the command line again either. Even better, if the path ever changes, your user can just run it with the command line path option again and voila, the old path in the config file is overwritten and the new one is saved.

I would definitely recommend the configuration file approach due to its flexibility and user-friendliness.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accept user input from Activity

From Dev

Accept YAML input from website user

From Dev

Accept YAML input from website user

From Dev

How to accept input from user (console) in erlang

From Dev

what is best way to accept function input parameters in php?

From Dev

What is the best way to safely read user input?

From Dev

Best way to submit user input on click

From Dev

What is the best way to have date and time input in html (from user) and receive in Javascript?

From Dev

Best Way To Access User Permissions From View

From Dev

What is the best way to add a permanent route?

From Java

How to accept a full and partial input String from the user?

From Dev

Why will my java program not accept alphabetical input from the user?

From Dev

ARRAY- Accept user input and output the corresponding choice from array

From Dev

Best way to go about sanitizing user input in rails

From Dev

jQuery mobile - What is the best way save user input?

From Dev

Best way to validate user input URL (see if URL exists)

From Dev

jQuery mobile - What is the best way save user input?

From Dev

Best way to perform string validation with user input in Python 3.4?

From Dev

Best way format user input before storing it (C)?

From Dev

PHP file to accept user input

From Dev

Best way to authenticate user

From Dev

Best way to alert user

From Dev

Java 8 Best way to build an IntStream from input

From Dev

Best way to get the 'id' from an input element if knowing the 'name' of it

From Dev

What is best way to make a calculation with input values from a html form?

From Dev

Best way to input/output data from file in C++?

From Dev

What is the best way to get amplitude and frequency of sound from microphone input?

From Dev

What is the best way to hide a specific user from a table in Laravel 4 ?

From Dev

Best way to handle user verification in iron router from resume

Related Related

  1. 1

    Accept user input from Activity

  2. 2

    Accept YAML input from website user

  3. 3

    Accept YAML input from website user

  4. 4

    How to accept input from user (console) in erlang

  5. 5

    what is best way to accept function input parameters in php?

  6. 6

    What is the best way to safely read user input?

  7. 7

    Best way to submit user input on click

  8. 8

    What is the best way to have date and time input in html (from user) and receive in Javascript?

  9. 9

    Best Way To Access User Permissions From View

  10. 10

    What is the best way to add a permanent route?

  11. 11

    How to accept a full and partial input String from the user?

  12. 12

    Why will my java program not accept alphabetical input from the user?

  13. 13

    ARRAY- Accept user input and output the corresponding choice from array

  14. 14

    Best way to go about sanitizing user input in rails

  15. 15

    jQuery mobile - What is the best way save user input?

  16. 16

    Best way to validate user input URL (see if URL exists)

  17. 17

    jQuery mobile - What is the best way save user input?

  18. 18

    Best way to perform string validation with user input in Python 3.4?

  19. 19

    Best way format user input before storing it (C)?

  20. 20

    PHP file to accept user input

  21. 21

    Best way to authenticate user

  22. 22

    Best way to alert user

  23. 23

    Java 8 Best way to build an IntStream from input

  24. 24

    Best way to get the 'id' from an input element if knowing the 'name' of it

  25. 25

    What is best way to make a calculation with input values from a html form?

  26. 26

    Best way to input/output data from file in C++?

  27. 27

    What is the best way to get amplitude and frequency of sound from microphone input?

  28. 28

    What is the best way to hide a specific user from a table in Laravel 4 ?

  29. 29

    Best way to handle user verification in iron router from resume

HotTag

Archive