在我的Linux(CentOS 6)系统上,我安装了一个sshfs网络共享,例如/data/remote
。我可以通过bash自由创建符号链接
ln -s /data/remote/source /data/remote/destination
但是下面的python代码失败了
# test_os_link.py
import os
if not os.path.exists('/data/remote/destination'):
os.link('/data/remote/source', '/data/remote/destination')
失败的原因
Traceback (most recent call last):
File "test_os_link.py", line 2, in <module>
os.link('/data/remote/source', '/data/remote/destination')
OSError: [Errno 38] Function not implemented
sshfs安装为
sshfs -o follow_symlinks user@host:/data/remote /data/remote
如您所见,目录结构在远程和本地系统上是相同的,因此
/data/remote
中的符号链接在这两个系统上都是有效的。strace报告如下:
stat("test_os_link.py", {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
open("test_os_link.py", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff7240bf10) = -1 ENOTTY (Inappropriate ioctl for device)
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c6080000
lseek(3, 0, SEEK_CUR) = 0
read(3, "import os\nos.link(\"/data/remote/dest"..., 4096) = 167
lseek(3, 167, SEEK_SET) = 167
brk(0x1e64000) = 0x1e64000
read(3, "", 4096) = 0
close(3) = 0
munmap(0x7f48c6080000, 4096) = 0
mmap(NULL, 200704, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c6050000
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c600f000
brk(0x1e85000) = 0x1e85000
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c5fce000
link("/data/remote/source", "/data/remote/destination") = -1 ENOSYS (Function not implemented)
write(2, "Traceback (most recent call last"..., 35Traceback (most recent call last):
) = 35
write(2, " File \"test_os_link.py\", line 2"..., 46 File "test_os_link.py", line 2, in <module>
) = 46
open("test_os_link.py", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=167, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f48c5fcd000
read(3, "import os\nos.link(\"/data/remote/dest"..., 4096) = 167
write(2, " ", 4 ) = 4
write(2, "os.link(\"/data/remote/source"..., 79os.link("/data/remote/source","/data/remote/destination")
) = 79
close(3) = 0
munmap(0x7f48c5fcd000, 4096) = 0
write(2, "OSError", 7OSError) = 7
write(2, ": ", 2: ) = 2
最佳答案
os.link
创建不受所有文件系统支持的硬链接。
使用os.symlink
创建符号链接。这相当于ln -s
。