الفرق بين المراجعتين ل"Python/pathlib/PurePath"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
 
(11 مراجعة متوسطة بواسطة نفس المستخدم غير معروضة)
سطر 1: سطر 1:
== الصنف <code>pathlib.PurePath</code> في بايثون==
+
= الصنف <code>pathlib.PurePath</code> في بايثون=
 
يحوي هذا الصنف التوابع والعمليات الأساسية للتعامل مع المسارات، إلا أنها لا تحوي أي وصول حقيقي إلى نظام الملفات، ومن ثمّ فليس فيها أي عملية إدخال أو إخراج بل تقتصر على العمليات النصية على المسارات، ويمكننا تلخيص عملها بأنها توابع لمعالجة السلاسل النصية التي تمثل مسارات الملفات.
 
يحوي هذا الصنف التوابع والعمليات الأساسية للتعامل مع المسارات، إلا أنها لا تحوي أي وصول حقيقي إلى نظام الملفات، ومن ثمّ فليس فيها أي عملية إدخال أو إخراج بل تقتصر على العمليات النصية على المسارات، ويمكننا تلخيص عملها بأنها توابع لمعالجة السلاسل النصية التي تمثل مسارات الملفات.
  
سطر 136: سطر 136:
 
يُستحسن استدعاء التابع <code>bytes</code> ضمن نظام يونكس فقط، فترميز <code>unicode</code> في نظام ويندوز هو التمثيل الأساسي لمسارات نظام الملفات.
 
يُستحسن استدعاء التابع <code>bytes</code> ضمن نظام يونكس فقط، فترميز <code>unicode</code> في نظام ويندوز هو التمثيل الأساسي لمسارات نظام الملفات.
 
==المتغيرات الأعضاء في الصنف <code>PurePath</code>==
 
==المتغيرات الأعضاء في الصنف <code>PurePath</code>==
 +
===[[Python/pathlib/PurePath/anchor|المتغير <code>PurePath.anchor</code>]]===
 +
يجمع اسم السواقة مع مسار الجذر
 +
===[[Python/pathlib/PurePath/drive|المتغير <code>PurePath.drive</code>]]===
 +
سلسلة نصية تمثل اسم السواقة أو الحرف الممثل لها (في حال وجوده ضمن المسار)
 +
===[[Python/pathlib/PurePath/name|المتغير <code>PurePath.name</code>]]===
 +
سلسة نصية تُمثّل العنصر الأخير في المسار، مع استثناء السواقة ومسارالجذر إن وُجد أي منهما
 
===[[Python/pathlib/PurePath/parts|المتغير <code>PurePath.parts</code>]]===
 
===[[Python/pathlib/PurePath/parts|المتغير <code>PurePath.parts</code>]]===
 
يمثّل هذا المتغير <code>tuple</code> لعناصر المسار
 
يمثّل هذا المتغير <code>tuple</code> لعناصر المسار
===[[Python/pathlib/PurePath/drive|المتغير <code>PurePath.drive</code>]]===
 
سلسلة نصية تمثل اسم السواقة أو الحرف الممثل لها (في حال وجوده ضمن المسار)
 
===المتغير <code>PurePath.anchor</code>===
 
يجمع اسم السواقة مع مسار الجذر<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('c:/Program Files/').anchor
 
'c:\\'
 
 
>>> PureWindowsPath('c:Program Files/').anchor
 
'c:'
 
 
>>> PurePosixPath('/etc').anchor
 
'/'
 
 
>>> PureWindowsPath('//host/share').anchor
 
'\\\\host\\share\\'
 
</syntaxhighlight>
 
===المتغير <code>PurePath.parents</code>===
 
سلسة غير قابلة للتعديل (immutable) تُعطي وصولًا للآباء المنطقية للمسار<syntaxhighlight lang="python3">
 
 
>>> p = PureWindowsPath('c:/foo/bar/setup.py')
 
 
>>> p.parents[0]
 
PureWindowsPath('c:/foo/bar')
 
 
>>> p.parents[1]
 
PureWindowsPath('c:/foo')
 
 
>>> p.parents[2]
 
PureWindowsPath('c:/')
 
</syntaxhighlight>
 
 
===[[Python/pathlib/PurePath/parent|المتغير <code>PurePath.parent</code>]]===
 
===[[Python/pathlib/PurePath/parent|المتغير <code>PurePath.parent</code>]]===
 
الأب المنطقي للمسار الحالي
 
