import shutil import os # 假设文件在当前目录下 source_directory = '.' # 源文件夹,可以根据需要修改 file_prefix = '0000' # 文件名前缀 file_suffix = '.jpg' # 文件名后缀 # 遍历 1 到 15 for i in range(1, 16): # 生成源文件名和目标文件名 src_file = os.path.join(source_directory, f'{file_prefix}{i:02d}{file_suffix}') dst_file = os.path.join(source_directory, f'{file_prefix}{i + 15:02d}{file_suffix}') # 复制文件 shutil.copy(src_file, dst_file) print(f'复制 {src_file} 到 {dst_file}')
# 读取文件内容 with open('numbers.txt', 'r') as file: lines = file.readlines() # 创建一个新列表,用于保存复制的结果 new_lines = [] # 依次复制并生成新的内容 for i, line in enumerate(lines): original = line.strip() # 去掉每行的换行符 # 计算对应的索引 corresponding_index = int(original) + 15 corresponding = f'{corresponding_index:06d}' # 格式化为6位数,前面补零 new_lines.append(original) new_lines.append(corresponding) # 将结果写入新的文件 with open('output.txt', 'w') as output_file: for new_line in new_lines: output_file.write(new_line + '\n') print("处理完成,结果已保存到 output.txt")
标签:corresponding,数字,lines,file,output,增加,new,line From: https://www.cnblogs.com/Anonytt/p/18419100