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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 160: سطر 160:
 
يمثّل هذا المتغير <code>tuple</code> لعناصر المسار<syntaxhighlight lang="python3">
 
يمثّل هذا المتغير <code>tuple</code> لعناصر المسار<syntaxhighlight lang="python3">
 
>>> p = PurePath('/usr/bin/python3')
 
>>> p = PurePath('/usr/bin/python3')
 
 
>>> p.parts
 
>>> p.parts
 
 
('/', 'usr', 'bin', 'python3')
 
('/', 'usr', 'bin', 'python3')
  
سطر 168: سطر 166:
  
 
>>> p.parts
 
>>> p.parts
 
 
('c:\\', 'Program Files', 'PSF')
 
('c:\\', 'Program Files', 'PSF')
 
  
 
</syntaxhighlight>(لاحظ في المثال السابق كيف تم دمج اسم السواقة مع مسار الجذر المحلي في جزء واحد)
 
</syntaxhighlight>(لاحظ في المثال السابق كيف تم دمج اسم السواقة مع مسار الجذر المحلي في جزء واحد)
  
المتغير <code>PurePath.drive</code>
+
=====المتغير <code>PurePath.drive</code>=====
  
 
سلسلة نصية تمثل اسم السواقة أو الحرف الممثل لها (في حال وجوده ضمن المسار)<syntaxhighlight lang="python3">
 
سلسلة نصية تمثل اسم السواقة أو الحرف الممثل لها (في حال وجوده ضمن المسار)<syntaxhighlight lang="python3">
سطر 188: سطر 184:
  
 
</syntaxhighlight>كما يتم يتم اعتبار مجلدات UNC المشارَكةاسمًا للسواقة  
 
</syntaxhighlight>كما يتم يتم اعتبار مجلدات UNC المشارَكةاسمًا للسواقة  
 
+
<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('//host/share/foo.txt').drive
 
>>> PureWindowsPath('//host/share/foo.txt').drive
 
 
'\\\\host\\share'
 
'\\\\host\\share'
  
PurePath.root
+
=====المتغير <code>PurePath.root</code>=====
 
+
سلسلة نصية تمثل مسار الجذر (المحلي أو العام) في حال وجوده ضمن المسار
A string representing the (local or global) root, if any:
 
 
 
>>>
 
  
 +
<syntaxhighlight lang="python3">
 
>>> PureWindowsPath('c:/Program Files/').root
 
>>> PureWindowsPath('c:/Program Files/').root
 
 
'\\'
 
'\\'
 
 
>>> PureWindowsPath('c:Program Files/').root
 
>>> PureWindowsPath('c:Program Files/').root
 
<nowiki>''</nowiki>
 
 
 
>>> PurePosixPath('/etc').root
 
>>> PurePosixPath('/etc').root
 
 
'/'
 
'/'
 +
</syntaxhighlight>
  
 
UNC shares always have a root:
 
UNC shares always have a root:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PureWindowsPath('//host/share').root
 
>>> PureWindowsPath('//host/share').root
 
 
'\\'
 
'\\'
 +
</syntaxhighlight>
  
 
PurePath.anchor
 
PurePath.anchor
سطر 223: سطر 210:
 
The concatenation of the drive and root:
 
The concatenation of the drive and root:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PureWindowsPath('c:/Program Files/').anchor
 
>>> PureWindowsPath('c:/Program Files/').anchor
 
 
'c:\\'
 
'c:\\'
  
 
>>> PureWindowsPath('c:Program Files/').anchor
 
>>> PureWindowsPath('c:Program Files/').anchor
 
 
'c:'
 
'c:'
  
 
>>> PurePosixPath('/etc').anchor
 
>>> PurePosixPath('/etc').anchor
 
 
'/'
 
'/'
  
 
>>> PureWindowsPath('//host/share').anchor
 
>>> PureWindowsPath('//host/share').anchor
 
 
'\\\\host\\share\\'
 
