如何知道您何时处于符号链接中

时间:2022-05-23 07:35:05

How to know, in Python, that the directory you are in is inside a symbolic link ?

如何在Python中知道您所在的目录是否在符号链接中?

I have a directory /tmp/foo/kiwi

我有一个目录/ tmp / foo / kiwi

I create a symlink /tmp/bar pointing to /tmp/foo

我创建了一个指向/ tmp / foo的符号链接/ tmp / bar

I enter into /tmp/bar/kiwi

我进入/ tmp / bar / kiwi

the linux command pwd tells me I'm in /tmp/bar/kiwi, which is correct.

linux命令pwd告诉我我在/ tmp / bar / kiwi,这是正确的。

The python command prompt tells me I'm in /tmp/foo/kiwi:

python命令提示符告诉我我在/ tmp / foo / kiwi:

Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'/tmp/foo/kiwi'

Is there a way, in Python, to get the directory I'm really in ?

有没有办法在Python中获取我真正的目录?

6 个解决方案

#1


If you don't find anything else, you can use

如果您没有找到任何其他内容,则可以使用

os.getenv("PWD")

It's not really a portable python method, but works on POSIX systems. It gets the value of the PWD environment variable, which is set by the cd command (if you don't use cd -P) to the path name you navigated into (see man cd) before running the python script. That variable is not altered by python, of course. So if you os.chdir somewhere else, that variable will retain its value.

它不是一个真正的便携式python方法,但适用于POSIX系统。它获取PWD环境变量的值,该值由cd命令(如果不使用cd -P)设置为运行python脚本之前导航到的路径名(请参阅man cd)。当然,python不会改变那个变量。因此,如果你在其他地方使用os.chdir,那么该变量将保留其值。

Anyway, as a side node, /tmp/foo/kiwi is the directory you are in. I'm not sure whether anything apart from the shell knows that you've really navigated through another path into that place, actually :)

无论如何,作为一个侧节点,/ tmp / foo / kiwi是你所在的目录。我不确定除了shell之外是否知道你真的已经通过另一条路径导航到那个地方了,实际上:)

#2


If your symlink is set up in the way you state, then /tmp/foo/kiwi is the directory that you're really in. /tmp/bar/kiwi is just another way to get to the same place.

如果您的符号链接是按照您所述的方式设置的,那么/ tmp / foo / kiwi就是您真正所在的目录./ tmp / bar / kiwi只是到达同一个地方的另一种方式。

Note that the shell command pwd -P will give you the physical path of the current directory. In your case, the shell is remembering that you got where you are through the bar symlink, so it tell you that you are in /tmp/bar/kiwi.

请注意,shell命令pwd -P将为您提供当前目录的物理路径。在你的情况下,shell记住你通过bar符号链接到达了你的位置,因此它告诉你你在/ tmp / bar / kiwi。

#3


Just as a matter of interest, if you are in a directory you can use the -P option to get the pwd command to resolve all symbolic links to their actual directories.

同样值得关注的是,如果您在目录中,则可以使用-P选项获取pwd命令以解析指向其实际目录的所有符号链接。

$ ln -s Desktop toto
$ cd toto
$
$ pwd
/home/ken/toto
$ 
$ pwd -P
/home/ken/Desktop
$ 

HTH

cheers,

Rob

#4


You could also try lstat. It will give you info about a file/dir, including telling you whether it's a link and showing you where it links to if it is.

你也可以试试lstat。它将为您提供有关文件/目录的信息,包括告诉您它是否是链接并显示链接到的位置(如果是)。

#5


When your shell returning the path, it is relying on the shell enviroment variable "PWD" which gets set as you traverse through the symlink path, but actually it is under the directory as returned by the getcwd(). So, if you get from shell's PWD you will get what you want.

当你的shell返回路径时,它依赖于shell环境变量“PWD”,它在遍历符号链接路径时被设置,但实际上它位于getcwd()返回的目录下。所以,如果你从shell的PWD获得,你将得到你想要的。

>>> os.getcwd()
'/home/ors/foo/tmp/foo/kiwi'
>>> os.environ["PWD"]
'/home/ors/foo/tmp/bar/kiwi'
>>>

#6


Here is another way:

这是另一种方式:

import os
os.popen('pwd').read().strip('\n')

Here is a demonstration in python shell:

这是python shell中的演示:

