繁体   English   中英

Python 无法打开符号链接文件

[英]Python can't open symlinked file

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

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

知道我做错了什么吗?

符号链接的目标不存在。

我不明白为什么我能够在问题的 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尝试解析链接,失败并显示失败信息。

...不要忘记使用完整路径。 在使用 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")

效果很好。

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

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

希望这对某人有帮助。

我有同样的问题-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 卡特琳娜、Python 3.7、3.8

暂无
暂无

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

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