OpenDroneMap-ODM/opendm/io.py

75 wiersze
1.6 KiB
Python

import os
import shutil, errno
def get_files_list(path_dir):
return os.listdir(path_dir)
def absolute_path_file(path_file):
return os.path.abspath(path_file)
def extract_file_from_path_file(path_file):
path, file = os.path.split(path_file)
return file
def extract_path_from_file(file):
path_file = os.path.abspath(os.path.dirname(file))
path, file = os.path.split(path_file)
return path
def join_paths(path1, path2):
return os.path.join(path1, path2)
def file_exists(path_file):
return os.path.isfile(path_file)
def dir_exists(dirname):
return os.path.isdir(dirname)
def copy(src, dst):
try:
shutil.copytree(src, dst)
except OSError as e:
if e.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else: raise
def rename_file(src, dst):
try:
os.rename(src, dst)
return True
except OSError as e:
if e.errno == errno.ENOENT:
return False
else:
raise
# find a file in the root directory
def find(filename, folder):
for root, dirs, files in os.walk(folder):
return '/'.join((root, filename)) if filename in files else None
def related_file_path(input_file_path, prefix="", postfix=""):
"""
For example: related_file_path("/path/to/file.ext", "a.", ".b")
--> "/path/to/a.file.b.ext"
"""
path, filename = os.path.split(input_file_path)
# path = path/to
# filename = file.ext
basename, ext = os.path.splitext(filename)
# basename = file
# ext = .ext
return os.path.join(path, "{}{}{}{}".format(prefix, basename, postfix, ext))