Home > Enterprise >  Striping a string from newline markers
Striping a string from newline markers

Time:08-16

I store configuration data (paths to specific files) inside a file named app.cfg that looks like this :

path/to/config.json
path/to/default/folder

and I query those item with the following Python code:

with open("app.cfg","r",newline='') as config:
    data = config.readlines()
    PathToConfig = data[0]
    DefaultPath = data[1]
    config.close()

But when I use PathToConfig in my script, the path stored in this variable cannot be used because there is \n at the end of the string. I tried to fix this issue by using this PathToConfig = data[0].rstrip() but there still is \n at the end of the string.

How can I strip this string from the newline marker ?

CodePudding user response:

You should be able to solve it with app.cfg

read contents from file:

with open("app.cfg","r",newline='') as config:
    data = config.readlines()
    PathToConfig = data[0].rstrip("\n")
    DefaultPath = data[1]

output: enter image description here

  • Related