1

If you use macOS then you would know how you can type cd ~/Documents in the terminal and regardless of the user of the computer, the CWD would change to Documents.

In python, I'm trying to do the same thing using os.chdir("~/Documents") using the os module but it doesn't work!

Does anyone know another way to do this?

hifromdev
  • 262
  • 3
  • 10

1 Answers1

3

How do I use '~' (tilde) in the context of paths?

One solution to your problem based on the above description would be os.chdir(os.path.expanduser('~/Documents'))

Zooby
  • 325
  • 1
  • 7
  • Thank you so much. I was so stumped on this one! – hifromdev Oct 05 '19 at 01:34
  • I think tilde is a special character in google, so you normally benefit from using the word not the character when searching. Happy to help :) – Zooby Oct 05 '19 at 01:38
  • 1
    Unrelated to the question or the answer, but relevant to @Zooby's comment, [tilde is not a special character in google search](https://www.searchenginewatch.com/2013/06/25/google-kills-tilde-search-operator/) – Guy Oct 05 '19 at 01:47
  • Then why does "How to use tilde in python" and "How to use ~ in python" return vastly different results? – Zooby Oct 05 '19 at 01:56