我收到了我需要检查内部档案的途径清单。 当然,如果给我一个根基和一个子基点,就没有必要处理该子基。 例如
c: est // process this
c: estpics // do not process this
c: est2 // process this
我怎么能告诉(跨平台),一条道路不是另一个路子。 我最好希望这成为跨平台,只要它们不是周期性的联系,就不会担心这种联系(更糟糕的是,我最终将两次处理数据)。
我收到了我需要检查内部档案的途径清单。 当然,如果给我一个根基和一个子基点,就没有必要处理该子基。 例如
c: est // process this
c: estpics // do not process this
c: est2 // process this
我怎么能告诉(跨平台),一条道路不是另一个路子。 我最好希望这成为跨平台,只要它们不是周期性的联系,就不会担心这种联系(更糟糕的是,我最终将两次处理数据)。
我将保留你已经处理过的一套目录,然后对每条新途径进行检查,看看其母子是否已经在处理之前的表格中存在:
import os.path
visited = set()
for path in path_list:
head, tail = os.path.split(path)
while head and tail:
if head in visited:
break
head, tail = os.path.split(head)
else:
process(path)
visited.add(path)
注:path_list
应当进行分类,以便各子公司如果存在的话,总是在母系之后。
def is_subdir(path, directory):
path = os.path.realpath(path)
directory = os.path.realpath(directory)
relative = os.path.relpath(path, directory)
if relative.startswith(os.pardir):
return False
else:
return True
跟踪已经处理过的目录(按正常格式处理),如果你已经看到这些目录,则不再处理。 与此类似:
from os.path import realpath, normcase, sep
dirs = [r"C: est", r"C: estpics", r"C: est2"]
processed = []
for dir in dirs:
dir = normcase(realpath(dir)) + sep
if not any(dir.startswith(p) for p in processed):
processed.append(dir)
process(dir) # your code here
这里是“is_subdir
”的通用功能一号。
bytes
and str
, matching os.path
which also supports both).os.path.relpath
which will raise an exception on ms-windows if the paths are on different drives. (C:foo
-> D:ar
)法典:
def is_subdir(path, directory):
"""
Returns true if *path* in a subdirectory of *directory*.
"""
import os
from os.path import normpath, normcase, sep
path = normpath(normcase(path))
directory = normpath(normcase(directory))
if len(path) > len(directory):
sep = sep.encode( ascii ) if isinstance(directory, bytes) else sep
if path.startswith(directory.rstrip(sep) + sep):
return True
return False
这里使用的解决方案一以Andrew Clarks的回答为基础,确保清单的分类,使儿童处于父母的状态,并使用<代码>normpath和>normcase
,以固定连接c:users
和c:/users
的视窗。
def unique_path_roots(paths):
visited = set()
paths = list(set(paths))
for path in sorted(paths,key=cmp_to_key(locale.strcoll)):
path = normcase(normpath(realpath(path)))
head, tail = os.path.split(path)
while head and tail:
if head in visited:
break
head, tail = os.path.split(head)
else:
yield path
visited.add(path)
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ]="...