How to do a dynamic Desktop path on Windows and Linux?

I have this line:

system.file.openFile("xlsx", "C:\Users\name\Desktop")

However some of my team works on Linux and that path doesn't work.
How could I do a dynamic Desktop path?

I tried:

import os
os.environ['USERPROFILE'] + '\Desktop'

But for some reason for some of us doesn't work (?). It opens in documents sometimes. Not sure.

Thank you!

I believe $HOME (or ~ for short) works on most distros of Linux and Windows.

Wiki: Home_directory

The below should expand to the appropriate user location in both OS:

import os.path
relativePath = "~/Desktop" # Linux, Windows
userPath = os.path.expanduser(relativePath)
print userPath

I’m unaware if the /Desktop portion breaks down on certain distros.

1 Like

Don't use jython stdlib. Use java.io.File or java.nio.file.Path to construct your absolute file names. These will properly use the appropriate platform folder separator. Use those classes to parse the string from java.lang.System.getEnv("HOME") as your starting point.

Also, be aware that using backslashes as folder separators only works on Windows. In java and jython, forward slashes usually work in all OSes.

3 Likes

Thanks! But I don't seem able to make it work on all enviroments. I changed the way it's getting done so I don't trully need it anymore.

Thx anyway! I trully appreciate it

You didn't tag your post as Vision, but it only applies to Vision. I hope you weren't expecting any of this to work in Perspective.

Not vision, not perspective. Script meant to execute from the designer only.