簡體   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