How can I parse the input when it is a list of paths?
file_in = input("Insert paths: ") # foo.jpg "C:\Program Files\bar.jpg"
print(file_in) # foo.jpg "C:\Program Files\bar.jpg"
I'm looking for a clean way to get the input foo.jpg "C:\Program Files\bar.jpg"
in a list ['foo.jpg', 'C:\Program Files\bar.jpg']
(note the quotes in the second path because of the space in Program Files
).
Is there something like argparse but for input()
s?
What is the best way to handle it?
Here is what you want:
import shlex
file_in = input("Insert paths: ") # foo.jpg "C:\Program Files\bar.jpg"
print(shlex.split(file_in)) # foo.jpg "C:\Program Files\bar.jpg"
Output:
['foo.jpg', 'C:\\Program Files\\bar.jpg']