0

Is there a way to use the Open() function with an absolute path? I would like to open any file wherever it is on the disk, but Open() sets it's working directory to the location of the file by default, can I change this?

4
  • What Open() function are you talking about? Do you mean the standard open() function? Commented Feb 27, 2021 at 21:02
  • Yes, sorry that was a typo Commented Feb 27, 2021 at 21:03
  • 1
    As long as you pass an absolute path to open(), the working directory is irrelevant. It is only used to turn a provided relative path into an absolute path. Commented Feb 27, 2021 at 21:04
  • Sorry, I was assigning the file path with a variable through a function call, when I surrounded it by an str(), for some reason it worked, thank you :) Commented Feb 27, 2021 at 21:06

1 Answer 1

1

If you are on linux you would need to add a / to the beginning of the path, for example to access /tmp/someFile.txt you would do open("/tmp/someFile.Txt"). This works because / references the root.

On windows (this has not been tested) i before you would do open("C:/someFile.txt").

The docs for the open function can be found at: https://docs.python.org/3/library/functions.html#open

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.