'\\\\host\\share\\'
 +
</syntaxhighlight>
  
 
PurePath.parents
 
PurePath.parents
سطر 245: سطر 228:
 
An immutable sequence providing access to the logical ancestors of the path:
 
An immutable sequence providing access to the logical ancestors of the path:
  
>>>
+
<syntaxhighlight lang="python3">
  
 
>>> p = PureWindowsPath('c:/foo/bar/setup.py')
 
>>> p = PureWindowsPath('c:/foo/bar/setup.py')
  
 
>>> p.parents[0]
 
>>> p.parents[0]
 
 
PureWindowsPath('c:/foo/bar')
 
PureWindowsPath('c:/foo/bar')
  
 
>>> p.parents[1]
 
>>> p.parents[1]
 
 
PureWindowsPath('c:/foo')
 
PureWindowsPath('c:/foo')
  
 
>>> p.parents[2]
 
>>> p.parents[2]
 
 
PureWindowsPath('c:/')
 
PureWindowsPath('c:/')
 +
</syntaxhighlight>
  
 
PurePath.parent
 
PurePath.parent
 
 
The logical parent of the path:
 
The logical parent of the path:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> p = PurePosixPath('/a/b/c/d')
 
>>> p = PurePosixPath('/a/b/c/d')
  
 
>>> p.parent
 
>>> p.parent
 
 
PurePosixPath('/a/b/c')
 
PurePosixPath('/a/b/c')
 +
</syntaxhighlight>
  
 
You cannot go past an anchor, or empty path:
 
You cannot go past an anchor, or empty path:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> p = PurePosixPath('/')
 
>>> p = PurePosixPath('/')
  
 
>>> p.parent
 
>>> p.parent
 
 
PurePosixPath('/')
 
PurePosixPath('/')
  
سطر 286: سطر 263:
  
 
>>> p.parent
 
>>> p.parent
 
 
PurePosixPath('.')
 
PurePosixPath('.')
 +
</syntaxhighlight>
  
 
Note
 
Note
سطر 293: سطر 270:
 
This is a purely lexical operation, hence the following behaviour:
 
This is a purely lexical operation, hence the following behaviour:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> p = PurePosixPath('foo/..')
 
>>> p = PurePosixPath('foo/..')
  
 
>>> p.parent
 
>>> p.parent
 
 
PurePosixPath('foo')
 
PurePosixPath('foo')
 
+
</syntaxhighlight>
 
If you want to walk an arbitrary filesystem path upwards, it is recommended to first call Path.resolve()so as to resolve symlinks and eliminate “..” components.
 
If you want to walk an arbitrary filesystem path upwards, it is recommended to first call Path.resolve()so as to resolve symlinks and eliminate “..” components.
  
سطر 307: سطر 282:
 
A string representing the final path component, excluding the drive and root, if any:
 
A string representing the final path component, excluding the drive and root, if any:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PurePosixPath('my/library/setup.py').name
 
>>> PurePosixPath('my/library/setup.py').name
 
 
'setup.py'
 
'setup.py'
 +
</syntaxhighlight>
  
 
UNC drive names are not considered:
 
UNC drive names are not considered:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PureWindowsPath('//some/share/setup.py').name
 
>>> PureWindowsPath('//some/share/setup.py').name
 
 
'setup.py'
 
'setup.py'
  
 
>>> PureWindowsPath('//some/share').name
 
>>> PureWindowsPath('//some/share').name
 
+
''
<nowiki>''</nowiki>
+
</syntaxhighlight>
  
 
PurePath.suffix
 
PurePath.suffix
سطر 329: سطر 301:
 
The file extension of the final component, if any:
 
The file extension of the final component, if any:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PurePosixPath('my/library/setup.py').suffix
 
>>> PurePosixPath('my/library/setup.py').suffix
 
 
'.py'
 
'.py'
  
 
>>> PurePosixPath('my/library.tar.gz').suffix
 
