نسخ الملف في Python: Shutil.copy()، طريقة Shutil.copystat()
⚡ ملخص ذكي
Python copies files with the shutil module. shutil.copy() duplicates a file's contents, while shutil.copystat() also copies permissions, modification time, and metadata. Here you will learn both methods with a step-by-step example.

Python طرق نسخ الملفات
Python يوفر وظائف مدمجة لنسخ الملفات بسهولة باستخدام Operaتينغ المرافق نظام شل.
يتم استخدام الأمر التالي لنسخ الملف
shutil.copy(src,dst)
يُستخدم الأمر التالي لنسخ الملف مع معلومات البيانات.
shutil.copystat(src,dst)
كيفية نسخ ملف في Python
فيما يلي خطوات نسخ الملف Python باستخدام طريقة Shutil Copy():
الخطوة 1) التقط المسار الأصلي في الدليل الحالي
Before, we copy a file, we need to get the the path to the original file in the current directory. In the code -
- إعلان المتغير
- تطبيق وظيفة الانقسام على المتغير
Code تفسير
- First we are going to check that our "guru99.txt" file exists or not. Since we have created guru99.txt file earlier, we know it exists, and we will carry on further with the code
- We store the file path in the variable "SRC" if your file exist
- بمجرد أن نحصل على المسار، سنقوم بفصل المسار واسم الملف
- لهذا، سوف نستخدم الانقسام
path.splitدالة على متغير المصدر - Code when executed prints out "اسم الملف"و"مسار الملف" separately
الخطوة 2) قم بإنشاء نسخة من ملفنا الحالي باستخدام وحدة Shutil
We use Shutil Module to create a copy of the existing file. Here we used to create a copy of our existing file "guru99.txt."
Code تفسير
- Take the original file name "guru99.txt" and add letters .bak at the end "guru99.txt.bak". This name with .bak extension is going to be our duplicate copy
- And then we going to use utility's copy function to copy from source to the destination
- عند تشغيل الكود، سترى أنه تم إنشاء ملف مكرر بامتداد .bak على الجانب الأيمن من اللوحة
الخطوة 3) انسخ البيانات الوصفية المرتبطة بالملف وأذونات الملف والمعلومات الأخرى
تقوم وظيفة النسخ بنسخ محتوى الملف فقط ولكن لا توجد معلومات أخرى. لنسخ البيانات الوصفية associated with the file, file permission and other information you have to use "com.copystat" function. Before we run this code, we have to delete our copy file "guru99.text.bak".
بمجرد حذف الملف وتشغيل البرنامج، سيقوم بإنشاء نسخة من ملف .txt الخاص بك ولكن هذه المرة مع جميع المعلومات مثل إذن الملف ووقت التعديل ومعلومات البيانات الوصفية. يمكنك الذهاب إلى نظام التشغيل الخاص بك للتحقق من المعلومات.
ها هو الكود
import os import shutil from os import path def main(): # make a duplicate of an existing file if path.exists("guru99.txt"): # get the path to the file in the current directory src = path.realpath("guru99.txt"); #seperate the path from the filter head, tail = path.split(src) print("path:" +head) print("file:" +tail) #let's make a backup copy by appending "bak" to the name dst = src+".bak" # nowuse the shell to make a copy of the file shutil.copy(src, dst) #copy over the permissions,modification shutil.copystat(src,dst) if __name__=="__main__": main()
الخطوة 4) جلب المعلومات
يمكنك جلب المعلومات حول الملف النصي الذي تم تعديله مؤخرًا
-
Code السطر رقم 15 - يوضح هذا السطر اليوم والتاريخ والشهر والسنة والوقت الذي تم فيه آخر تعديل لملف .txt (guru99). نستخدم وحدة المسار للحصول على تفاصيل وقت تعديل الملف، ثم سنستخدم... فئات الوقت ج وظيفة الوقت لتحويل ذلك إلى وقت قابل للقراءة. لذا عندما نقوم بتشغيل الكود، يمكننا أن نرى أن الملف guru99.txt تم تعديله آخر مرة في الاثنين 8 ينايرth الساعة 13:35 2018.
-
Code السطر رقم 17 - يقوم بنفس الشيء، حيث يعرض معلومات حول تعديل الملف، ولكن بتنسيق مختلف. هنا نستخدم الحصول على وظيفة وقت التعديل (path.getmtime("guru99.txt")). Now instead of using the c time function we going to use من وظيفة الطابع الزمني والذهاب لبناء كائن التاريخ والوقت. في الإخراج، يمكنك رؤية تفاصيل وقت تعديل الملف مطبوعة بتنسيق مختلف 2018-01-08, 13:35:51.334072
ها هو الكود
# # Example file for working with o.s path module import os from os import path import datetime from datetime import date, time, timedelta import time def main(): # Get the modification time t = time.ctime(path.getmtime("guru99.txt.bak")) print(t) print(datetime.datetime.fromtimestamp(path.getmtime("guru99.txt.bak"))) if __name__ == "__main__": main()





