English 中文(简体)
2. 带甲型六氯环己烷的母子
原标题:Getting a specific parent folder with Python

我已经有了这一可操作的法典,但在撰写后,我确实感到,我敦促不要“活着,活着”。

我想做的是,有双倍的“模块”作为其父母的夹子,例如,从家庭/使用者/使用者/使用者/使用者/使用者/使用者/学生/测试/插手/项目/项目/项目/项目/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/方案/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划/计划 我想要/家/用户/pet/摩杜里/简便/测试。

我提出的是:

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 +  /modules/  + cwd.split( / )[i+2] +  / 
...         print "moduleDir: %s" % moduleDir
...         break
...     else:
...         path = head
...
head: /home/user/puppet/modules/impuls-test/templates
tail: apache22
head: /home/user/puppet/modules/impuls-test
tail: templates
head: /home/user/puppet/modules
tail: impuls-test
head: /home/user/puppet
tail: modules
moduleDir: /home/user/puppet/modules/impuls-test/

我获得目前的工作名录,并使用os.path.split。 直到单元到达时,时间已经很长。 利用正常的<代码>string.split功能,在笼罩上旋转,然后从原来的<代码>cwd.split( /)阵列向现任首长递交<代码>。

Can someone tell me a better/pythonic way to do this? Sure I can check if the current head ends with modules and then append the current tail, but that would only make the loop break faster and would still be ugly.

最佳回答
path = "/home/user/puppet/modules/impuls-test/templates"
components = path.split(os.sep)
print str.join(os.sep, components[:components.index("modules")+2])

印刷

/home/user/puppet/modules/impuls-test
问题回答

由于os虫病处理“......经营者”,你只能打上“......让规范周到的工作:

>>> path = "/home/user/puppet/modules/impuls-test/templates"
>>> os.path.normpath(os.path.join(path, ".."))
 /home/user/puppet/modules/impuls-test 

您可以定期发言。

>>> import re
>>> print re.findall(r (/.*?/modules/(.*?)/).* , /hello/modules/foo/bye )[0]
( /hello/modules/foo/ ,  foo )

类似Sven s(原始)wer,但更便捷,处理错误......

>>> import os
>>> cwd =  /home/user/puppet/modules/impuls-test/templates/apache22 
>>> directories = cwd.split(os.sep)
>>> try:
...     modules_depth = directories.index( modules )
...     print(os.sep.join(directories[:modules_depth + 2]))
... except ValueError:
...     print( Could not find "modules" )
... 
/home/user/puppet/modules/impuls-test




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签