>>> PurePosixPath('my/library.tar.gz').suffix
 
 
'.gz'
 
'.gz'
  
 
>>> PurePosixPath('my/library').suffix
 
>>> PurePosixPath('my/library').suffix
 
+
''
<nowiki>''</nowiki>
+
</syntaxhighlight>
  
 
PurePath.suffixes
 
PurePath.suffixes
سطر 347: سطر 316:
 
A list of the path’s file extensions:
 
A list of the path’s file extensions:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PurePosixPath('my/library.tar.gar').suffixes
 
>>> PurePosixPath('my/library.tar.gar').suffixes
 
 
['.tar', '.gar']
 
['.tar', '.gar']
  
 
>>> PurePosixPath('my/library.tar.gz').suffixes
 
>>> PurePosixPath('my/library.tar.gz').suffixes
 
 
['.tar', '.gz']
 
['.tar', '.gz']
  
 
>>> PurePosixPath('my/library').suffixes
 
>>> PurePosixPath('my/library').suffixes
 
 
[]
 
[]
 
+
</syntaxhighlight>
 
PurePath.stem
 
PurePath.stem
  
 
The final path component, without its suffix:
 
The final path component, without its suffix:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PurePosixPath('my/library.tar.gz').stem
 
>>> PurePosixPath('my/library.tar.gz').stem
 
 
'library.tar'
 
'library.tar'
  
 
>>> PurePosixPath('my/library.tar').stem
 
>>> PurePosixPath('my/library.tar').stem
 
 
'library'
 
'library'
  
 
>>> PurePosixPath('my/library').stem
 
>>> PurePosixPath('my/library').stem
 
 
'library'
 
'library'
 
+
</syntaxhighlight>
 
PurePath.as_posix()
 
PurePath.as_posix()
  
 
Return a string representation of the path with forward slashes (/):
 
Return a string representation of the path with forward slashes (/):
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> p = PureWindowsPath('c:\\windows')
 
>>> p = PureWindowsPath('c:\\windows')
  
 
>>> str(p)
 
>>> str(p)
 
 
'c:\\windows'
 
'c:\\windows'
  
 
>>> p.as_posix()
 
>>> p.as_posix()
 
 
'c:/windows'
 
'c:/windows'
 
+
</syntaxhighlight>
 
PurePath.as_uri()
 
PurePath.as_uri()
  
 
Represent the path as a file URI. ValueError is raised if the path isn’t absolute.
 
Represent the path as a file URI. ValueError is raised if the path isn’t absolute.
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> p = PurePosixPath('/etc/passwd')
 
>>> p = PurePosixPath('/etc/passwd')
  
 
>>> p.as_uri()
 
>>> p.as_uri()
 
 
'file:///etc/passwd'
 
'file:///etc/passwd'
  
سطر 410: سطر 366:
  
 
>>> p.as_uri()
 
>>> p.as_uri()
 
 
'file:///c:/Windows'
 
'file:///c:/Windows'
 
+
</syntaxhighlight>
 
PurePath.is_absolute()
 
PurePath.is_absolute()
  
 
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:
 
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()
 
>>> PurePosixPath('/a/b').is_absolute()
 
 
True
 
True
  
 
>>> PurePosixPath('a/b').is_absolute()
 
>>> PurePosixPath('a/b').is_absolute()
 
 
False
 
False
  
 
>>> PureWindowsPath('c:/a/b').is_absolute()
 
>>> PureWindowsPath('c:/a/b').is_absolute()
 
 
True
 
True
  
 
>>> PureWindowsPath('/a/b').is_absolute()
 
>>> PureWindowsPath('/a/b').is_absolute()
 
 
False
 
False
  
 
>>> PureWindowsPath('c:').is_absolute()
 
>>> PureWindowsPath('c:').is_absolute()
 
 
False
 
False
  
 
>>> PureWindowsPath('//some/share').is_absolute()
 
>>> PureWindowsPath('//some/share').is_absolute()
 
 
True
 
