【问题标题】:Python can't open symlinked filePython无法打开符号链接文件
【发布时间】:2016-05-13 16:20:37
【问题描述】:

Python 无法打开我的 simlinked 文件。我确保该文件存在并且我可以访问它。我的印象是符号链接是在操作系统级别解析的,所以 Python 永远不会知道它。

therold@therold:~/Programming/ML/deeptank/dataset/inc (master)$ ls -lisa  /Users/therold/Programming/ML/deeptank/dataset/inc/training/tanks/matilda-iv_689.png
7870541 8 lrwxr-xr-x  1 therold  staff  46 13 Mai 16:44 /Users/therold/Programming/ML/deeptank/dataset/inc/training/tanks/matilda-iv_689.png -> ../training_data/matilda-iv/matilda-iv_689.png

OSX 显然能够解析符号链接的图像文件。但是 Python open() 方法让我失望了:

therold@therold:~/Programming/ML/deeptank/dataset/inc (master)$ python
Python 2.7.10 (default, Sep 23 2015, 04:34:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
>>> open("/Users/therold/Programming/ML/deeptank/dataset/inc/training/tanks/matilda-iv_689.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/Users/therold/Programming/ML/deeptank/dataset/inc/training/tanks/matilda-iv_689.png'
>>>

任何想法我做错了什么?非常感谢您的帮助。

【问题讨论】:

  • 符号链接可能已损坏。即使操作系统可以读取链接指向的目标,也不意味着目标确实存在。你能验证一下吗?
  • 似乎符号链接是作为相对链接创建的。因此,全局 Python 安装在解析相对引用后无法打开文件。
  • 尝试ls -lL /Users/therold/Programming/ML/deeptank/dataset/inc/training/tanks/matilda-iv_689.png确认文件存在。
  • 有没有办法相对于绝对符号链接进行转换?
  • 可能有,但你的逻辑有缺陷。 Python 和其他所有程序一样,完全能够打开相对符号链接。相对符号链接总是相对于包含链接的目录进行解析。

标签: python symlink


【解决方案1】:

任何想法我做错了什么?

符号链接的目标不存在。

我不明白为什么我能够在问题的 ls 语句中解决它。

你不是。

ls 命令默认作用于链接本身,而不是链接的目标。如果没有 -L 选项,ls 永远不会尝试解析符号链接。

考虑一个包含这两个文件的目录:

$ ls -l
-rw-rw-r-- 1 me me 6 May 13 11:58 a
lrwxrwxrwx 1 me me 3 May 13 12:00 b -> ./a

a 是一个包含六个字节 'hello\n' 的文本文件。 b 是一个链接文件,其中包含指向其目标路径的三个字节:'./a'ls 能够描述链接的属性,而无需取消引用链接本身。

相比之下,使用-L 选项:

$ ls -lL
-rw-rw-r-- 1 me me 6 May 13 11:58 a
-rw-rw-r-- 1 me me 6 May 13 11:58 b

现在ls 已解析b 中的链接,并显示有关链接到文件的信息。有了-Lls 现在声称b 也是一个六字节的文本文件。

最后,考虑一下:

$ rm a
$ ls -l
lrwxrwxrwx 1 me me 3 May 13 12:00 b -> ./a
$ ls -lL
ls: cannot access b: No such file or directory
l????????? ? ? ? ?            ? b

现在b 是一个链接,可解析为不再存在的文件。由于ls -l 从未尝试解析链接,因此其输出与之前的测试相比没有变化。 (b是一个链接文件,3字节长,内容'./a'。)

但是ls -lL尝试解析链接,失败,并显示失败信息。

【讨论】:

  • 再次感谢您的详细解答。
【解决方案2】:

...不要忘记使用完整路径。在带有 Python 3.7.3 的 Raspberry Pi 上,我必须包含工作链接的整个路径:

os.symlink("Desktop/a.txt","Desktop/a_link.txt")  # WRONG !

python3 无法识别文件 a.txt,因为标准 shell 的路径未激活。

os.symllink("/home/pi/Desktop/a.txt","/home/pi/Desktop/a_link.txt")

效果很好。

【讨论】:

  • 你确定第一个版本的问题不是脚本的[print] working directory当前没有设置为/home/pi吗?使用os.getcwd() 确认使用os.chdir() 更改工作目录(使用相对或绝对路径)
【解决方案3】:

我正在记录一个答案,因为我遇到了同样的问题,但是我的符号链接的目标确实存在

试图通过 pandas 中的符号链接打开文件给出了无用的未找到消息:

⇒  ipython.exe  # this is a clue to the answer below...

In [1]: import pandas as pd

In [2]: codes = pd.read_excel('codes-no-agg-children.xlsx', ...)
---------------------------------------------------------------------------
<...lots of traceback stuff...>
FileNotFoundError: [Errno 2] No such file or directory: 'codes-no-agg-children.xlsx'   

由于目标确实存在,所以上面的答案对我没有帮助,但问题提示我尝试使用常规 python 打开而不是通过 pandas:

In [3]: import os                                                                                                                                                                                                                                         
In [4]: os.system('cp ./nvivo-win/good-extracts/codes-no-agg-children.xlsx .')                                               
\\wsl$\Ubuntu\home\sigfried\ccvs\analysis'                                                                                  
CMD.EXE was started with the above path as the current directory.                                                            
UNC paths are not supported.  Defaulting to windows directory.                                                               
cp: cannot stat './nvivo-win/good-extracts/codes-no-agg-children.xlsx': No such file or directory

我不知道这意味着什么,但我认为原因是:

Windows 版 Python 无法遵循 Ubuntu (WSL 2) 符号链接(或者,至少它无法将 Ubuntu 符号链接打开回 Windows 文件。

当然,这引发了我为什么要从 Ubuntu 运行 windows python 来处理 windows 文件的问题——但这在stack-psychoanalysis-for-self-sabotaging-programmers 上会更合适。

【讨论】:

  • 我看到有人赞成我的答案,所以我回顾了一下。我觉得我的结论是对的,最后的笑话有点好笑,但是代码块完全没有意义。我一定是粘贴了错误的代码或其他东西。很抱歉!
【解决方案4】:

我遇到了同样的问题——Fedora 32 系统,Jupyter 的 Notebook 文件夹包含我的文件,其中包含指向 json 文件的符号链接。我把头撞在众所周知的墙上几天,谷歌搜索失败了。 最后,复制文件解决了这个问题(尽管还有其他选项,如硬链接)。

【讨论】:

    【解决方案5】:

    我今天遇到了类似的事情:当符号链接到文件并将行读入数组时,一些数字字符串会被分解成单独的列表元素??

    直接打开原始文件(而不是通过符号链接)时,此行为会消失:

    Original file -> Python list
    123456  ->  '1', '23', '456', ''
    123456789  ->  '1234567', '89'
    
    

    十六进制编辑器必须正确地遵循符号链接

    $ head rockyou.txt | xxd
    00000000: 3132 3334 3536 0a31 3233 3435 0a31 3233  123456.12345.123
    00000010: 3435 3637 3839 0a70 6173 7377 6f72 640a  456789.password.
    00000020: 696c 6f76 6579 6f75 0a70 7269 6e63 6573  iloveyou.princes
    00000030: 730a 3132 3334 3536 370a 726f 636b 796f  s.1234567.rockyo
    00000040: 750a 3132 3334 3536 3738 0a61 6263 3132  u.12345678.abc12
    00000050: 330a                                     3.
    $ head rockyou.txt.short | xxd
    00000000: 3132 3334 3536 0a31 3233 3435 0a31 3233  123456.12345.123
    00000010: 3435 3637 3839 0a70 6173 7377 6f72 640a  456789.password.
    00000020: 696c 6f76 6579 6f75 0a70 7269 6e63 6573  iloveyou.princes
    00000030: 730a 3132 3334 3536 370a 726f 636b 796f  s.1234567.rockyo
    00000040: 750a 3132 3334 3536 3738 0a61 6263 3132  u.12345678.abc12
    00000050: 330a                                     3.
    $ ls -lah rock*
    lrwxr-xr-x  1 u  g    17B Apr 16 10:56 rockyou.txt -> rockyou.txt.short
    -rw-r--r--  1 u  g   945B Apr 16 11:39 rockyou.txt.short
    

    MacOS Catalina、Python 3.7、3.8

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-19
      • 2013-09-19
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      相关资源
      最近更新 更多