简体   繁体   English

如何仅使用相对路径在Python中创建符号链接

[英]How to create symlink in Python using only relative paths

Consider a simple function to make a directory and then a symlink to the directory, all required to be assumed under the current working directory. 考虑一个简单的函数来创建目录,然后建立目录的符号链接,所有这些都必须假定在当前工作目录下。 In the case when the directory and link are directly within the current working directory, this process succeeds. 如果目录和链接直接位于当前工作目录中,则此过程成功。 But when the directory and link are nested further, the directory creation succeeds, while the symlink that is created is broken and points to an incorrect path. 但是,当目录和链接进一步嵌套时,目录创建成功,而创建的符号链接断开并指向错误的路径。

In [1]: def make_dir_and_symlink(dir_relative_to_cwd, sym_relative_to_cwd):
   ...:     os.mkdir(dir_relative_to_cwd)
   ...:     os.symlink(dir_relative_to_cwd, sym_relative_to_cwd)
   ...:     

In [2]: make_dir_and_symlink("test", "test-link")

In [3]: os.path.exists("test")
Out[3]: True

In [4]: os.path.exists("test-link")
Out[4]: True

In [5]: make_dir_and_symlink("test/other-test", "test/other-test-link")

In [6]: os.path.exists("test/other-test")
Out[6]: True

In [7]: os.path.exists("test/other-test-link")
Out[7]: False

Without resorting to any absolute paths in the second example, how can I create a symlink somewhere within a path that is relative to the current working directory? 在第二个示例中,在不使用任何绝对路径的情况下,如何在相对于当前工作目录的路径中的某处创建符号链接?

It appears this can be achieved for a limited situation by combining os.path.relpath and os.path.basename : 似乎可以通过组合os.path.relpathos.path.basename在有限的情况下实现:

def make_dir_and_symlink(dir_relative_to_cwd, sym_relative_to_cwd):
    os.mkdir(dir_relative_to_cwd)
    target_name = os.path.basename(
        os.path.relpath(dir_relative_to_cwd, sym_relative_to_cwd)
    )
    os.symlink(target_name, sym_relative_to_cwd)



In [1]: def make_dir_and_symlink(dir_relative_to_cwd, sym_relative_to_cwd):
   ...:     os.mkdir(dir_relative_to_cwd)
   ...:     os.symlink(os.path.basename(os.path.relpath(dir_relative_to_cwd, sym_relative_to_cwd)), sym_relative_to_cwd)
   ...:     

In [2]: make_dir_and_symlink("test", "test-link")

In [3]: os.path.exists("test")
Out[3]: True

In [4]: os.path.exists("test-link")
Out[4]: True

In [5]: make_dir_and_symlink("test/other-test", "test/other-test-link")

In [6]: os.path.exists("test/other-test")
Out[6]: True

In [7]: os.path.exists("test/other-test-link")
Out[7]: True

This is not expected to handle the general case when the symlink location can be arbitrary with respect to the target location. 当符号链接位置相对于目标位置可以是任意的时,这不能解决一般情况。 os.path.basename will only return the immediate base component of the path name, so it works in this situation because it's further assumed that the target and its symlink reside next to each other in the tree starting from the current working directory. os.path.basename将仅返回路径名的直接基本组成部分,因此在这种情况下可以使用,因为它进一步假设目标及其符号链接在从当前工作目录开始的树中彼此相邻。

A solution that does not require resolving absolute paths, but still lets the link name be arbitrary within the current working directory relative to the target name is very valuable. 不需要解析绝对路径,但仍允许链接名称相对于目标名称在当前工作目录内是任意的解决方案非常有价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM