I'm loading a text file which contains a path to a file on each line. However, I'm having issues with the newline character.
This is how I'm reading in the data:
- Code: Select all
def parse_remote_paths(remote_config_path):
remote_paths_list = []
_remote_paths_list_file = open(os.path.normpath(remote_config_path), 'r')
_remote_paths_list_lines = _remote_paths_list_file.readlines()
for _line in _remote_paths_list_lines:
_norm_line = os.path.normpath(_line)
remote_paths_list.append(_norm_line)
return remote_paths_list
However, the content of this list ends up as:
- Code: Select all
['\\\\dworkin\\FeersumEndjinn\\difftest\\one.config\n', '\\\\dworkin\\FeersumEndjinn\\difftest\\eventlogger.config\n',
'\\\\dworkin\\FeersumEndjinn\\difftest\\spec01.config']
Ignore the multiple backslashes as this is pulling files from a Windows share, and os.path.normpath fixes the slashes for me (tried and tested!). However it does appear that normpath is ignoring the \n and probably assuming it's part of the path rather than a newline command.
What I have having problems with, is the end of all but the last last has a \n newline character added?
What is the best way to accomodate that and get rid of it? Should I iterate through each row in the file and use string slicing in a conditional replace statement?
i.e.
- Code: Select all
# pseudo code!
for line in file:
if line[-2:] == "\n":
line = line[:-2]
Or is there a proper way to drop the newline character that I'm totally missing, i.e. by reading the file in a different way?
Kind Regards,
FeersumEndjinn