0

I am creating a terminal based python application whereby the user drags and drops a csv file into the terminal to get the file path. The file path is therefore escaped.

How do I remove all instances of this?

For example, I have a file

thisisatestfile/\(2).csv

but when I drag it into terminal it appears as:

thisisatestfile\:\\\(2\).csv

I have a list of all the shell escape characters that I need to remove: link to characters

I am not very good at regex so any help much appreciated!

Itergator
  • 299
  • 3
  • 16

1 Answers1

0

I just implemented this with shlex.split

>>> shlex.split('thisisatestfile/\(2).csv')
['thisisatestfile/(2).csv']

Since this method is intended for taking a raw shell invocation and returning a list of args to be passed to, e.g., subprocess.Popen, it returns a list. If you know you only have a single string to process, just grab the first element of the returned list.

>>> shlex.split('thisisatestfile/\(2).csv')[0]
'thisisatestfile/(2).csv'
theY4Kman
  • 5,572
  • 2
  • 32
  • 38