Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธี Copy ไฟล์ไปยังไดเรกทอรีใหม่ด้วย Python

ใช้โมดูล shutil สำหรับ copy ไฟล์/โฟลเดอร์ สำหรับหลายไฟล์ ต้องมี list แล้ววนลูป copy

shutil.copy(source, destination) copy ไฟล์จาก source ไป destination (ถ้า destination เป็นชื่อไฟล์ จะ rename) คืนค่าพาธไฟล์ใหม่

ตัวอย่าง

import shutil, os
files = ['file1.txt', 'file2.txt', 'file3.txt']
os.mkdir('my_new_folder')
for f in files:
    shutil.copy(f, 'my_new_folder')