本文整理匯總了Python中os.path.split方法的典型用法代碼示例。如果您正苦於以下問題:Python path.split方法的具體用法?Python path.split怎麽用?Python path.split使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類os.path
的用法示例。
在下文中一共展示了path.split方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _walk_moler_python_files
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _walk_moler_python_files(path, *args):
"""
Walk thru directory with commands and search for python source code (except __init__.py)
Yield relative filepath to parameter path
:param path: relative path do directory with commands
:type path:
:rtype: str
"""
repo_path = abspath(join(path, '..', '..'))
observer = "event" if "events" in split(path) else "command"
print("Processing {}s test from path: '{}'".format(observer, path))
for (dirpath, _, filenames) in walk(path):
for filename in filenames:
if filename.endswith('__init__.py'):
continue
if filename.endswith('.py'):
abs_path = join(dirpath, filename)
in_moler_path = relpath(abs_path, repo_path)
yield in_moler_path
示例2: load_data_set
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def load_data_set(index):
id2file = dict()
f=open(index_file)
for line in f.readlines():
id, text_file = line.strip().split("\t")
id2file[id] = text_file
texts = []
all_sentences = []
for item in index:
file = TEXT_DIR+id2file[item]
text = open(file).read()
sentences = text.split("\n")
clean_sentences = [clean_str(s).split(" ") for s in sentences]
all_sentences.extend(clean_sentences)
clean_words = [word for s in clean_sentences for word in s]
texts.append(clean_words)
return texts, all_sentences
示例3: filesFromPathsUrls
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def filesFromPathsUrls(paths):
"""
Takes paths or URLs and yields file (path, fileName, file) tuples for
them
"""
for path in paths:
if '://' in path:
r = requests.get(path)
if not r.status_code < 300: #TODO: Make this better..., should only accept success
raise RuntimeError(f'Could not get file {path}, HTTP {r.status_code}')
fileName = path.split('?')[0]
fileName = fileName.split('/')[-1]
fileLike = io.StringIO(r.text)
fileLike.name = path
yield (path, fileName, fileLike)
else:
globPaths = glob.glob(path, recursive=True)
if not globPaths:
raise RuntimeError(f'No file found for glob {path}')
for path in globPaths:
with open(path, "r", encoding="utf-8") as file:
yield (path, os.path.basename(path), file)
示例4: __init__
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def __init__(self, root_dir, split_name, transform):
super(ISSDataset, self).__init__()
self.root_dir = root_dir
self.split_name = split_name
self.transform = transform
# Folders
self._img_dir = path.join(root_dir, ISSDataset._IMG_DIR)
self._msk_dir = path.join(root_dir, ISSDataset._MSK_DIR)
self._lst_dir = path.join(root_dir, ISSDataset._LST_DIR)
for d in self._img_dir, self._msk_dir, self._lst_dir:
if not path.isdir(d):
raise IOError("Dataset sub-folder {} does not exist".format(d))
# Load meta-data and split
self._meta, self._images = self._load_split()
示例5: save_prediction_raw
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def save_prediction_raw(raw_pred, _, img_info, out_dir):
# Prepare folders and paths
folder, img_name = path.split(img_info["rel_path"])
img_name, _ = path.splitext(img_name)
out_dir = path.join(out_dir, folder)
ensure_dir(out_dir)
out_path = path.join(out_dir, img_name + ".pth.tar")
out_data = {
"sem_pred": raw_pred[0],
"bbx_pred": raw_pred[1],
"cls_pred": raw_pred[2],
"obj_pred": raw_pred[3],
"msk_pred": raw_pred[4]
}
torch.save(out_data, out_path)
示例6: _walk_moler_commands
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _walk_moler_commands(path, base_class):
for fname in _walk_moler_python_files(path=path):
pkg_name = fname.replace(".py", "")
parts = pkg_name.split(sep)
pkg_name = ".".join(parts)
moler_module = import_module(pkg_name)
for _, cls in moler_module.__dict__.items():
if not isinstance(cls, type):
continue
if not issubclass(cls, base_class):
continue
module_of_class = cls.__dict__['__module__']
# take only Commands
# take only the ones defined in given file (not imported ones)
if (cls != base_class) and (module_of_class == pkg_name):
yield moler_module, cls
示例7: run_script
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def run_script(self, script_name, namespace):
script = 'scripts/' + script_name
if not self.has_metadata(script):
raise ResolutionError("No script named %r" % script_name)
script_text = self.get_metadata(script).replace('\r\n', '\n')
script_text = script_text.replace('\r', '\n')
script_filename = self._fn(self.egg_info, script)
namespace['__file__'] = script_filename
if os.path.exists(script_filename):
source = open(script_filename).read()
code = compile(source, script_filename, 'exec')
exec(code, namespace, namespace)
else:
from linecache import cache
cache[script_filename] = (
len(script_text), 0, script_text.split('\n'), script_filename
)
script_code = compile(script_text, script_filename, 'exec')
exec(script_code, namespace, namespace)
示例8: _index
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _index(self):
try:
return self._dirindex
except AttributeError:
ind = {}
for path in self.zipinfo:
parts = path.split(os.sep)
while parts:
parent = os.sep.join(parts[:-1])
if parent in ind:
ind[parent].append(parts[-1])
break
else:
ind[parent] = [parts.pop()]
self._dirindex = ind
return ind
示例9: _by_version_descending
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _by_version_descending(names):
"""
Given a list of filenames, return them in descending order
by version number.
>>> names = 'bar', 'foo', 'Python-2.7.10.egg', 'Python-2.7.2.egg'
>>> _by_version_descending(names)
['Python-2.7.10.egg', 'Python-2.7.2.egg', 'foo', 'bar']
>>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.egg'
>>> _by_version_descending(names)
['Setuptools-1.2.3.egg', 'Setuptools-1.2.3b1.egg']
>>> names = 'Setuptools-1.2.3b1.egg', 'Setuptools-1.2.3.post1.egg'
>>> _by_version_descending(names)
['Setuptools-1.2.3.post1.egg', 'Setuptools-1.2.3b1.egg']
"""
def _by_version(name):
"""
Parse each component of the filename
"""
name, ext = os.path.splitext(name)
parts = itertools.chain(name.split('-'), [ext])
return [packaging.version.parse(part) for part in parts]
return sorted(names, key=_by_version, reverse=True)
示例10: parse
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def parse(cls, src, dist=None):
"""Parse a single entry point from string `src`
Entry point syntax follows the form::
name = some.module:some.attr [extra1, extra2]
The entry name and module name are required, but the ``:attrs`` and
``[extras]`` parts are optional
"""
m = cls.pattern.match(src)
if not m:
msg = "EntryPoint must be in 'name=module:attrs [extras]' format"
raise ValueError(msg, src)
res = m.groupdict()
extras = cls._parse_extras(res['extras'])
attrs = res['attr'].split('.') if res['attr'] else ()
return cls(res['name'], res['module'], attrs, extras, dist)
示例11: _dep_map
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _dep_map(self):
try:
return self.__dep_map
except AttributeError:
dm = self.__dep_map = {None: []}
for name in 'requires.txt', 'depends.txt':
for extra, reqs in split_sections(self._get_metadata(name)):
if extra:
if ':' in extra:
extra, marker = extra.split(':', 1)
if invalid_marker(marker):
# XXX warn
reqs = []
elif not evaluate_marker(marker):
reqs = []
extra = safe_extra(extra) or None
dm.setdefault(extra, []).extend(parse_requirements(reqs))
return dm
示例12: load_perturbed_image
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def load_perturbed_image(d):
in_file = d['path']
seed = "%d-%d" % (random_seed, d['row_number'])
out_file = (out_file_base + in_file).replace(".jpeg", "-perturbed-%s.jpeg" % seed)
base_dir = split(out_file)[0]
if not os.path.exists(base_dir):
os.makedirs(base_dir)
if not os.path.exists(out_file):
try:
subprocess.check_call('./make_perturbed_image.sh %s %s %s' % (in_file, out_file, seed), shell=True)
except Exception:
pass
return attempt_image_load(in_file, out_file)
示例13: main
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def main():
logging.basicConfig(level=logging.INFO)
parsed_args = get_args()
output_path = parsed_args.output_path
# If no output path is given, default to saving it in the first logdir under a fixed name
if output_path is None:
if len(parsed_args.logdir) > 1:
raise ValueError("Must specify --output_path when using multiple log directories.")
output_path = os.path.join(parsed_args.logdir[0], 'highest_win_policies_and_rates.json')
for logdir in parsed_args.logdir:
if 'multi_train' not in logdir.split(os.path.sep):
logger.warning(f"logdir '{logdir}' does not contain 'multi_train'."
"Falling back to absolute paths, JSON may not be portable.")
logger.info(f"Output path: {output_path}")
logger.info(f"Log dir: {parsed_args.logdir}")
with open(output_path, 'w') as f: # fail fast if output_path inaccessible
result = find_best(parsed_args.logdir, parsed_args.episode_window)
json.dump(result, f)
示例14: run_script
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def run_script(self, script_name, namespace):
script = 'scripts/'+script_name
if not self.has_metadata(script):
raise ResolutionError("No script named %r" % script_name)
script_text = self.get_metadata(script).replace('\r\n', '\n')
script_text = script_text.replace('\r', '\n')
script_filename = self._fn(self.egg_info, script)
namespace['__file__'] = script_filename
if os.path.exists(script_filename):
source = open(script_filename).read()
code = compile(source, script_filename, 'exec')
exec(code, namespace, namespace)
else:
from linecache import cache
cache[script_filename] = (
len(script_text), 0, script_text.split('\n'), script_filename
)
script_code = compile(script_text, script_filename,'exec')
exec(script_code, namespace, namespace)
示例15: _rebuild_mod_path
# 需要導入模塊: from os import path [as 別名]
# 或者: from os.path import split [as 別名]
def _rebuild_mod_path(orig_path, package_name, module):
"""
Rebuild module.__path__ ensuring that all entries are ordered
corresponding to their sys.path order
"""
sys_path = [_normalize_cached(p) for p in sys.path]
def position_in_sys_path(path):
"""
Return the ordinal of the path based on its position in sys.path
"""
path_parts = path.split(os.sep)
module_parts = package_name.count('.') + 1
parts = path_parts[:-module_parts]
return sys_path.index(_normalize_cached(os.sep.join(parts)))
orig_path.sort(key=position_in_sys_path)
module.__path__[:] = [_normalize_cached(p) for p in orig_path]