python - 如何区分本地文件系统路径和SSH文件系统路径?

标签 python regex

在 Linux 上的 Python 中,我需要区分本地文件系统路径 à la /mnt/data/wherever/%Y-%m-%d.txt~/data/一方面是 bla.txt,另一方面是 user@host1:/data/bla.txtuser@host2.mydomain.com:blupp 的 SSH“路径”。 txt.

我怎样才能有效地做到这一点?

编辑:在我的示例中,没有文件 /mnt/data/wherever/%Y-%m-%d.txt,因为 %Y 等只是占位符。因此,检查给定名称的文件/目录是否存在将不起作用。

最佳答案

唯一的了解方法是使用启发式方法,因为 ssh 路径也是有效的文件名(尽管非常奇怪)。

来自 scp 文档:

File names may contain a user and host specification to indicate that the file is to be copied to/from that host. Local file names can be made explicit using absolute or relative pathnames to avoid scp treating file names containing ‘:’ as host specifiers.

因此,请检查它们是否以显式路径开头,或者在正确的位置中没有冒号。然后假设它是本地路径,否则假设它是远程路径。

示例:

pattern = '(\.?/|/)|(^:?[^:]*$)'
re.match(pattern, ":home/test") # match, is a local path
re.match(pattern, "user@host:blah") # no match

关于python - 如何区分本地文件系统路径和SSH文件系统路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429705/

相关文章:

regex - 如何用一个有效的命令在OpenWrt上安装所有iptables相关的软件包?

Java Regex递归获取特定单词前后的11个单词

python - 两个整数的二进制表示的统一重组

python - 用泊松函数拟合直方图

python - '{ :08b}' . format(i) 等效于 Python 2.x

javascript - 性能 - String.charAt(0) 与/^.{1}/

regex - linux bash 字符串替换

python - Keras,在每个时期获得一层的输出

python - 从 python 检查 aerospike set 是否为空的最简单方法是什么?

javascript - 正则表达式不返回预期值