True
 
+
</syntaxhighlight>
 
PurePath.is_reserved()
 
PurePath.is_reserved()
  
 
With PureWindowsPath, return True if the path is considered reserved under Windows, False otherwise. With PurePosixPath, False is always returned.
 
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()
 
>>> PureWindowsPath('nul').is_reserved()
 
 
True
 
True
  
 
>>> PurePosixPath('nul').is_reserved()
 
>>> PurePosixPath('nul').is_reserved()
 
 
False
 
False
 +
</syntaxhighlight>
  
 
File system calls on reserved paths can fail mysteriously or have unintended effects.
 
File system calls on reserved paths can fail mysteriously or have unintended effects.
سطر 463: سطر 409:
 
Calling this method is equivalent to combining the path with each of the other arguments in turn:
 
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').joinpath('passwd')
 
 
PurePosixPath('/etc/passwd')
 
PurePosixPath('/etc/passwd')
  
 
>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))
 
>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))
 
 
PurePosixPath('/etc/passwd')
 
PurePosixPath('/etc/passwd')
  
 
>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
 
>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
 
 
PurePosixPath('/etc/init.d/apache2')
 
PurePosixPath('/etc/init.d/apache2')
  
 
>>> PureWindowsPath('c:').joinpath('/Program Files')
 
>>> PureWindowsPath('c:').joinpath('/Program Files')
 
 
PureWindowsPath('c:/Program Files')
 
PureWindowsPath('c:/Program Files')
 
+
</syntaxhighlight>
 
PurePath.match(pattern)
 
PurePath.match(pattern)
  
سطر 487: سطر 428:
 
If pattern is relative, the path can be either relative or absolute, and matching is done from the right:
 
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')
 
>>> PurePath('a/b.py').match('*.py')
 
 
True
 
True
  
 
>>> PurePath('/a/b/c.py').match('b/*.py')
 
>>> PurePath('/a/b/c.py').match('b/*.py')
 
 
True
 
True
  
 
>>> PurePath('/a/b/c.py').match('a/*.py')
 
>>> PurePath('/a/b/c.py').match('a/*.py')
 
 
False
 
False
 +
</syntaxhighlight>
  
 
If pattern is absolute, the path must be absolute, and the whole path must match:
 
If pattern is absolute, the path must be absolute, and the whole path must match:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PurePath('/a.py').match('/*.py')
 
>>> PurePath('/a.py').match('/*.py')
 
 
True
 
True
  
 
>>> PurePath('a/b.py').match('/*.py')
 
>>> PurePath('a/b.py').match('/*.py')
 
 
False
 
False
 +
</syntaxhighlight>
  
 
As with other methods, case-sensitivity is observed:
 
As with other methods, case-sensitivity is observed:
  
>>>
+
<syntaxhighlight lang="python3">
 
 
 
>>> PureWindowsPath('b.py').match('*.PY')
 
>>> PureWindowsPath('b.py').match('*.PY')
 
 
True
 
True
 
+
</syntaxhighlight>
 
PurePath.relative_to(*other)
 
PurePath.relative_to(*other)
  
 
Compute a version of this path relative to the path represented by other. If it’s impossible, ValueError is raised:
 
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 = PurePosixPath('/etc/passwd')
  
 
>>> p.relative_to('/')
 
>>> p.relative_to('/')
 
 
PurePosixPath('etc/passwd')
 
PurePosixPath('etc/passwd')
  
 
>>> p.relative_to('/etc')
 
>>> p.relative_to('/etc')
 
 
PurePosixPath('passwd')
 
PurePosixPath('passwd')
  
سطر 540: سطر 471:
  
 
Traceback (most recent call last):
 
Traceback (most recent call last):
 
 
 File "<stdin>", line 1, in <module>
 
 File "<stdin>", line 1, in <module>
 
 
 File "pathlib.py", line 694, in relative_to
 
 File "pathlib.py", line 694, in relative_to
 
 
   .format(str(self), str(formatted)))
 
   .format(str(self), str(formatted)))
 
 
