الدرس الرابع عشر: إنشاء برنامج لإدارة الملفات باستخدام Python

الدرس الرابع عشر: إنشاء برنامج لإدارة الملفات باستخدام Python
الدرس الرابع عشر: إنشاء برنامج لإدارة الملفات باستخدام Python

 

الدرس الرابع عشر: إنشاء برنامج لإدارة الملفات باستخدام Python 📂

مقدمة:

في الدرس ده، هننشئ برنامج بسيط يساعدك على إدارة الملفات في جهازك. البرنامج هيكون قادر على:

  1. عرض قائمة الملفات في مجلد معين.
  2. نسخ أو نقل الملفات.
  3. حذف الملفات أو المجلدات.

هنستخدم مكتبات Python المدمجة مثل os و shutil لتحقيق الهدف ده.


خطوات تنفيذ المشروع:

1. استعراض قائمة الملفات في مجلد

هنبدأ بكتابة كود يعرض الملفات والمجلدات الموجودة في مجلد معين.


import os
def list_files(folder_path):
try:
files = os.listdir(folder_path)
print(f"Files and Folders in '{folder_path}':")
for file in files:
print(file)
except FileNotFoundError:
print("The folder does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
# اختبار الدالة
list_files(".") # عرض محتويات المجلد الحالي

2. نسخ الملفات باستخدام shutil

هنضيف وظيفة لنسخ الملفات من مكان لآخر.


import shutil
def copy_file(source, destination):
try:
shutil.copy(source, destination)
print(f"File '{source}' copied to '{destination}'.")
except FileNotFoundError:
print("Source file or destination folder not found.")
except Exception as e:
print(f"An error occurred: {e}")
# اختبار الدالة
copy_file("example.txt", "backup/example.txt")

3. نقل الملفات

لتنفيذ عملية نقل الملفات، هنستخدم shutil.move.


def move_file(source, destination):
try:
shutil.move(source, destination)
print(f"File '{source}' moved to '{destination}'.")
except FileNotFoundError:
print("Source file or destination folder not found.")
except Exception as e:
print(f"An error occurred: {e}")
# اختبار الدالة
move_file("backup/example.txt", "final_folder/example.txt")

4. حذف الملفات أو المجلدات

هنضيف وظيفة لحذف الملفات أو المجلدات بالكامل.


def delete_file_or_folder(path):
try:
if os.path.isfile(path):
os.remove(path)
print(f"File '{path}' deleted.")
elif os.path.isdir(path):
shutil.rmtree(path)
print(f"Folder '{path}' deleted.")
else:
print("The specified path is not a file or folder.")
except FileNotFoundError:
print("The file or folder does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
# اختبار الدالة
delete_file_or_folder("final_folder/example.txt")
delete_file_or_folder("backup")

5. وضع كل الوظائف في قائمة أوامر

هنجمع كل الوظائف السابقة في قائمة تفاعلية.


def file_manager():
print("Welcome to File Manager!")
while True:
print("\nOptions:")
print("1. List files in a folder")
print("2. Copy a file")
print("3. Move a file")
print("4. Delete a file or folder")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
folder = input("Enter the folder path: ")
list_files(folder)
elif choice == "2":
source = input("Enter the source file path: ")
destination = input("Enter the destination folder path: ")
copy_file(source, destination)
elif choice == "3":
source = input("Enter the source file path: ")
destination = input("Enter the destination folder path: ")
move_file(source, destination)
elif choice == "4":
path = input("Enter the file or folder path to delete: ")
delete_file_or_folder(path)
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
# تشغيل البرنامج
file_manager()

ملاحظات:

  • الأمان: تأكد من أنك تتعامل مع الملفات بحذر لتجنب حذف ملفات مهمة بالخطأ.
  • الإضافات: يمكنك تحسين البرنامج بإضافة واجهة رسومية باستخدام مكتبة tkinter أو PyQt.

خاتمة:

في الدرس ده، تعلمنا كيفية إنشاء برنامج لإدارة الملفات باستخدام Python. في الدرس القادم، هنناقش كيفية إنشاء تطبيق ويب بسيط لإدارة الملفات باستخدام Flask.

تعليقات