2

I have a text file that looks like this:

../../../../foo/bar ../../this/that ../barfoo

and I want:

foo/bar this/that barfoo

 with open('file_list.txt', 'r') as file_list:
        for file_list_lines in file_list:
            file_list_lines.lstrip('../')
            print(file_list_lines)

I tried .lstrip('../') but nothing was stripped from the beginning of the line.

Semaphore
  • 35
  • 4
  • The simplest solution for this issue can be using split and use python filter "/".join(filter(lambda x: x!='..' ,file_list_lines.split('/')))) But you can also use Python regular expression – Sudhanshu Patel May 17 '19 at 19:27

1 Answers1

6

The string.lstrip() does not do the string manipulation in place. In other words, you would need to store it into a variable like so:

stripped_line = file_list_lines.lstrip('../')
print( stripped_line )

In your version, you did the lstrip, but did not store the result of that operation anywhere.

Jason K Lai
  • 1,500
  • 5
  • 15