小编xor*_*aul的帖子

使用Python获取特定的父文件夹

我已经有了这段功能正常的代码,但在写完之后,我确实感到尖叫的冲动"它还活着,它还活着!".

我想要做的是获取文件夹"modules"作为其父文件夹,例如从/ home/user/puppet/modules/impuls-test/templates/apache22 /我想/ home/user/puppet/modules/IMPULS测试/

我想出的是以下内容:

user@server:~/puppet/modules/impuls-test/templates/apache22$ python
Python 2.4.2 (#1, Apr 13 2007, 15:38:32)
[GCC 4.1.0 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> cwd = os.getcwd()
>>> path = cwd
>>> print "cwd: %s" % cwd
cwd: /home/user/puppet/modules/impuls-test/templates/apache22
>>> for i in xrange(len(cwd.split('/'))):
...     (head, tail) = os.path.split(path)
...     print "head: %s" % head
...     print "tail: %s" % tail
...     if tail == 'modules':
...         moduleDir = head …
Run Code Online (Sandbox Code Playgroud)

python directory path

2
推荐指数
2
解决办法
4233
查看次数

标签 统计

directory ×1

path ×1

python ×1