الأب المنطقي للمسار الحالي
===المتغير <code>PurePath.name</code>===
+
===[[Python/pathlib/PurePath/parents|المتغير <code>PurePath.parents</code>]]===
سلسة نصية تُمثّل العنصر الأخير في المسار، مع استثناء السواقة ومسارالجذر إن وُجد أي منهما<syntaxhighlight lang="python3">
+
سلسة غير قابلة للتعديل (immutable) تُعطي وصولًا للآباء المنطقية للمسار
>>> PurePosixPath('my/library/setup.py').name
 
'setup.py'
 
</syntaxhighlight>لا يؤخذ اعتبارٌ لأسماء سواقات المجلدات المُشاركة (UNC drive)<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('//some/share/setup.py').name
 
'setup.py'
 
 
 
>>> PureWindowsPath('//some/share').name
 
''
 
</syntaxhighlight>
 
===المتغير <code>PurePath.suffix</code>===
 
امتداد الملف لآخر عنصر في المسار (إن كان له امتداد)<syntaxhighlight lang="python3">
 
>>> PurePosixPath('my/library/setup.py').suffix
 
'.py'
 
 
 
>>> PurePosixPath('my/library.tar.gz').suffix
 
'.gz'
 
 
 
>>> PurePosixPath('my/library').suffix
 
''
 
</syntaxhighlight>
 
===المتغير <code>PurePath.suffixes</code>===
 
قائمة بامتدادات الملف الذي يشير إليه المسار.<syntaxhighlight lang="python3">
 
>>> PurePosixPath('my/library.tar.gar').suffixes
 
['.tar', '.gar']
 
 
 
>>> PurePosixPath('my/library.tar.gz').suffixes
 
['.tar', '.gz']
 
 
 
>>> PurePosixPath('my/library').suffixes
 
[]
 
</syntaxhighlight>
 
 
 
=== المتغير <code>PurePath.stem</code> ===
 
The final path component, without its suffix:<syntaxhighlight lang="python3">
 
>>> PurePosixPath('my/library.tar.gz').stem
 
'library.tar'
 
 
 
>>> PurePosixPath('my/library.tar').stem
 
'library'
 
 
 
>>> PurePosixPath('my/library').stem
 
'library'
 
