python - 使用 Python 缩短文件路径
问题描述
我有一个应用程序应该向用户显示文件的路径。假设用户输入/home/me/foo/
,应用程序应该将其缩短为~/foo
.
有没有办法在 Python 中做到这一点,以便它与 Linux 和 Windows 跨平台工作?
解决方案
现在确定您要做什么,但将其更改为其他格式很简单:
newpath = "~"+filepath[9:]
如果您希望它与其他用户名一起使用,
newpath = "~/"+filepath[findfirstindex('foo'):]
推荐阅读
- flutter - 推后如何在脚手架树中弹出抽屉
- cakephp - CakePHP 教程错误:对 /users 的请求没有应用任何授权检查
- html - Vuetify DataTable - 垂直对齐单元格内容
- python - Python Azure 事件中心使用事件属性
- css - 符合内容大小的网格框
- c# - 在 System.Windows.Data.ListCollectionView 中导航项目时,CurrentPosition 和当前索引不同步
- function - 尝试在 Kotlin 类中使用 if 函数
- python - Python - 通过键数组更改嵌套值
- c - 在 C 中声明结构后的变量是什么意思?
- c++ - 致命错误:模板实例化深度超过最大值 900