Marc
Marc

Reputation: 3

Finding a string in a file

I am attempting to rename a file with a known string from inside the file. I can get the information I want from the file, in my output from searching the file. I cannot get the information I am after if it has an extra carriage return. Here is the code:

>>> with open('c:/test/277coreB.txt', 'r') as config:
...     for line in config:
...            if "hostname" in line:
...                     host = line
...     #This is where the extra carriage return come from and I cannot get rid of it
>>> print host[9:]
XR77-2-DC-Core-B

Upvotes: 0

Views: 69

Answers (1)

Jack
Jack

Reputation: 2830

str.rstrip('\n') will get rid of the trailing newline.

Upvotes: 3

Related Questions