</syntaxhighlight>
 
  
== التوابع الأعضاء في الصنف <code>PurePath</code> ==
+
=== [https://wiki.hsoub.com/Python/pathlib/PurePath/root المتغير <code>PurePath.root</code>] ===
 +
سلسلة نصية تمثل مسار الجذر (المحلي أو العام) إن وُجد.
  
=== التابع <code>PurePath.as_posix</code> ===
+
===[[Python/pathlib/PurePath/suffix|المتغير <code>PurePath.suffix</code>]]===
Return a string representation of the path with forward slashes (/):<syntaxhighlight lang="python3">
+
امتداد الملف لآخر عنصر في المسار (إن كان له امتداد)
>>> p = PureWindowsPath('c:\\windows')
+
===[[Python/pathlib/PurePath/suffixes|المتغير <code>PurePath.suffixes</code>]]===
 
+
قائمة بامتدادات الملف الذي يشير إليه المسار.
>>> str(p)
+
===[[Python/pathlib/PurePath/stem|المتغير <code>PurePath.stem</code>]]===
'c:\\windows'
+
آخر عنصر في المسار، دون امتداده
 
+
==التوابع الأعضاء في الصنف <code>PurePath</code>==
>>> p.as_posix()
+
===[[Python/pathlib/PurePath/as posix|التابع <code>PurePath.as_posix</code>]]===
'c:/windows'
+
يعيد تمثيلًا نصّيًّا للمسار مع استخدام الشرطة الأمامية للفصل بين المستويات في المسار <code>'/'</code>
</syntaxhighlight>
+
===[[Python/pathlib/PurePath/as uri|التابع <code>PurePath.as_uri</code>]]===
 
+
يعيد تمثيل المسار بتمثيل <code>URI</code>، ويرمي استثناء <code>ValueError</code> إن لم يكن المسار بصيغته المطلقة.
=== التابع <code>PurePath.as_uri</code> ===
+
===[[Python/pathlib/PurePath/is absolute|التابع <code>PurePath.is_absolute</code>]]===
Represent the path as a file URI. ValueError is raised if the path isn’t absolute.<syntaxhighlight lang="python3">
+
يعيد <code>True</code> إن كان كائن المسار يشير إلى مسارٍ مطلق. يكون المسار مطلقًا إن كان يحتوي على مسار جذر واسم السواقة (إن كانت نكهة كائن المسار تسمح بذلك).
>>> p = PurePosixPath('/etc/passwd')
+
===[[Python/pathlib/PurePath/is reserved|التابع <code>PurePath.is_reserved</code>]]===
 
+
يعيد <code>True</code> إذا كان المسار يُعتبر اسمًا مجوزًا في النظام.
>>> p.as_uri()
+
===[[Python/pathlib/PurePath/joinpath|التابع <code>PurePath.joinpath</code>]]===
'file:///etc/passwd'
+
يُؤدي استدعاء هذا التابع إلى دمج المسار مع كلٍّ من الوسطاء الممررة له بالترتيب.
 
+
===[[Python/pathlib/PurePath/match|التابع <code>PurePath.match</code>]]===
>>> p = PureWindowsPath('c:/Windows')
+
يختبر هذا التابع مطابقة المسار لنمط اختبار المطابقة <code>glob</code> المعطى.
 
+
===[[Python/pathlib/PurePath/relative to|التابع <code>PurePath.relative_to</code>]]===
>>> p.as_uri()
+
يحسب هذا التابع كيف يبدو المسار عند اعتباره منسوبًا إلى المسار المُعطى، ويرمي الاستثناء <code>ValueError</code> عندما لا يُمكن نسب المسار الأصلي للمسار المُعطى.
'file:///c:/Windows'
+
===[[Python/pathlib/PurePath/with name|التابع <code>PurePath.with_name</code>]]===
</syntaxhighlight>
+
يعيد مسارًا جديدًا مع تغيير اسم العنصر الأخير، ويرمي استثناءً <code>ValueError</code> إن لم يكن في المسار الأصلي اسم ملف.
====التابع <code>PurePath.is_absolute</code>====
+
===[[Python/pathlib/PurePath/with suffix|التابع <code>PurePath.with_suffix</code>]]===
Return whether the path is absolute or not. A path is considered absolute if it has both a root and (if the flavour allows) a drive:<syntaxhighlight lang="python3">
+
يعيد مسارًا جديدًا بعد تغيير امتداد الملف، وإن لم يكن للملف امتداد فإنه يضيف الامتداد المُعطى إليه.
>>> PurePosixPath('/a/b').is_absolute()
 
True
 
 
 
>>> PurePosixPath('a/b').is_absolute()
 
False
 
 
 
>>> PureWindowsPath('c:/a/b').is_absolute()
 
True
 
 
 
>>> PureWindowsPath('/a/b').is_absolute()
 
False
 
 
 
>>> PureWindowsPath('c:').is_absolute()
 
False
 
 
 
>>> PureWindowsPath('//some/share').is_absolute()
 
True
 
</syntaxhighlight>
 
===التابع <code>PurePath.is_reserved</code>===
 
With PureWindowsPath, return True if the path is considered reserved under Windows, False otherwise. With PurePosixPath, False is always returned.<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('nul').is_reserved()
 
True
 
 
 
>>> PurePosixPath('nul').is_reserved()
 
False
 
</syntaxhighlight>File system calls on reserved paths can fail mysteriously or have unintended effects.
 
===التابع <code>PurePath.joinpath</code>===
 
Calling this method is equivalent to combining the path with each of the other arguments in turn:<syntaxhighlight lang="python3">
 
>>> PurePosixPath('/etc').joinpath('passwd')
 
PurePosixPath('/etc/passwd')
 
 
 
>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))
 
PurePosixPath('/etc/passwd')
 
 
 
>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
 
PurePosixPath('/etc/init.d/apache2')
 
 
 
>>> PureWindowsPath('c:').joinpath('/Program Files')
 
PureWindowsPath('c:/Program Files')
 
</syntaxhighlight>
 
===التابع <code>PurePath.match</code>===
 
Match this path against the provided glob-style pattern. Return True if matching is successful, Falseotherwise.
 
 
 
If pattern is relative, the path can be either relative or absolute, and matching is done from the right:<syntaxhighlight lang="python3">
 
>>> PurePath('a/b.py').match('*.py')
 
True
 
 
 
>>> PurePath('/a/b/c.py').match('b/*.py')
 
True
 
 
 
>>> PurePath('/a/b/c.py').match('a/*.py')
 
False
 