ValueError: '/etc/passwd' does not start with '/usr'
 
ValueError: '/etc/passwd' does not start with '/usr'
 +
</syntaxhighlight>
  
 
PurePath.with_name(name)
 
PurePath.with_name(name)
سطر 553: سطر 481:
 
Return a new path with the name changed. If the original path doesn’t have a name, ValueError is raised:
 
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 = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
 
 
>>> p.with_name('setup.py')
 
>>> p.with_name('setup.py')
 
 
PureWindowsPath('c:/Downloads/setup.py')
 
PureWindowsPath('c:/Downloads/setup.py')
  
 
>>> p = PureWindowsPath('c:/')
 
>>> p = PureWindowsPath('c:/')
 
 
>>> p.with_name('setup.py')
 
>>> p.with_name('setup.py')
 
 
Traceback (most recent call last):
 
Traceback (most recent call last):
 
 
 File "<stdin>", line 1, in <module>
 
 File "<stdin>", line 1, in <module>
 
 
 File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
 
 File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
 
 
   raise ValueError("%r has an empty name" % (self,))
 
   raise ValueError("%r has an empty name" % (self,))
 
 
ValueError: PureWindowsPath('c:/') has an empty name
 
ValueError: PureWindowsPath('c:/') has an empty name
 
+
</syntaxhighlight>
 
PurePath.with_suffix(suffix)
 
PurePath.with_suffix(suffix)
  
 
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead:
 
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 = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
  
 
>>> p.with_suffix('.bz2')
 
>>> p.with_suffix('.bz2')
 
 
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
 
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
  
سطر 590: سطر 507:
  
 
>>> p.with_suffix('.txt')
 
>>> p.with_suffix('.txt')
 +
PureWindowsPath('README.txt')
 +
</syntaxhighlight>
  
PureWindowsPath('README.txt')
 
 
===التقابل مع الوحدة os===
 
===التقابل مع الوحدة os===
 
يبيّن الجدول التالي تقابل عدة توابع من الوحدة <code>os</code>  مع ما يقابلها و يكافئها من الوحدة  <code>PurePath</code>.
 
يبيّن الجدول التالي تقابل عدة توابع من الوحدة <code>os</code>  مع ما يقابلها و يكافئها من الوحدة  <code>PurePath</code>.

مراجعة 17:45، 1 أغسطس 2018

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

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

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

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

11.1.2.2. Operators

11.1.2.3. Accessing individual parts

11.1.2.4. Methods and properties

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

الاستخدام الأساسي للصنف 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.parts

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

>>> p = PurePath('/usr/bin/python3')
>>> p.parts
('/', 'usr', 'bin', 'python3')

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

>>> p.parts
('c:\\', 'Program Files', 'PSF')

(لاحظ في المثال السابق كيف تم دمج اسم السواقة مع مسار الجذر المحلي في جزء واحد)

المتغير PurePath.drive

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

>>> PureWindowsPath('c:/Program Files/').drive
'c:'

>>> PureWindowsPath('/Program Files/').drive
''

>>> PurePosixPath('/etc').drive
''

كما يتم يتم اعتبار مجلدات UNC المشارَكةاسمًا للسواقة

>>> PureWindowsPath('//host/share/foo.txt').drive
'\\\\host\\share'

=====المتغير <code>PurePath.root</code>=====
سلسلة نصية تمثل مسار الجذر (المحلي أو العام) في حال وجوده ضمن المسار

<syntaxhighlight lang="python3">
>>> PureWindowsPath('c:/Program Files/').root
'\\'
>>> PureWindowsPath('c:Program Files/').root
>>> PurePosixPath('/etc').root
'/'

UNC shares always have a root:

>>> PureWindowsPath('//host/share').root
'\\'

PurePath.anchor

The concatenation of the drive and root:

