0

I am fairly new in python and I am writing a python script that can read json files from a directory and work on it. For that purpose, I am asking the user about the file name.

file_dir = input("Please enter the directory COMPLETE path where all dialog json files are placed: ")

files = listdir(str(file_dir))

However, I get the error when I enter the path.

Please enter the directory COMPLETE path where all dialog json files are placed: /Users/monideepde/Documents/Sanofi/EnglishDialogs
Traceback (most recent call last):
  File "/Users/monideepde/PycharmProjects/KoreDialogLanguageConverter/Converter.py", line 4, in <module>
    file_dir = input("Please enter the directory COMPLETE path where all dialog json files are placed: ")
  File "<string>", line 1
    /Users/monideepde/Documents/Sanofi/EnglishDialogs
    ^
SyntaxError: invalid syntax

Process finished with exit code 1

Strangely, this error is not seen when I wrap my path in double-quotes. If anyone understands why I am getting this error can you please share it with me?

P.S: I am using python 2.7

Thanks

Moni
  • 869
  • 3
  • 9
  • 21

2 Answers2

1

In Python 2.x, input() tries to run the input as a Python expression. Use raw_input() instead - it returns a string without evaluation

file_dir = raw_input("Please enter the directory COMPLETE path where all dialog json files are placed: ")
Oleh Rybalchenko
  • 6,998
  • 3
  • 22
  • 36
1

Try raw_input instead of input since you are using python2.7. raw_input("enter the directory").

And for working with directories check out the os module.

EHM
  • 877
  • 1
  • 7
  • 28