Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 62ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
b

backup

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 shutil 進行檔案與目錄操作

Last active 5 hours ago automation backup directory file-management python shutil

shutil 模組提供高級的檔案與目錄管理功能,包括複製、移動、壓縮與刪除,適用於備份、部署與自動化檔案管理。

0 0 0 1
1 import shutil
2 import os
3
4 # 定義來源與目標
5 source_file = "example.txt"
6 destination_dir = "backup/"
7 destination_file = os.path.join(destination_dir, source_file)
8
9 # 確保目標目錄存在
10 os.makedirs(destination_dir, exist_ok=True)