Finding a file's directory address on a Mac
The desktop is just a subdirectory of the user’s home directory. Because the latter is not fixed, use something like os.path.expanduser
to keep the code generic. For example, to read a file called somefile.txt
that resides on the desktop, use
import os
f = open(os.path.expanduser("~/Desktop/somefile.txt"))
If you want this to be portable across operating systems, you have to find out where the desktop directory is located on each system separately.
f = open (r"/Users/USERNAME/Desktop/somedir/somefile.txt")
or even better
import os
f = open (os.path.expanduser("~/Desktop/somedir/somefile.txt"))
Because on bash (the default shell on Mac Os X) ~/
represents the user's home directory.