>>> PureWindowsPath('c:/Program Files/').anchor
'c:\\'

>>> PureWindowsPath('c:Program Files/').anchor
'c:'

>>> PurePosixPath('/etc').anchor
'/'

>>> PureWindowsPath('//host/share').anchor
'\\\\host\\share\\'

PurePath.parents

An immutable sequence providing access to the logical ancestors of the path:

>>> 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:/')

PurePath.parent The logical parent of the path:

>>> p = PurePosixPath('/a/b/c/d')

>>> p.parent
PurePosixPath('/a/b/c')

You cannot go past an anchor, or empty path:

>>> p = PurePosixPath('/')

>>> p.parent
PurePosixPath('/')

>>> p = PurePosixPath('.')

>>> p.parent
PurePosixPath('.')

Note

This is a purely lexical operation, hence the following behaviour:

>>> p = PurePosixPath('foo/..')

>>> p.parent
PurePosixPath('foo')

If you want to walk an arbitrary filesystem path upwards, it is recommended to first call Path.resolve()so as to resolve symlinks and eliminate “..” components.

PurePath.name

A string representing the final path component, excluding the drive and root, if any:

>>> PurePosixPath('my/library/setup.py').name
'setup.py'

UNC drive names are not considered:

>>> PureWindowsPath('//some/share/setup.py').name
'setup.py'

>>> PureWindowsPath('//some/share').name
''

PurePath.suffix

The file extension of the final component, if any:

>>> PurePosixPath('my/library/setup.py').suffix
'.py'

>>> PurePosixPath('my/library.tar.gz').suffix
'.gz'

>>> PurePosixPath('my/library').suffix
''

PurePath.suffixes

A list of the path’s file extensions:

>>> PurePosixPath('my/library.tar.gar').suffixes
['.tar', '.gar']

>>> PurePosixPath('my/library.tar.gz').suffixes
['.tar', '.gz']

>>> PurePosixPath('my/library').suffixes
[]

PurePath.stem

The final path component, without its suffix:

>>> PurePosixPath('my/library.tar.gz').stem
'library.tar'

>>> PurePosixPath('my/library.tar').stem
'library'

>>> PurePosixPath('my/library').stem
'library'

PurePath.as_posix()

Return a string representation of the path with forward slashes (/):

>>> p = PureWindowsPath('c:\\windows')

>>> str(p)
'c:\\windows'

>>> p.as_posix()
'c:/windows'

PurePath.as_uri()

Represent the path as a file URI. ValueError is raised if the path isn’t absolute.

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

>>> p.as_uri()
'file:///etc/passwd'

>>> p = PureWindowsPath('c:/Windows')

>>> p.as_uri()
'file:///c:/Windows'

PurePath.is_absolute()

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:

>>> 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

PurePath.is_reserved()

With PureWindowsPath, return True if the path is considered reserved under Windows, False otherwise. With PurePosixPath, False is always returned.

>>> PureWindowsPath('nul').is_reserved()
True

>>> PurePosixPath('nul').is_reserved()
False

File system calls on reserved paths can fail mysteriously or have unintended effects.

PurePath.joinpath(*other)

Calling this method is equivalent to combining the path with each of the other arguments in turn:

>>> 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')

PurePath.match(pattern)

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:

>>> 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

If pattern is absolute, the path must be absolute, and the whole path must match:

>>> PurePath('/a.py').match('/*.py')
True

>>> PurePath('a/b.py').match('/*.py')
False

As with other methods, case-sensitivity is observed:

>>> PureWindowsPath('b.py').match('*.PY')
True

PurePath.relative_to(*other)

Compute a version of this path relative to the path represented by other. If it’s impossible, ValueError is raised:

>>> 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'

PurePath.with_name(name)

Return a new path with the name changed. If the original path doesn’t have a name, ValueError is raised:

>>> 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

PurePath.with_suffix(suffix)

Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead:

>>> 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')

التقابل مع الوحدة 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