Lex*_*con 3 linux symlink python-3.x
在我的 Python3 程序中,我采用了一堆路径并根据它们的内容执行操作。当我评估以下符号链接(片段)时:
lrwxrwxrwx 1 513 513 5 Aug 19 10:56 console -> ttyS0
lrwxrwxrwx 1 513 513 11 Aug 19 10:56 core -> /proc/kcore
lrwxrwxrwx 1 513 513 13 Aug 19 10:56 fd -> /proc/self/fd
Run Code Online (Sandbox Code Playgroud)
结果是:
symlink console -> ttyS0
file core -> /proc/kcore
symlink console -> ttyS0
Run Code Online (Sandbox Code Playgroud)
它评估核心就像它是一个文件(相对于符号链接)。对我来说,将其评估为符号链接与文件的最佳方法是什么?下面的代码
#!/usr/bin/python3
import sys
import os
from pathlib import Path
def filetype(filein):
print(filein)
if Path(filein).is_file():
return "file"
if Path(filein).is_symlink():
return "symlink"
else:
return "doesn't match anything"
if __name__ == "__main__":
file = sys.argv[1]
print(str(file))
print(filetype(file))
Run Code Online (Sandbox Code Playgroud)
的结果is_file旨在回答“如果我打开这个名称,我会打开一个文件”这个问题。对于符号链接,如果目标是文件,则答案为“是”,因此返回值。
如果您想知道该名称是否是符号链接,请询问is_symlink。
| 归档时间: |
|
| 查看次数: |
2282 次 |
| 最近记录: |