View Categories

10.1. واجهة نظام التشغيل

< 1 دقيقة

جدول المحتويات

10.1. واجهة نظام التشغيل #

توفر وحدة os عشرات الدوال للتفاعل مع نظام التشغيل:

>>> import os
>>> os.getcwd()      # Return the current working directory
'C:\\Python39'
>>> os.chdir('/server/accesslogs')   # Change current working directory
>>> os.system('mkdir today')   # Run the command mkdir in the system shell
0

تأكد من استخدام نمط import os بدلاً من from os import *. سيمنع هذا os.open() من حجب دالة open() المدمجة التي تعمل بشكل مختلف تمامًا.

الدالتان المدمجتان dir() وhelp() مفيدتان كمساعدات تفاعلية للعمل مع وحدات كبيرة مثل نظام التشغيل:

>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>

لمهام إدارة الملفات والمجلدات اليومية، توفر وحدة shutil واجهة مستخدم متطورة وسهلة الاستخدام:

>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'
error: Content is protected !!
Scroll to Top