Python/pathlib/Path

من موسوعة حسوب
< Python‏ | pathlib
مراجعة 10:44، 27 يوليو 2018 بواسطة مصطفى-عطا (نقاش | مساهمات) (إنشاء مبدئي)
(فرق) → مراجعة أقدم | المراجعة الحالية (فرق) | مراجعة أحدث ← (فرق)
اذهب إلى التنقل اذهب إلى البحث

الصنف pathlib.Path في بايثون

الاستخدام الأساسي

استيراد الصنف الأساسي:

>>> from pathlib import Path

تعداد المسارات الفرعية:

>>> p = Path('.')

>>> [x for x in p.iterdir() if x.is_dir()]

[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),

PosixPath('__pycache__'), PosixPath('build')]

تعداد ملفات بايثون المصدرية في شجرة المجلدات هذه:

>>>

>>> list(p.glob('**/*.py'))

[PosixPath('test_pathlib.py'), PosixPath('setup.py'),

PosixPath('pathlib.py'), PosixPath('docs/conf.py'),

PosixPath('build/lib/pathlib.py')]

التنقّل ضمن شجرة المجلدات:

>>> p = Path('/etc')

>>> q = p / 'init.d' / 'reboot'

>>> q

PosixPath('/etc/init.d/reboot')

>>> q.resolve()

PosixPath('/etc/rc.d/init.d/halt')

الاستعلام عن خصائص المسار:

>>> q.exists()

True

>>> q.is_dir()

False

فتح ملف:

>>>

>>> with q.open() as f: f.readline()

...

'#!/bin/bash\n'

11.1.2.‏ ‎‏PurePaths‏

الكائنات المنشأة من الصنف المسارات النقية تقدم عمليات لمعالجة المسارات بحيث لا يتم الوصول الفعلي إلى نظام الملفات. توجد ثلاث طرائق للوصول إلى هذه الأصناف، ونسمي هذه الطرائق بالنكهات. 

class pathlib.PurePath(*pathsegments)

هو صنف عام يمثل نكهة مسار النظام (حيث يُنشأ كائن من نوع PurePosixPath أو PureWindowsPath عند إنشاء كائن منه)

class pathlib.PurePosixPath(*pathsegments)

هو صنف فرعي من الصنف PurePath، ويمثل مسارات أنظمة الملفات المغايرة لنظام ويندوز

class pathlib.PureWindowsPath(*pathsegments)

هو صنف الفرعي من الصنف PurePath، ويمثل مسارات نظام الملفات في ويندوز

11.1.3. Concrete paths

المسارات الصلبة هي أصناف فرعية من أصناف المسارات النقية، وهي تقدم إضافة لكل ما سبق طرائق للقيام باستدعاءات من النظام على كائنات المسار، وهناك ثلاث طرق لإنشاء كائن من المسارات الصلبة

class pathlib.Path(*pathsegments)

هو صنف فرعي من الصنف PurePath يمثل المسارات الصلبة لنكهات المسارات المختلفة للنظام، وعند إنشاء كائن منه يُنشأ كائن من أحد الصنفين PosixPath  أو WindowsPath.

A subclass of PurePath, this class represents concrete paths of the system’s path flavour (instantiating it creates either a PosixPath or a WindowsPath):

class pathlib.PosixPath(*pathsegments)

هو صنف فرعي من الصنف Path  والصنف PurePosixPath، يمثل المسارات الصلبة لمسارات أنظمة الملفات المغايرة لنظام ويندوز.

A subclass of Path and PurePosixPath, this class represents concrete non-Windows filesystem paths:

class pathlib.WindowsPath(*pathsegments)

هو صنف فرعي من الصنف Path  والصنف PurePosixPath، يمثل المسارات الصلبة لمسارات أنظمة ملفات ويندوز.

A subclass of Path and PureWindowsPath, this class represents concrete Windows filesystem paths:

11.1.4. تقابل الأدوات المذكورة مع وحدة os

يبيّن الجدول التالي تقابل عدة توابع من الوحدة os  مع ما يقابلها و يكافئها من الوحدة PurePath و Path.

Below is a table mapping various os functions to their corresponding PurePath/Path equivalent.

ملاحظة:

بالرغم من وجود بعض التقاطع في حالات استخدام التابعين os.path.relpath‎‎()‎ والتابع PurePath.relative_to()‎ إلا أن مدلول كل منهما مختلف بما يدعو لعدم اعتبارهما متكافئين.

Note

Although os.path.relpath() and PurePath.relative_to() have some overlapping use-cases, their semantics differ enough to warrant not considering them equivalent.

الوحدة os والوحدة os.path pathlib
os.path.abspath() Path.resolve()
os.getcwd() Path.cwd()
os.path.exists() Path.exists()
os.path.expanduser() Path.expanduser() و Path.home()
os.path.isdir() Path.is_dir()
os.path.isfile() Path.is_file()
os.path.islink() Path.is_symlink()
os.stat() Path.stat(),Path.owner(), Path.group()
os.path.isabs() PurePath.is_absolute()
os.path.join() PurePath.joinpath()
os.path.basename() PurePath.name
os.path.dirname() PurePath.parent
os.path.splitext() PurePath.suffix

Previous topic

11. File and Directory Access

Next topic

11.2. os.path — Common pathname manipulations

This Page

  • Report a Bug
  • Show Source