</syntaxhighlight>If pattern is absolute, the path must be absolute, and the whole path must match:<syntaxhighlight lang="python3">
 
>>> PurePath('/a.py').match('/*.py')
 
True
 
 
 
>>> PurePath('a/b.py').match('/*.py')
 
False
 
</syntaxhighlight>As with other methods, case-sensitivity is observed:<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('b.py').match('*.PY')
 
True
 
</syntaxhighlight>
 
 
 
=== التابع <code>PurePath.relative_to</code> ===
 
Compute a version of this path relative to the path represented by other. If it’s impossible, ValueError is raised:<syntaxhighlight lang="python3">
 
>>> p = PurePosixPath('/etc/passwd')
 
 
 
>>> p.relative_to('/')
 
PurePosixPath('etc/passwd')
 
 
 
>>> p.relative_to('/etc')
 
PurePosixPath('passwd')
 
 
 
>>> p.relative_to('/usr')
 
 
 
Traceback (most recent call last):
 
 File "<stdin>", line 1, in <module>
 
 File "pathlib.py", line 694, in relative_to
 
   .format(str(self), str(formatted)))
 
ValueError: '/etc/passwd' does not start with '/usr'
 
</syntaxhighlight>
 
===التابع <code>PurePath.with_name</code>===
 
Return a new path with the name changed. If the original path doesn’t have a name, ValueError is raised:<syntaxhighlight lang="python3">
 
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
 
>>> p.with_name('setup.py')
 
PureWindowsPath('c:/Downloads/setup.py')
 
 
 
>>> p = PureWindowsPath('c:/')
 
>>> p.with_name('setup.py')
 
Traceback (most recent call last):
 
 File "<stdin>", line 1, in <module>
 
 File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
 
   raise ValueError("%r has an empty name" % (self,))
 
ValueError: PureWindowsPath('c:/') has an empty name
 
</syntaxhighlight>
 
===التابع <code>PurePath.with_suffix</code>===
 
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead:<syntaxhighlight lang="python3">
 
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
 
 
 
>>> p.with_suffix('.bz2')
 
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
 
 
 
>>> p = PureWindowsPath('README')
 
 
 
>>> p.with_suffix('.txt')
 
PureWindowsPath('README.txt')
 
</syntaxhighlight>
 
 
==التقابل مع الوحدة <code>os</code>==
 
==التقابل مع الوحدة <code>os</code>==
 
يبيّن الجدول التالي تقابل عدة توابع من الوحدة <code>os</code>  مع ما يقابلها و يكافئها من الوحدة  <code>PurePath</code>.
 
يبيّن الجدول التالي تقابل عدة توابع من الوحدة <code>os</code>  مع ما يقابلها و يكافئها من الوحدة  <code>PurePath</code>.
 
<span> </span>
 
 
{| class="wikitable"
 
{| class="wikitable"
 
!الوحدة os والوحدة os.path
 
!الوحدة os والوحدة os.path
 
!الوحدة pathlib
 
!الوحدة pathlib
 
|-
 
|-
|os.path.isabs()
+
|‎os.path.isabs()
|PurePath.is_absolute()
+
|PurePath.is_absolute()
 
|-
 
|-
|os.path.join()
+
|os.path.join()
|PurePath.joinpath()
+
|PurePath.joinpath()
 
|-
 
|-
|os.path.basename()
+
|os.path.basename()
 
|PurePath.name
 
|PurePath.name
 
|-
 
|-
|os.path.dirname()
+
|os.path.dirname()
 
|PurePath.parent
 
|PurePath.parent
 
|-
 
|-
|os.path.splitext()
+
|os.path.splitext()
|PurePath.suffix
+
|PurePath.suffix‎
 
|}
 
|}

المراجعة الحالية بتاريخ 11:32، 12 أغسطس 2018

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

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

يمكن للمسارات النقية أن تكون مفيدةٌ في بعض الحالات الخاصة المماثلة لما يلي:

  1. لمعالجة مسارات نظام ويندوز أثناء العمل على جهاز يعمل بنظام يونكس (والعكس بالعكس)، حيث لا يمكنك إنشاء كائن من الصنف WindowsPath عند العمل على نظام يونكس، ولكن يمكنك إنشاء كائن من الصنف PureWindowsPath.
  2. عليك التأكد بأن الشيفرة البرمجية تعالج المسارات فقط دون الوصول الفعلي إلى نظام التشغيل، ففي هذه الحالة، يمكنك إنشاء كائن من أحد الأصناف النقية، فهي لا تملك عمليات تسمح لها بالوصول إلى نظام التشغيل.

