fsfwgen/fsfwgen/utility/file_management.py

46 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
import logging
import shutil
import os
from pathlib import Path
_LOGGER = logging.getLogger(__name__)
def copy_file(
filename: Path, destination: Path = "", delete_existing_file: bool = False
):
if not os.path.exists(filename):
_LOGGER.warning(f"File {filename} does not exist")
return
if not os.path.isdir(destination) and os.path.exists(destination):
if delete_existing_file:
os.remove(destination)
else:
_LOGGER.warning(f"Destination file {destination} already exists")
return
try:
shutil.copy2(src=filename, dst=destination)
except FileNotFoundError:
_LOGGER.exception("File not found!")
except shutil.SameFileError:
_LOGGER.exception("Source and destination are the same!")
def move_file(file_name: Path, destination: Path = ""):
if not os.path.exists(file_name):
print(f"move_file: File {file_name} does not exist")
return
if not os.path.exists(destination):
print(f"move_file: Destination directory {destination} does not exist")
return
try:
shutil.copy2(file_name, destination)
os.remove(file_name)
return
except FileNotFoundError:
_LOGGER.exception("File not found!")
except shutil.SameFileError:
_LOGGER.exception("Source and destination are the same!")