0

I use this code to get the path of the excel file:

xlsx_path = input("excel path:")
print(repr(xlsx_path))

The user will give me "C:\Users\Lenovo\test.xlsx" or C:\Users\Lenovo\test.xlsx. But if user give me a "C:\Users\Lenovo\test.xlsx"(Note the user will give me a redundant quotation mark), the xlsx_path will be '"C:\\Users\\Lenovo\\test.xlsx"' actually. Then I cannot recognize the path. How to get a uniform path regardless of user give me any path?

yode
  • 483
  • 7
  • 16
  • the_path = the_path.strip('\"') works for both cases giving the same result. – bad_coder Jan 10 '20 at 18:21
  • Does this answer your question? [Remove quotes from String in Python](https://stackoverflow.com/questions/40950791/remove-quotes-from-string-in-python) – bad_coder Jan 10 '20 at 18:26

1 Answers1

0

If your users are typing in double-quotes around the filename, you can remove them by using the .strip() method for strings. If there are no double-quotes around it, it will have no effect. Try this:

xlsx_path = input("excel path:")
xlsx_path = xlsx_path.strip('"')
print(repr(xlsx_path))
sql_knievel
  • 1,199
  • 1
  • 13
  • 26