0

我有一个应用程序应该向用户显示文件的路径。假设用户输入/home/me/foo/,应用程序应该将其缩短为~/foo.

有没有办法在 Python 中做到这一点,以便它与 Linux 和 Windows 跨平台工作?

4

1 回答 1

1

现在确定您要做什么,但将其更改为其他格式很简单:

newpath = "~"+filepath[9:]

如果您希望它与其他用户名一起使用,

newpath = "~/"+filepath[findfirstindex('foo'):]
于 2019-09-20T16:34:18.733 回答