繁体   English   中英

使用 Python 缩短文件路径

[英]Shortening file paths using Python

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

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

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

newpath = "~"+filepath[9:]

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

newpath = "~/"+filepath[findfirstindex('foo'):]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM