我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用os.path.rfind()。
def __mimetype(self, path): """Detect mimetype of file""" mime = mimetypes.guess_type(path)[0] or 'unknown' ext = path[path.rfind('.') + 1:] if mime == 'unknown' and ('.' + ext) in mimetypes.types_map: mime = mimetypes.types_map['.' + ext] if mime == 'text/plain' and ext == 'pl': mime = self._mimeType[ext] if mime == 'application/vnd.ms-office' and ext == 'doc': mime = self._mimeType[ext] if mime == 'unknown': if os.path.basename(path) in ['README', 'ChangeLog']: mime = 'text/plain' else: if ext in self._mimeType: mime = self._mimeType[ext] # self.__debug('mime ' + os.path.basename(path), ext + ' ' + mime) return mime
def execute(self): cf = self.fm.thisfile path = cf.relative_path.replace("%", "%%") if path.find('.') != 0 and path.rfind('.') != -1 and not cf.is_directory: self.fm.open_console('rename ' + path, position=(7 + path.rfind('.'))) else: self.fm.open_console('rename ' + path)
def pyconstruct(s): """Constructs a Python object from a constructor, an expression of the form x.y.z.name(args). This ensures that x.y.z is imported. In the future, more forms of syntax may be accepted.""" env = {} if "(" not in s: s += "()" path = s[:s.find("(")] if "." in path: module = path[:path.rfind(".")] print("import", module) exec "import "+module in env return eval(s,env)
def idFromDir(self, path): assert len(path) > len(self.baseDir) intId = path[1 + path.rfind('#') : ] pathId = path[len(self.baseDir) + 1 : ] return (intId, pathId)
def GetPath(self, path): if path in self.tree: return self.tree[path] i = path.rfind('/') if i == -1: parent = self.tree[''] branch = Branch(path, parent) else: parent = self.GetPath(path[:i]) branch = Branch(path[i+1:], parent) parent.Add(branch) self.tree[path] = branch return branch
def test_get_anchorhub_path_directory(): """ getanchorhubpath.py: Make sure directory given is named 'anchorhub' """ path = get_anchorhub_path() dir = path[path.rfind(get_path_separator()) + 1:] assert dir == 'anchorhub'
def clean_shot_list(self,path): data = loadtxt(path) ending_idx = path.rfind('.') new_path = append_to_filename(path,'_clear') if len(shape(data)) < 2: #nondisruptive nd_times = -1.0*ones_like(data) data_two_column = vstack((data,nd_times)).transpose() savetxt(new_path,data_two_column,fmt = '%d %f') print('created new file: {}'.format(new_path)) print('deleting old file: {}'.format(path)) os.remove(path)
def append_to_filename(path,to_append): ending_idx = path.rfind('.') new_path = path[:ending_idx] + to_append + path[ending_idx:] return new_path
def __uniqueName(self, path, copy = ' copy'): """Generate unique name for file copied file""" curDir = os.path.dirname(path) curName = os.path.basename(path) lastDot = curName.rfind('.') ext = newName = '' if not os.path.isdir(path) and re.search(r'\..{3}\.(gz|bz|bz2)$', curName): pos = -7 if curName[-1:] == '2': pos -= 1 ext = curName[pos:] oldName = curName[0:pos] newName = oldName + copy elif os.path.isdir(path) or lastDot <= 0: oldName = curName newName = oldName + copy pass else: ext = curName[lastDot:] oldName = curName[0:lastDot] newName = oldName + copy pos = 0 if oldName[-len(copy):] == copy: newName = oldName elif re.search(r'' + copy +'\s\d+$', oldName): pos = oldName.rfind(copy) + len(copy) newName = oldName[0:pos] else: newPath = os.path.join(curDir, newName + ext) if not os.path.exists(newPath): return newPath # if we are here then copy already exists or making copy of copy # we will make new indexed copy *black magic* idx = 1 if pos > 0: idx = int(oldName[pos:]) while True: idx += 1 newNameExt = newName + ' ' + str(idx) + ext newPath = os.path.join(curDir, newNameExt) if not os.path.exists(newPath): return newPath # if idx >= 1000: break # possible loop return