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

วิธี Copy ไฟล์ Binary ใน Python

โมดูล shutil มีฟังก์ชันสำหรับ copy ไฟล์และโฟลเดอร์ทั้งหมด

shutil.copy(source, destination) จะ copy ไฟล์จาก source ไปยังโฟลเดอร์ destination หาก destination เป็นชื่อไฟล์ จะใช้เป็นชื่อไฟล์ใหม่ ฟังก์ชันคืนค่าพาธของไฟล์ที่ copy แล้ว

ตัวอย่าง

>>> import shutil
>>> shutil.copy('original.txt', 'duplicate.txt')
'/home/username/duplicate.txt'
>>> shutil.copy('original.txt', 'my_folder/duplicate.txt')
'/home/username/my_folder/duplicate.txt'

กระบวนการเดียวกันใช้กับไฟล์ binary ได้เช่นกัน