在 Linux 上的 Python 中,我需要区分本地文件系统路径 à la /mnt/data/wherever/%Y-%m-%d.txt
或 ~/data/一方面是 bla.txt
,另一方面是 user@host1:/data/bla.txt
或 user@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/