#!/usr/bin/env python
# -- coding: utf-8 --
import os
import csv
import shutil
import time
def move_files_according_to_record():
current_directory = os.path.dirname(os.path.abspath(__file__))
move_directory = os.path.join(current_directory, "移動(dòng)")
if not os.path.exists(move_directory):
os.mkdir(move_directory)
record_file_path = os.path.join(current_directory, "發(fā)布記錄.csv")
moved_file_path = os.path.join(current_directory, "移動(dòng)記錄.csv")
# 讀取已經(jīng)移動(dòng)的文件列表
moved_files = set()
if os.path.exists(moved_file_path):
with open(moved_file_path, "r", encoding="utf-8") as moved_file:
reader = csv.reader(moved_file)
for row in reader:
moved_files.add(row[0])
# 讀取發(fā)布記錄文件并移動(dòng)文件
with open(record_file_path, "r", encoding="gbk", newline='') as record_file:
reader = csv.reader(record_file)
for row in reader:
filename = row[0]
source_file_path = os.path.join(current_directory, filename)
destination_file_path = os.path.join(move_directory, filename)
if os.path.exists(source_file_path) and filename not in moved_files:
try:
shutil.move(source_file_path, destination_file_path)
print(f"已移動(dòng)文件: {filename}")
# 記錄移動(dòng)成功的文件名
with open(moved_file_path, "a", encoding="utf-8", newline='') as moved_file:
writer = csv.writer(moved_file)
writer.writerow([filename])
except Exception as e:
print(f"移動(dòng)文件{filename}時(shí)出現(xiàn)錯(cuò)誤:{e}")
else:
print(f"未發(fā)現(xiàn){filename},移動(dòng)失敗。")
if __name__ == "__main__":
move_files_according_to_record()