使用多线程拷贝一个文件夹中的数据到另一个文件夹中的示例代码
import os
import shutil
import threading
def copy_files(source_dir, target_dir, files):
"""
将指定文件从源文件夹拷贝到目标文件夹。
参数:
- source_dir:源文件夹路径。
- target_dir:目标文件夹路径。
- files:要拷贝的文件列表。
返回值:
无。
"""
for file in files:
source_path = os.path.join(source_dir, file)
target_path = os.path.join(target_dir, file)
shutil.copy(source_path, target_path)
print(f"Copied {file} from {source_dir} to {target_dir}")
def copy_files_multithread(source_dir, target_dir, num_threads=4):
"""
使用多线程将文件从源文件夹拷贝到目标文件夹。
参数:
- source_dir:源文件夹路径。
- target_dir:目标文件夹路径。
- num_threads:线程数量,默认为4。
返回值:
无。
"""
files = os.listdir(source_dir)
num_files = len(files)
files_per_thread = (num_files + num_threads - 1) // num_threads
threads = []
for i in range(num_threads):
start_index = i * files_per_thread
end_index = min(start_index + files_per_thread, num_files)
thread_files = files[start_index:end_index]
thread = threading.Thread(target=copy_files, args=(source_dir, target_dir, thread_files))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
# 示例使用
source_folder = "source_folder"
target_folder = "target_folder"
num_threads = 4
copy_files_multithread(source_folder, target_folder, num_threads)
threads = []
:创建一个空列表,用于存放线程对象。for i in range(num_threads):
:使用循环来创建多个线程。start_index = i * files_per_thread
:计算每个线程要处理的文件列表的起始索引。end_index = min(start_index + files_per_thread, num_files)
:计算每个线程要处理的文件列表的结束索引,确保不超过文件总数。thread_files = files[start_index:end_index]
:从文件列表中提取出每个线程要处理的文件列表。thread = threading.Thread(target=copy_files, args=(source_dir, target_dir, thread_files))
:创建一个线程对象,指定线程函数为copy_files
,并传入参数。threads.append(thread)
:将创建的线程对象添加到列表中。thread.start()
:启动线程,开始执行线程函数。
多线程拷贝一个文件夹中可能多个文件夹下的一个文件名称中含有x的文件传输到另外一个文件夹中
import os
import shutil
import threading
def copy_files(source_dir, target_dir, file_list):
"""
将指定文件从源文件夹拷贝到目标文件夹。
参数:
- source_dir:源文件夹路径。
- target_dir:目标文件夹路径。
- file_list:要拷贝的文件列表。
返回值:
无。
"""
for file_name in file_list:
source_path = os.path.join(source_dir, file_name)
target_path = os.path.join(target_dir, file_name)
shutil.copy(source_path, target_path)
print(f"Copied {file_name} from {source_dir} to {target_dir}")
def copy_files_with_x(source_dir, target_dir, num_threads=4):
"""
使用多线程将文件夹中含有 "x" 的文件拷贝到目标文件夹。
参数:
- source_dir:源文件夹路径。
- target_dir:目标文件夹路径。
- num_threads:线程数量,默认为4。
返回值:
无。
"""
file_list = [file_name for file_name in os.listdir(source_dir) if 'x' in file_name]
num_files = len(file_list)
files_per_thread = (num_files + num_threads - 1) // num_threads
threads = []
for i in range(num_threads):
start_index = i * files_per_thread
end_index = min(start_index + files_per_thread, num_files)
thread_files = file_list[start_index:end_index]
thread = threading.Thread(target=copy_files, args=(source_dir, target_dir, thread_files))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
# 示例使用
source_folder = "source_folder"
target_folder = "target_folder"
num_threads = 4
copy_files_with_x(source_folder, target_folder, num_threads)
多线程传输时间戳相同的文件夹
import os
import shutil
import concurrent.futures
def copy_folder(src_folder, dest_folder):
"""复制文件夹到目标文件夹中"""
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
shutil.copytree(src_folder, os.path.join(dest_folder, os.path.basename(src_folder)))
print(f"Folder '{src_folder}' copied to '{dest_folder}'")
def find_folder(timestamp, search_folder):
"""在搜索文件夹中递归查找包含指定时间戳的文件夹"""
for root, dirs, files in os.walk(search_folder):
for dir_name in dirs:
if dir_name.startswith(f"C_{timestamp}"):
return os.path.join(root, dir_name)
return None
def process_png_file(png_file, raw_folder, tmp_folder):
"""处理每个 PNG 文件"""
_, timestamp = os.path.splitext(os.path.basename(png_file))[0].split("_")
folder_path = find_folder(timestamp, raw_folder)
if folder_path:
copy_folder(folder_path, tmp_folder)
else:
print(f"Folder with timestamp '{timestamp}' not found in '{raw_folder}'")
def main():
# 定义文件夹路径
camera_folder = "camera"
raw_folder = "raw"
tmp_folder = "tmp"
# 获取 camera 文件夹中的所有 PNG 文件路径
png_files = [os.path.join(camera_folder, file) for file in os.listdir(camera_folder) if file.endswith(".png")]
# 使用多线程处理每个 PNG 文件
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for png_file in png_files:
futures.append(executor.submit(process_png_file, png_file, raw_folder, tmp_folder))
for future in concurrent.futures.as_completed(futures):
future.result()
if __name__ == "__main__":
main()