We use cookies to ensure you get the best experience on our website.
import os
def move_directory_to_backup(source_directory, backup_directory):
try:
# Check if the backup directory exists, and create it if it doesn't
if not os.path.exists(backup_directory):
os.makedirs(backup_directory) # If it doesn't exist, create it
# Create a new target path for the source directory inside the backup.old directory
target_directory = os.path.join(backup_directory, os.path.basename(source_directory))
os.rename(source_directory, target_directory)
print(f"The directory '{source_directory}' has been moved to '{target_directory}'.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
source_directory = "directory_name" # Replace this with the actual source directory name
backup_directory = "backup.old" # Replace this with the actual path to the "backup.old" directory
move_directory_to_backup(source_directory, backup_directory)
Nadpisywanie Plikow
import os
import shutil
def move_directory_to_backup(source_directory, backup_directory):
try:
# Sprawdź, czy katalog docelowy istnieje, i utwórz go, jeśli nie istnieje
if not os.path.exists(backup_directory):
os.makedirs(backup_directory)
# Kopiuj zawartość katalogu źródłowego do katalogu docelowego z nadpisywaniem istniejących plików
for item in os.listdir(source_directory):
source_item = os.path.join(source_directory, item)
target_item = os.path.join(backup_directory, item)
if os.path.isdir(source_item):
shutil.copytree(source_item, target_item, symlinks=True)
else:
shutil.copy2(source_item, target_item)
# Usuń katalog źródłowy (opcjonalne, jeśli chcesz go usunąć po skopiowaniu)
shutil.rmtree(source_directory)
print(f"The directory '{source_directory}' has been moved to '{backup_directory}' with file overwriting.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
source_directory = "directory_name" # Zastąp to właściwą nazwą katalogu źródłowego
backup_directory = "backup.old" # Zastą
Cześć Podróżniku!
Ta strona ma nie być typowym poradnikiem w IT, Głównym jej cel to zapisanie krótkich notatek, które mogą się przydać w codziennym życiu podczas korzystania/konfiguracji różnych urządzeń np. Ustawienia DHCP na Routerze Cisco, Ustawieniu Karty sieciowej na Linuxie itp.
Wszelkie prawa zastrzeżone
Dodaj komentarz