يمكنك إنشاء كائنات من أيٍّ من الأصناف المذكورة آنفًا على أي نظام تشغيل، وذلك لأنها لا تقوم بأي استدعاءات من النظام.

الاستخدام الأساسي للصنف PurePath

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

عند استخدام الصنف العام المجرد PurePath فإن الصنف الحقيقي للكائن المنشأ منه يعتمد على نظام التشغيل، فهو إما PurePosixPath أو PureWindowsPath

>>> PurePath('setup.py')      # يتم العمل على نظام يونكس
PurePosixPath('setup.py')

يمكن لكل عنصر من الوسيط pathsegments أن يكون سلسلة نصية تمثل قطعة مسار أو كائنًا ينفذ الواجهة os.PathLike التي تعيد سلسلة نصية، كما يمكن أن تكون كائن مسار آخر.

>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')

>>> PurePath(Path('foo'), Path('bar'))
PurePosixPath('foo/bar')

ويُفترض وجود المسار '.' عندما يكون الوسيط pathsegments  خاليًا:

>>> PurePath()
PurePosixPath('.')

عندما تُعطى عدة مسارات مطلقة، يُعتبر المسار الأخير على أنه مسار المبدأ(anchor) (بشكل مطابق لسلوك التابع os.path.join)

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')

>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')

عند العمل في مسارات ويندوز، فإن تغيير مجلد الجذر المحلي لا يؤدي إلى إهمال إعدادات القرص السابق

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')

يُضمّ رمز الشرطة '/' المكرر وكذلك النقطة المفردة، إلا أنه لا يُتعامل بالمثل مع النقطتين المزدوجتين '..' وذلك لأن هذا قد يغير دلالة المسار في حال استخدام الروابط الرمزية:

>>> PurePath('foo//bar')
PurePosixPath('foo/bar')

>>> PurePath('foo/./bar')
PurePosixPath('foo/bar')

>>> PurePath('foo/../bar')
PurePosixPath('foo/../bar')

قد يبدو للوهلة الأولى أن المسار ‎‎‎PurePosixPath('foo/../bar')‎‎ يجب أن يكون مساويًا للمسار PurePosixPath('bar')‎ ولكن ذلك غير صحيح إذا كان المجلد 'foo' رابطًا رمزيًّا (symbolic link) لمجلد آخر.

تقوم كائنات المسار النقي بتنفيذ الواجهة os.PathLike، بما يسمح باستخدامها في أي مكان تُقبل في هذه الواجهة.

تغيرت في النسخة 3.6: أُضيف الدعم للواجهة os.PathLike

الخصائص الأساسية لكائنات الصنف PurePath

تتميز أصناف المسارات بكونها ثابتة immutable و قابلة للتهشير hashable، كما يمكن مقارنة وترتيب كائنات المسارات التي تكون من نفس النكهة مع مراعاة خصائص المسارات الخاصة بكل نكهة

>>> PurePosixPath('foo') == PurePosixPath('FOO')
False

>>> PureWindowsPath('foo') == PureWindowsPath('FOO')
True

>>> PureWindowsPath('FOO') in { PureWindowsPath('foo') } #لاحظ أن نظام ويندوز لا يهتم بحالة الحرف الإنكليزي في اسم الملف
True

>>> PureWindowsPath('C:') < PureWindowsPath('d:')
True

لا يمكن مقارنة كائنات المسارات من نكهات مختلفة ومن ثمّ فإن ترتيب عناصر من كائنات مسارات مختلفة النكهات غير ممكن.

>>> PureWindowsPath('foo') == PurePosixPath('foo')
False

>>> PureWindowsPath('foo') < PurePosixPath('foo')
Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'

العمليات المُعاد تعريفها للصنف PurePath

تساعد عملية القسمة '/' في إنشاء مسارات الأبناء، بشكل مشابه لما يقوم به التابع os.path.join

>>> p = PurePath('/etc')
>>> p
PurePosixPath('/etc')

>>> p / 'init.d' / 'apache2'
PurePosixPath('/etc/init.d/apache2')

>>> q = PurePath('bin')
>>> '/usr' / q
PurePosixPath('/usr/bin')

يمكن استخدام كائنات المسارات في أي مكان يقبل كائنًا ينفذ الواجهة os.PathLike

