本文整理汇总了Python中os.path.strip方法的典型用法代码示例。如果您正苦于以下问题:Python path.strip方法的具体用法?Python path.strip怎么用?Python path.strip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.path
的用法示例。
在下文中一共展示了path.strip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: action_upload
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def action_upload(self, courseid, taskid, path, fileobj):
""" Upload a file """
# the path is given by the user. Let's normalize it
path = path.strip()
if not path.startswith("/"):
path = "/" + path
wanted_path = self.verify_path(courseid, taskid, path, True)
if wanted_path is None:
return self.show_tab_file(courseid, taskid, _("Invalid new path"))
task_fs = self.task_factory.get_task_fs(courseid, taskid)
try:
task_fs.put(wanted_path, fileobj.file.read())
except:
return self.show_tab_file(courseid, taskid, _("An error occurred while writing the file"))
return self.show_tab_file(courseid, taskid)
示例2: get_sources
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def get_sources(def_file):
sources = []
files = []
visited = set()
mxnet_path = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))
for line in open(def_file):
files = files + line.strip().split(' ')
for f in files:
f = f.strip()
if not f or f.endswith('.o:') or f == '\\': continue
f = os.path.realpath(f)
fn = os.path.relpath(f)
if f.startswith(mxnet_path) and fn not in visited:
sources.append(fn)
visited.add(fn)
return sources
示例3: get_problem_hashes
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def get_problem_hashes():
"""
Function: get_problem_hashes
Summary: Walking from each problem and return a tuple
(problem_name, hash_content)
Returns: list of tuples <problem_name: string, hash_content: string>
"""
hash_pattern = re.compile("./Problem[0-9]{3}")
hashes = {}
for file_tuple in os.walk("."):
if hash_pattern.match(file_tuple[0]) and ".hash" in file_tuple[-1]:
problem = file_tuple[0]
hash_path = path.join(problem, '.hash')
hash_content = read_hashfile(hash_path)
hashes[problem.strip('./')] = hash_content
return hashes
示例4: which
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
#----------------------------------------------------------------------
示例5: action_create
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def action_create(self, courseid, taskid, path):
""" Delete a file or a directory """
# the path is given by the user. Let's normalize it
path = path.strip()
if not path.startswith("/"):
path = "/" + path
want_directory = path.endswith("/")
wanted_path = self.verify_path(courseid, taskid, path, True)
if wanted_path is None:
return self.show_tab_file(courseid, taskid, _("Invalid new path"))
task_fs = self.task_factory.get_task_fs(courseid, taskid)
if want_directory:
task_fs.from_subfolder(wanted_path).ensure_exists()
else:
task_fs.put(wanted_path, b"")
return self.show_tab_file(courseid, taskid)
示例6: action_rename
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def action_rename(self, courseid, taskid, path, new_path):
""" Delete a file or a directory """
# normalize
path = path.strip()
new_path = new_path.strip()
if not path.startswith("/"):
path = "/" + path
if not new_path.startswith("/"):
new_path = "/" + new_path
old_path = self.verify_path(courseid, taskid, path)
if old_path is None:
return self.show_tab_file(courseid, taskid, _("Internal error"))
wanted_path = self.verify_path(courseid, taskid, new_path, True)
if wanted_path is None:
return self.show_tab_file(courseid, taskid, _("Invalid new path"))
try:
self.task_factory.get_task_fs(courseid, taskid).move(old_path, wanted_path)
return self.show_tab_file(courseid, taskid)
except:
return self.show_tab_file(courseid, taskid, _("An error occurred while moving the files"))
示例7: action_delete
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def action_delete(self, courseid, taskid, path):
""" Delete a file or a directory """
# normalize
path = path.strip()
if not path.startswith("/"):
path = "/" + path
wanted_path = self.verify_path(courseid, taskid, path)
if wanted_path is None:
return self.show_tab_file(courseid, taskid, _("Internal error"))
# special case: cannot delete current directory of the task
if "/" == wanted_path:
return self.show_tab_file(courseid, taskid, _("Internal error"))
try:
self.task_factory.get_task_fs(courseid, taskid).delete(wanted_path)
return self.show_tab_file(courseid, taskid)
except:
return self.show_tab_file(courseid, taskid, _("An error occurred while deleting the files"))
示例8: path_components
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def path_components(path, delimiter=PathDelimiter):
# type: (str, str) -> collections.Iterable[str]
p = path.strip()
pos = 0
while pos < len(p):
idx = p.find(delimiter, pos)
if idx >= 0:
if idx+1 < len(p):
if p[idx+1] == delimiter:
pos = idx + 2
continue
comp = p[:idx].strip()
p = p[idx+1:].strip()
pos = 0
if len(comp) > 0:
yield comp.replace(2*delimiter, delimiter)
else:
p = strip_path_delimiter(p, delimiter=delimiter)
if len(p) > 0:
yield p.replace(2*delimiter, delimiter)
p = ''
示例9: delete_file
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def delete_file(self, path):
"""Delete file at path."""
path = path.strip('/')
if not self._pyfilesystem_instance.exists(path):
raise web.HTTPError(404, u'File or directory does not exist: %s' % path)
def is_non_empty_dir(os_path):
if self._pyfilesystem_instance.isdir(path):
# A directory containing only leftover checkpoints is
# considered empty.
cp_dir = getattr(self.checkpoints, 'checkpoint_dir', None)
if set(self._pyfilesystem_instance.listdir(path)) - {cp_dir}:
return True
return False
if self._pyfilesystem_instance.isdir(path):
# Don't permanently delete non-empty directories.
if is_non_empty_dir(path):
raise web.HTTPError(400, u'Directory %s not empty' % path)
self.log.debug("Removing directory %s", path)
self._pyfilesystem_instance.removetree(path)
else:
self.log.debug("Unlinking file %s", path)
self._pyfilesystem_instance.remove(path)
示例10: rename_file
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def rename_file(self, old_path, new_path):
"""Rename a file."""
old_path = old_path.strip('/')
new_path = new_path.strip('/')
if new_path == old_path:
return
# Should we proceed with the move?
if self._pyfilesystem_instance.exists(new_path): # TODO and not samefile(old_os_path, new_os_path):
raise web.HTTPError(409, u'File already exists: %s' % new_path)
# Move the file
try:
self._pyfilesystem_instance.move(old_path, new_path)
except web.HTTPError:
raise
except Exception as e:
raise web.HTTPError(500, u'Unknown error renaming file: %s %s' % (old_path, e))
示例11: which
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def which(program):
"""Test if program is pathed."""
# stackoverflow.com/questions/377017/test-if-executable-exists-in-python
fpath = os.path.split(program)[0]
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
示例12: exe_exist
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def exe_exist(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return True
else:
try_paths = []
is_win = platform.system() == 'Windows'
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
full_path = os.path.join(path, program)
try_paths.append(full_path)
if is_win:
try_paths.append(full_path + '.exe')
if any(map(is_exe, try_paths)):
return True
return False
示例13: which
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return "Not found"
示例14: which
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def which(program):
"""Tests if the given executable is available in system PATH.
args:
program: string
The executable name.
"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
#===============================================================================
示例15: which
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import strip [as 别名]
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None