1

I've been working on a python application that takes input from the user.

The input is the path to a text file.

Once the user has entered the path to a text file, it will be opened, and the contents displayed in the console.

file = input()

with open(file, "r") as file:
    content = file.read()

Currently, it will only accept 'literal file paths', for example C:/users/name/documents/filename.txt

I would like it to accept relative file paths aswell, for example ./file.txt, which should get a file named file.txt in the current directory, or ../file2.txt which should go up a directory (..) and then get a file called file2.txt.

How can I achieve this?

I was unable to find any relevant answers.

  • . and .. are relevant to your current working directory. So make sure they are correct. (open command doesn't care about absolute paths and relative paths) – RvdK Sep 03 '18 at 14:56
  • 2
    There's nothing wrong with that code, it already supports relative file paths as-is. What makes you think that it doesn't? Are you sure the current working directory is set to what you think it is? – Aran-Fey Sep 03 '18 at 14:56

0 Answers0