Python 无法打开符号链接文件

Python can't open symlinked file

Python 无法打开我的模拟链接文件。我确定该文件存在并且我可以访问它。我的印象是符号链接在 OS 级别解析,因此 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'
>>>

知道我做错了什么吗?非常感谢帮助。

Any ideas what I am doing wrong?

符号 link 的目标不存在。

I don't understand why I was able to resolve it in the ls statement in my question.

你不是。

默认情况下,ls 命令在 link 本身上运行,而不是在 link 的目标上运行。缺少 -L 选项,ls 永远不会尝试解析符号 link.

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

$ 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 是一个 link 文件,包含指向其目标路径的三个字节:'./a'ls 能够描述 link 的属性 而无需取消引用 link 本身。

相反,使用-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 中的 link,并显示有关 linked-to 文件的信息。对于 -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 是一个 link 解析为不再存在的文件。由于 ls -l 从不尝试解析 link,其输出与之前的测试没有变化。 (b 是一个 link 文件,3 个字节长,内容 './a'。)

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

...不要忘记使用完整路径。在 Raspberry Pi 和 Python 3.7.3 上,我必须包含工作 link:

的整个路径
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")

效果不错。

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

尝试通过 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

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

Python for windows 无法跟随 Ubuntu (WSL 2) 符号链接(或者,至少它无法打开Ubuntu 符号链接回到 windows 文件。

希望这对某人有所帮助。

我遇到了同样的问题 - Fedora 32 系统,Jupyter 的笔记本文件夹包含我的文件,这些文件具有指向 json 文件的符号链接。我用头撞了几天众所周知的墙,谷歌搜索很短。 最后,复制文件解决了这个问题(尽管还有其他选项,如硬链接)。

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

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

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 卡特琳娜,Python3.7、3.8