>>> import os

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

>>> os.fspath(p)
'/etc'

يعطي التمثيل النصي لكائن المسار المسار الخام ضمن نظام الملفات (فمثلًا تُستخدم الشرطة الخلفية المضاعفة في ويندوز، والشرطة الأمامية المفردة في لينوكس لفصل المسارات الآباء عن الأبناء).

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

>>> str(p)

'/etc'

>>> p = PureWindowsPath('c:/Program Files')

>>> str(p)

'c:\\Program Files'

يعطي تمرير كائن مسار إلى التابع bytes المسار الخام كما هو موجود في نظام الملفات ككائن من النوع bytes وذلك بنفس طريقة التابع os.fsencode

>>> bytes(p)

b'/etc'

مثال آخر في نظام ويندوز

>>> p=Path('C:\\Program Files\\Everything\\Everything.exe')
>>> p
WindowsPath('C:/Program Files/Everything/Everything.exe')
>>> bytes(p)
b'C:\\Program Files\\Everything\\Everything.exe'
>>> os.fsencode(p)
b'C:\\Program Files\\Everything\\Everything.exe'

ملاحظة:

يُستحسن استدعاء التابع bytes ضمن نظام يونكس فقط، فترميز unicode في نظام ويندوز هو التمثيل الأساسي لمسارات نظام الملفات.

المتغيرات الأعضاء في الصنف PurePath

المتغير PurePath.anchor

يجمع اسم السواقة مع مسار الجذر

المتغير PurePath.drive

سلسلة نصية تمثل اسم السواقة أو الحرف الممثل لها (في حال وجوده ضمن المسار)

المتغير PurePath.name

سلسة نصية تُمثّل العنصر الأخير في المسار، مع استثناء السواقة ومسارالجذر إن وُجد أي منهما

المتغير PurePath.parts

يمثّل هذا المتغير tuple لعناصر المسار

المتغير PurePath.parent

الأب المنطقي للمسار الحالي

المتغير PurePath.parents

سلسة غير قابلة للتعديل (immutable) تُعطي وصولًا للآباء المنطقية للمسار

المتغير PurePath.root

سلسلة نصية تمثل مسار الجذر (المحلي أو العام) إن وُجد.

المتغير PurePath.suffix

امتداد الملف لآخر عنصر في المسار (إن كان له امتداد)

المتغير PurePath.suffixes

قائمة بامتدادات الملف الذي يشير إليه المسار.

المتغير PurePath.stem

آخر عنصر في المسار، دون امتداده

التوابع الأعضاء في الصنف PurePath

التابع PurePath.as_posix

يعيد تمثيلًا نصّيًّا للمسار مع استخدام الشرطة الأمامية للفصل بين المستويات في المسار '/'

التابع PurePath.as_uri

يعيد تمثيل المسار بتمثيل URI، ويرمي استثناء ValueError إن لم يكن المسار بصيغته المطلقة.

التابع PurePath.is_absolute

يعيد True إن كان كائن المسار يشير إلى مسارٍ مطلق. يكون المسار مطلقًا إن كان يحتوي على مسار جذر واسم السواقة (إن كانت نكهة كائن المسار تسمح بذلك).

التابع PurePath.is_reserved

يعيد True إذا كان المسار يُعتبر اسمًا مجوزًا في النظام.

التابع PurePath.joinpath

يُؤدي استدعاء هذا التابع إلى دمج المسار مع كلٍّ من الوسطاء الممررة له بالترتيب.

التابع PurePath.match

يختبر هذا التابع مطابقة المسار لنمط اختبار المطابقة glob المعطى.

التابع PurePath.relative_to

يحسب هذا التابع كيف يبدو المسار عند اعتباره منسوبًا إلى المسار المُعطى، ويرمي الاستثناء ValueError عندما لا يُمكن نسب المسار الأصلي للمسار المُعطى.

التابع PurePath.with_name

يعيد مسارًا جديدًا مع تغيير اسم العنصر الأخير، ويرمي استثناءً ValueError إن لم يكن في المسار الأصلي اسم ملف.

التابع PurePath.with_suffix

يعيد مسارًا جديدًا بعد تغيير امتداد الملف، وإن لم يكن للملف امتداد فإنه يضيف الامتداد المُعطى إليه.

التقابل مع الوحدة os

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

الوحدة os والوحدة os.path الوحدة pathlib
‎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‎