本文整理汇总了Python中posixpath.relpath函数的典型用法代码示例。如果您正苦于以下问题:Python relpath函数的具体用法?Python relpath怎么用?Python relpath使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了relpath函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
chapter_files, other_files = get_filenames()
# make previous of first file and next of last file to just bring
# back to README
prevs = ['README.md'] + chapter_files[:-1]
nexts = chapter_files[1:] + ['README.md']
print("Chapter files:")
for prevpath, thispath, nextpath in zip(prevs, chapter_files, nexts):
# all paths should be like 'section/file.md'
where = posixpath.dirname(thispath)
prev = posixpath.relpath(prevpath, where)
next_ = posixpath.relpath(nextpath, where)
extralinks = "[Previous](%s) | [Next](%s) |\n" % (prev, next_)
end = END_TEMPLATE.format(
toplevel='..', extralinks=extralinks, readmeheader=where)
update_end(thispath, end)
print()
print("Other files:")
for filename in other_files:
where = posixpath.dirname(filename)
end = END_TEMPLATE.format(
toplevel=posixpath.relpath('.', where),
extralinks="", readmeheader='list-of-contents')
update_end(filename, end)
示例2: __iter__
def __iter__(self):
for item in self.previous:
yield item
cwd = os.getcwd()
yield {self.pathkey: posixpath.sep,
self.typekey: self.foldertype}
try:
os.chdir(self.dirname)
for (dirpath, dirnames, filenames) in os.walk(os.curdir):
os.chdir(cwd)
# Convert path from os.path to posixpath
dirpath = posixpath.join(*pathsplit(dirpath, ospath=os.path))
def sortkey(basename, dirpath=dirpath, sortkey=self.sortkey):
return sortkey({}, dirpath=dirpath, basename=basename)
for basename in sorted(filenames, key=sortkey):
yield {self.pathkey: posixpath.relpath(
posixpath.join(posixpath.sep, dirpath, basename),
posixpath.sep)}
for basename in sorted(dirnames, key=sortkey):
yield {
self.pathkey: posixpath.relpath(
posixpath.join(posixpath.sep, dirpath, basename),
posixpath.sep) + posixpath.sep,
self.typekey: self.foldertype}
os.chdir(self.dirname)
finally:
os.chdir(cwd)
示例3: scan
def scan(self):
if not self.fs:
self.attachFileSystem(self.basepath)
self._dirs = dict()
self._files = dict()
for root, _dirs, _files in self.fs.walk(self.fs.basepath):
for _dir in _dirs:
path = os.path.join(root, _dir).replace("\\", "/")
stat = self.fs.stat(path)
path = posixpath.relpath(path, self.fs.basepath)
self._dirs[path] = {
"size": stat.st_size,
"mdate": stat.st_mtime
}
for _file in _files:
path = os.path.join(root, _file).replace("\\", "/")
stat = self.fs.stat(path)
path = posixpath.relpath(path, self.fs.basepath)
self._files[path] = {
"size": stat.st_size,
"mdate": stat.st_mtime
}
return self
示例4: run
def run(self):
# MOE needs to be invoked from the directory containing the SVL scripts, otherwise it can't seem to
# "find" the other SVL files. It would be nice to eliminate this issue and use absolute paths.
#lex = shlex.shlex(
# 'moe -load "{script}" -exec "HomologyBatch [\'{input}\']"'.format(
# script=self.args['svl_script_name'], # The file will be accessed from the parent dir.
# # MOE only accepts POSIX-like file paths as SVL function arguments.
# input=posixpath.relpath(self.args['input_directory'], start=self.args['svl_directory'])
# )
#)
#lex.whitespace_split = True
#process_args = list(lex)
#check_call(process_args, stdout=PIPE, cwd=self.args['svl_directory'])
process_args = 'moebatch -run "{script}" -options "{options}" -template "{template}" -sequence "{sequence}" -out "{outDir}"'.format(
script=self.args['svl_script_name'], # The file will be accessed from the parent dir.
options=posixpath.relpath(self.args['homology_options'], start=self.args['svl_directory']),
template=posixpath.relpath(self.args['template_file'], start=self.args['svl_directory']),
sequence=posixpath.relpath(self.args['sequence_file'], start=self.args['svl_directory']),
outDir=posixpath.relpath(self.args['outputDir'], start=self.args['svl_directory'])
)
try:
# This script currently outputs the homology model files in the directory where it was invoked.
# Call the script from the output directory.
check_call(process_args, stdout=PIPE, shell=True, cwd=self.args['svl_directory'])
except CalledProcessError as e:
# For some reason, moebatch seems to always return 1.
if e.returncode != 1: # Ignore a return code of 1.
raise e
示例5: diff
def diff(self, relative = False):
(d, f) = self.scan()
ef = f - self.__of
mf = self.__of - f
ed = d - self.__od
md = self.__od - d
if relative:
ef = set([posixpath.relpath(x, self.__path) for x in ef])
mf = set([posixpath.relpath(x, self.__path) for x in mf])
ed = set([posixpath.relpath(x, self.__path) for x in ed])
md = set([posixpath.relpath(x, self.__path) for x in md])
return ed, ef, md, mf
示例6: lookup_redirect
def lookup_redirect(url):
sub_url = url
for sub_url, _ in Segment(url):
for base, filename in Segment(sub_url):
try:
redirects = self._cache.GetFromFile(
posixpath.normpath(posixpath.join(base, "redirects.json"))
).Get()
except FileNotFoundError:
continue
redirect = redirects.get(posixpath.join(filename, "..."))
if redirect is None:
continue
redirect = Join(base, redirect.rstrip("..."))
# Avoid infinite redirection loops by breaking if seen before.
if redirect in seen_redirects:
break
seen_redirects.add(redirect)
return lookup_redirect(Join(redirect, posixpath.relpath(url, sub_url)))
return url
示例7: get_relative_url
def get_relative_url(destination, source):
"""Get relative URL between two sources.
http://stackoverflow.com/a/7469668/315168
:param destination:
:param source:
:return: tuple (is same domain, relative url)
"""
u_dest = urlparse.urlsplit(destination)
u_src = urlparse.urlsplit(source)
_uc1 = urlparse.urlunsplit(u_dest[:2]+tuple('' for i in range(3)))
_uc2 = urlparse.urlunsplit(u_src[:2]+tuple('' for i in range(3)))
if _uc1 != _uc2:
## This is a different domain
return False, destination
# If there is no / component in url assume it's root path
src_path = u_src.path or "/"
_relpath = posixpath.relpath(u_dest.path, posixpath.dirname(src_path))
return True, _relpath
# return True, urlparse.urlunsplit(('', '', _relpath, u_dest.query, u_dest.fragment))
示例8: make_relative
def make_relative(self, url):
"""
Given a URL path return it as a relative URL,
given the context of the current page.
"""
suffix = '/' if (url.endswith('/') and len(url) > 1) else ''
return posixpath.relpath(url, start=self.base_path) + suffix
示例9: _apply_regex_rule
def _apply_regex_rule(self, rule_name, rule, cat_name, cat_path, settings):
accepted_flags = {
'a': re.ASCII,
'i': re.IGNORECASE,
'l': re.LOCALE,
'x': re.VERBOSE
}
flags = sum([accepted_flags[f] for f in rule.get('flags', [])])
pattern = None
try:
pattern = re.compile(rule['pattern'], flags)
except KeyError:
raise InvalidRegexFilter(cat_name, rule_name)
actions = []
rename = rule.get('rename', None)
for root, dirs, files in os.walk(self._path):
if posixpath.abspath(root) == self._repo_path:
continue
for file_name in files:
file_name = posixpath.relpath(posixpath.join(root, file_name), self._path)
match = pattern.match(file_name)
if match:
new_name = file_name
if rename:
new_name = rename.format(**match.groupdict())
new_name = posixpath.join(cat_path, new_name)
actions.append(('mv', posixpath.join(self._path, file_name), new_name))
return actions
示例10: get_path_components
def get_path_components(self, relative_to=None):
if relative_to:
if not isinstance(relative_to, FTPFile):
raise ValueError("relative_to must be another FTPFile "
"instance")
return posixpath.relpath(self._path, relative_to._path).split("/")
return self._path.split("/")
示例11: makeNodesRelative
def makeNodesRelative(nodes, knobTypes):
result = { 'warnings': [], 'replacements': [], 'projectFolder': None}
projectfile = nuke.root()['name'].value()
if projectfile =="":
result['warnings'].append('Please save the nuke script before running this function such that it has a valid path.')
return result
projectFolderAbsolute = posixpath.dirname(projectfile)
result['projectFolder'] = projectFolderAbsolute
projectFolderRelative = "[file dirname [value root.name]]"
for n in nodes:
for k in knobTypes:
if n.knob(k):
originalFilePath = n[k].value()
if n[k].isAnimated():
result['warnings'].append("Didn't replace "+k+' of node '+n['name'].value()+' since the knob is animated')
elif n[k].hasExpression():
result['warnings'].append("Didn't replace "+k+' of node '+n['name'].value()+' since the knob has an expression')
elif originalFilePath.strip()=="":
#result['warnings'].append("Didn't replace "+k+' of node '+n['name'].value()+' since it is empty')
pass
elif originalFilePath.startswith(projectFolderRelative):
result['warnings'].append("Didn't replace "+k+' of node '+n['name'].value()+' since it is already a relative path:\n'+ __removePrefix(originalFilePath,projectFolderRelative))
else:
relativeFilePath = posixpath.relpath(originalFilePath,projectFolderAbsolute)
n[k].setValue(projectFolderRelative + '/' +relativeFilePath)
result['replacements'].append(k+' of '+ n['name'].value()+':\n'+relativeFilePath)
return result
示例12: get_jottapath
def get_jottapath(localtopdir, dirpath, jottamountpoint):
"""Translate localtopdir to jottapath. Returns unicode string"""
log.debug("get_jottapath %r %r %r", localtopdir, dirpath, jottamountpoint)
normpath = posixpath.normpath(
posixpath.join(jottamountpoint, posixpath.basename(localtopdir), posixpath.relpath(dirpath, localtopdir))
)
return _decode_filename_to_unicode(normpath)
示例13: _get_relative_path
def _get_relative_path(a, b):
"""
returns a relative path for navigation from dir *a* to dir *b*
if the common parent of both is "/", return an absolute path
"""
a += "/"
b += "/"
parent = posixpath.dirname(posixpath.commonprefix([a,b]))
if parent == "/": return b[:-1]
a = posixpath.relpath(a, parent)
b = posixpath.relpath(b, parent)
if a == ".": return b
return posixpath.normpath("../" * (a.count("/")+1) + b)
示例14: get_url
def get_url(current, target):
current = urlparse(current).path
target = urlparse(target).path
result = posixpath.relpath(target, current).split('/')
result = '/'.join(result[1:])
return result
示例15: recursive_copy
def recursive_copy(sourcefs, targetfs, sourcepath, targetpath):
#normalise paths
norm_sourcepath = posixpath.normpath(sourcepath)
norm_targetpath = posixpath.normpath(targetpath)
#Create the first directory in the target file system if it does not already exist
source_end_path = posixpath.split(posixpath.normpath(sourcepath))[1]
#if the target path exists, make a new directory into the target path directory
if targetfs.exists(norm_targetpath):
base_target_path = posixpath.normpath(posixpath.join(norm_targetpath, source_end_path))
#if the target does not exist but its parent does, rename the directory and copy
elif targetfs.exists(posixpath.normpath(posixpath.join(norm_targetpath, ".."))):
#If it does not exist, create that directory
base_target_path = norm_targetpath
else:
raise IOError("Cannot copy into target: "+targetpath)
if not targetfs.exists(base_target_path):
targetfs.mkdir(base_target_path)
for (path, directories, files) in sourcefs.walk(norm_sourcepath):
rel_source_path = posixpath.relpath(path, norm_sourcepath)
new_target_path = posixpath.normpath(posixpath.join(base_target_path, rel_source_path))
print new_target_path
for f in files:
copy_file_into_directory(sourcefs, targetfs, posixpath.join(path, f), new_target_path)
for d in directories:
new_directory = posixpath.join(new_target_path, d)
if not targetfs.exists(new_directory):
targetfs.mkdir(new_directory)