Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个应用程序应该向用户显示文件的路径。假设用户输入/home/me/foo/,应用程序应该将其缩短为~/foo.
/home/me/foo/
~/foo
有没有办法在 Python 中做到这一点,以便它与 Linux 和 Windows 跨平台工作?
现在确定您要做什么,但将其更改为其他格式很简单:
newpath = "~"+filepath[9:]
如果您希望它与其他用户名一起使用,
newpath = "~/"+filepath[findfirstindex('foo'):]