Search code examples
pythonpython-os

Python changing directory without path


I would like to change the current work directory to the desktop without using os.chdir(path) since i want to use this code on different computer so the path will be different


Solution

  • To get the desktop location you can use os.path.expanduser("~/Desktop"). This should work both in Windows and Linux, regardless of actual directories where the desktop is kept. To change directory to the current desktop you should do something like this:

    os.chdir(os.path.expanduser("~/Desktop"))