I am currently trying to get the file_path (suppose ~/hello_world/) as user input and list all the files inside this directory. I can do this exact same thing if I pass ~/hello_world as sys.argv however, I can't seem to get it work if I take it as an input. I am trying to work the code from any directory and user inputted file path will be from /home/ubunut/... Here's my working code as sys.argv:
This code is intended for unix based os only for now.
if len(sys.argv) == 2:
path = sys.argv[1]
files = os.listdir(path)
for name in files:
print(name)
Here's the code I am trying to work with:
path = input("file_path: ")
files = os.listdir(path)
for name in files:
print(name)
This is when it crashed with the following error:
Traceback (most recent call last):
File "test.py", line 14, in <module>
files = os.listdir(path)
FileNotFoundError: [Errno 2] No such file or directory: '~/hello_world/'
Thanks in advance.