记录下 免得用的时候找不到
import os
import shutil
import time
# 源目录
source_dir = r'W:\欧美满天星'
# 目标目录
target_dir = r'F:\影视库\欧美满天星'
# 视频文件扩展名,补充了更多常见的视频格式
video_extensions = ('.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.webm', '.mpeg', '.mpg', '.3gp', '.rmvb', '.ts', '.vob')
# 刮削文件扩展名,补充了常见的图片和字幕格式
scraping_extensions = ('.nof', '.srt', '.ass', '.jpg', '.jpeg', '.png', '.nfo')
# 定义延迟时间(秒)
DELAY_TIME = 1
def create_links_and_copy_files(source, target):
# 遍历源目录下的所有文件和文件夹
for root, dirs, files in os.walk(source):
# 计算相对路径
relative_path = os.path.relpath(root, source)
# 创建目标目录结构
target_sub_dir = os.path.join(target, relative_path)
os.makedirs(target_sub_dir, exist_ok=True)
for file in files:
source_file_path = os.path.join(root, file)
target_file_path = os.path.join(target_sub_dir, file)
file_extension = os.path.splitext(file)[1].lower()
if os.path.exists(target_file_path):
print(f'{target_file_path} 已存在,跳过操作')
continue
if file_extension in video_extensions:
# 创建软连接
try:
if os.name == 'nt':
# Windows 系统
os.system(f'mklink "{target_file_path}" "{source_file_path}"')
else:
# Linux 或 macOS 系统
os.symlink(source_file_path, target_file_path)
print(f'为 {source_file_path} 创建软连接到 {target_file_path}')
except Exception as e:
print(f'为 {source_file_path} 创建软连接时出错: {e}')
elif file_extension in scraping_extensions:
# 复制刮削文件
try:
shutil.copy2(source_file_path, target_file_path)
print(f'将 {source_file_path} 复制到 {target_file_path}')
except Exception as e:
print(f'复制 {source_file_path} 时出错: {e}')
# 添加延迟
time.sleep(DELAY_TIME)
if __name__ == "__main__":
create_links_and_copy_files(source_dir, target_dir)