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.
我有一个字符串看起来像这样
urse/project/kb/use.js
从这个字符串我需要得到
urse/project/kb/
use.js名称将始终更改,除了.js.
use.js
.js
或者删除字符串直到/从最后的第一个?
/
Python 有一个专门的模块来处理文件路径:
In [13]: os.path.dirname('urse/project/kb/use.js') Out[13]: 'urse/project/kb'
根据需要附加尾随os.sep(或者,更好的是,坚持使用os.path模块来操作路径)。
os.sep
os.path
>>> s = 'urse/project/kb/use.js' >>> s[:s.rfind('/') + 1] 'urse/project/kb/'