Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's os.chdir() and os.getcwd() mismatch when using tempfile.mkdtemp() on Mac OSX Lion

I'm not sure if this is a bug or a feature, but when I change the directory using os.chdir() to the one generated with tempfile.mkdtemp() then os.getcwd() reports a directory with /private prefix.

The following code illustrates this:

In [1]: import os, tempfile

In [2]: d = tempfile.mkdtemp()

In [3]: d
Out[3]: '/var/folders/s4/grpfgn297hjgnfws3tl_gnt80000gn/T/tmpmfNUYz'

In [4]: os.chdir( d )

In [5]: os.getcwd()
Out[5]: '/private/var/folders/s4/grpfgn297hjgnfws3tl_gnt80000gn/T/tmpmfNUYz'

Could someone please explain why this is so?

like image 488
kgr Avatar asked Sep 18 '12 18:09

kgr


1 Answers

/var is a symlink to /private/var

$ ls -l /var
lrwxr-xr-x@ 1 root  wheel  11 Dec  2  2011 /var -> private/var

tempfile is just using the environment TMPDIR variable to prefix the path location, so its just a string. But os.getcwd() is resolving the absolute location:

$ echo $TMPDIR
/var/folders/04/kc575q1n6x9drkwxyfljg5zw0000gn/T/
like image 200
jdi Avatar answered Nov 03 '22 22:11

jdi