>>> import os
>>> os.popen('pwd').read().strip('\n')
'/home/projteam/staging/site/proj'
>>> # This returns actual path
>>> import subprocess
>>> p = subprocess.Popen('pwd', stdout=subprocess.PIPE)
>>> p.communicate()[0]  # returns non-symlink path
'/home/projteam/staging/deploys/20150114-141114/site/proj\n'

Getting the environment variable PWD didn't always work for me so I use the popen method. Cheers!

获取环境变量PWD并不总是适合我,所以我使用popen方法。干杯!

#1


If you don't find anything else, you can use

如果您没有找到任何其他内容,则可以使用

os.getenv("PWD")

It's not really a portable python method, but works on POSIX systems. It gets the value of the PWD environment variable, which is set by the cd command (if you don't use cd -P) to the path name you navigated into (see man cd) before running the python script. That variable is not altered by python, of course. So if you os.chdir somewhere else, that variable will retain its value.

它不是一个真正的便携式python方法,但适用于POSIX系统。它获取PWD环境变量的值,该值由cd命令(如果不使用cd -P)设置为运行python脚本之前导航到的路径名(请参阅man cd)。当然,python不会改变那个变量。因此,如果你在其他地方使用os.chdir,那么该变量将保留其值。

Anyway, as a side node, /tmp/foo/kiwi is the directory you are in. I'm not sure whether anything apart from the shell knows that you've really navigated through another path into that place, actually :)

无论如何,作为一个侧节点,/ tmp / foo / kiwi是你所在的目录。我不确定除了shell之外是否知道你真的已经通过另一条路径导航到那个地方了,实际上:)

#2


If your symlink is set up in the way you state, then /tmp/foo/kiwi is the directory that you're really in. /tmp/bar/kiwi is just another way to get to the same place.

如果您的符号链接是按照您所述的方式设置的,那么/ tmp / foo / kiwi就是您真正所在的目录./ tmp / bar / kiwi只是到达同一个地方的另一种方式。

Note that the shell command pwd -P will give you the physical path of the current directory. In your case, the shell is remembering that you got where you are through the bar symlink, so it tell you that you are in /tmp/bar/kiwi.

请注意,shell命令pwd -P将为您提供当前目录的物理路径。在你的情况下,shell记住你通过bar符号链接到达了你的位置,因此它告诉你你在/ tmp / bar / kiwi。

#3


Just as a matter of interest, if you are in a directory you can use the -P option to get the pwd command to resolve all symbolic links to their actual directories.

同样值得关注的是,如果您在目录中,则可以使用-P选项获取pwd命令以解析指向其实际目录的所有符号链接。

$ ln -s Desktop toto
$ cd toto
$
$ pwd
/home/ken/toto
$ 
$ pwd -P
/home/ken/Desktop
$ 

HTH

cheers,

Rob

#4


You could also try lstat. It will give you info about a file/dir, including telling you whether it's a link and showing you where it links to if it is.

你也可以试试lstat。它将为您提供有关文件/目录的信息,包括告诉您它是否是链接并显示链接到的位置(如果是)。

#5


When your shell returning the path, it is relying on the shell enviroment variable "PWD" which gets set as you traverse through the symlink path, but actually it is under the directory as returned by the getcwd(). So, if you get from shell's PWD you will get what you want.

当你的shell返回路径时,它依赖于shell环境变量“PWD”,它在遍历符号链接路径时被设置,但实际上它位于getcwd()返回的目录下。所以,如果你从shell的PWD获得,你将得到你想要的。

>>> os.getcwd()
'/home/ors/foo/tmp/foo/kiwi'
>>> os.environ["PWD"]
'/home/ors/foo/tmp/bar/kiwi'
>>>

#6


Here is another way:

这是另一种方式:

import os
os.popen('pwd').read().strip('\n')

Here is a demonstration in python shell:

这是python shell中的演示:

>>> import os
>>> os.popen('pwd').read().strip('\n')
'/home/projteam/staging/site/proj'
>>> # This returns actual path
>>> import subprocess
>>> p = subprocess.Popen('pwd', stdout=subprocess.PIPE)
>>> p.communicate()[0]  # returns non-symlink path
'/home/projteam/staging/deploys/20150114-141114/site/proj\n'

Getting the environment variable PWD didn't always work for me so I use the popen method. Cheers!

获取环境变量PWD并不总是适合我,所以我使用popen方法。干杯!