I have a config file abc.txt
which looks somewhat like:
path1 = "D:\test1\first" path2 = "D:\test2\second" path3 = "D:\test2\third"
I want to read these paths from the abc.txt
to use it in my program to avoid hard coding.
Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.
Because they are plain text documents, you can open mugen. cfg or any other M.U.G.E.N CFG file with any text editor. In Windows, you can open a CFG file with Microsoft Notepad or Microsoft WordPad. In macOS, you can use Apple TextEdit.
In order to use my example, your file "abc.txt" needs to look like this.
[your-config] path1 = "D:\test1\first" path2 = "D:\test2\second" path3 = "D:\test2\third"
Then in your code you can use the config parser.
import ConfigParser configParser = ConfigParser.RawConfigParser() configFilePath = r'c:\abc.txt' configParser.read(configFilePath)
As human.js noted in his comment, in Python 3, ConfigParser
has been renamed configparser
. See Python 3 ImportError: No module named 'ConfigParser' for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With