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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
 
(29 مراجعة متوسطة بواسطة نفس المستخدم غير معروضة)
سطر 1: سطر 1:
== الصنف <code>pathlib.Path</code> في بايثون ==
 
الاستخدام الأساسي
 
  
استيراد الصنف الأساسي:
+
==الصنف <code>pathlib.Path</code> في بايثون==
 +
هو الصنف العام للأصناف التي تتعامل مع المسارات مع السماح بالقيام بعمليات الإدخال والإخراج، وتُسمّى بأصناف المسارات الصلبة Concrete Paths.
 +
==البنية العامة==
 +
<syntaxhighlight lang="python3">
 +
pathlib.Path(*args,**kwargs)‎
 +
</syntaxhighlight>
 +
==المعاملات==
 +
===<code>args*</code>===
 +
يمكن تمرير عدد غير محدود من السلاسل النصية أو من كائنات من النوع <code>Path</code> حيث تمثل السلاسل النصية أسماء المجلدات والملفات بالترتيب من المستوى الأعلى للأدنى.
 +
===<code>kwargs**</code>===
 +
معامل غير مستخدم.
  
>>> from pathlib import Path
+
يمكنك مراجعة [https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/ المقال التالي] بخصوص هذا النوع من المعاملات.
 
+
==الاستخدام الأساسي==
تعداد المسارات الفرعية:
+
يمكن إنشاء كائن من الصنف <code>Path</code> بثلاث طرق مختلفة:
 
 
>>> 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.
 
{| class="wikitable"
 
|الوحدة 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
 
|}
 
 
 
== 11.1.3. Concrete paths ==
 
Concrete paths are subclasses of the pure path classes. In addition to operations provided by the latter, they also provide methods to do system calls on path objects. There are three ways to instantiate concrete paths:
 
 
 
class pathlib.Path(*pathsegments)
 
 
 
A subclass of PurePath, this class represents concrete paths of the system’s path flavour (instantiating it creates either a PosixPath or a WindowsPath):
 
 
 
>>>
 
  
 +
1- باستخدام باني الصنف العام Path، وتعتمد هذه الطريقة على إنشاء كائن من الصنف <code>PosixPath</code> أو <code>WindowsPath</code> حسب نظام التشغيل الذي يتم تفسير الشيفرة عليه:<syntaxhighlight lang="python3">
 
>>> Path('setup.py')
 
>>> Path('setup.py')
  
 
PosixPath('setup.py')
 
PosixPath('setup.py')
  
pathsegments is specified similarly to PurePath.
 
 
class pathlib.PosixPath(*pathsegments)
 
 
A subclass of Path and PurePosixPath, this class represents concrete non-Windows filesystem paths:
 
 
>>>
 
  
 +
</syntaxhighlight>2- باستخدام باني الصنف الخاص بالأنظمة المغايرة لويندوز <code>PosixPath</code><syntaxhighlight lang="python3">
 
>>> PosixPath('/etc')
 
>>> PosixPath('/etc')
  
 
PosixPath('/etc')
 
PosixPath('/etc')
  
pathsegments is specified similarly to PurePath.
 
 
class pathlib.WindowsPath(*pathsegments)
 
 
A subclass of Path and PureWindowsPath, this class represents concrete Windows filesystem paths:
 
 
>>>
 
  
 +
</syntaxhighlight>3- باستخدام باني الصنف الخاص بنظام ويندوز <code>WindowsPath</code><syntaxhighlight lang="python3">
 
>>> WindowsPath('c:/Program Files/')
 
>>> WindowsPath('c:/Program Files/')
  
 
WindowsPath('c:/Program Files')
 
WindowsPath('c:/Program Files')
  
pathsegments is specified similarly to PurePath.
 
 
You can only instantiate the class flavour that corresponds to your system (allowing system calls on non-compatible path flavours could lead to bugs or failures in your application):
 
  
 +
</syntaxhighlight>كما تجدر الإشارة إلى أنه لا يُسمح لإنشاء أحد الصنفين <code>PosixPath</code>  أو <code>WindowsPath</code> على نظام مغاير له، حيث يرمي الباني الخاص بالصنف الاستثناء <code>NotImplementedError</code> كما يوضح المثال التالي:<syntaxhighlight lang="python3">
 
>>>
 
>>>
  
سطر 197: سطر 40:
  
 
>>> os.name
 
>>> os.name
 
+
'posix'     #نظام التشغيل الذي يتم تفسير الشيفرة عليه مغاير لويندوز
'posix'
 
  
 
>>> Path('setup.py')
 
>>> Path('setup.py')
 
 
PosixPath('setup.py')
 
PosixPath('setup.py')
  
 
>>> PosixPath('setup.py')
 
>>> PosixPath('setup.py')
 
 
PosixPath('setup.py')
 
PosixPath('setup.py')
  
>>> WindowsPath('setup.py')
+
>>> WindowsPath('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 "pathlib.py", line 798, in __new__
 
 File "pathlib.py", line 798, in __new__
 
 
   % (cls.__name__,))
 
   % (cls.__name__,))
 
 
NotImplementedError: cannot instantiate 'WindowsPath' on your system
 
NotImplementedError: cannot instantiate 'WindowsPath' on your system
 +
</syntaxhighlight>
 +
==أمثلة على الاستخدام==
 +
تُوضح الشيفرات التالية أمثلةً لاستخدام هذا الصنف:
  
=== 11.1.3.1. Methods ===
+
استيراد الصنف الأساسي من الوحدة <code>pathlib</code>:<syntaxhighlight lang="python3">
Concrete paths provide the following methods in addition to pure paths methods. Many of these methods can raise an OSError if a system call fails (for example because the path doesn’t exist):
+
>>> from pathlib import Path #استيراد الصنف الأساسي
  
classmethod Path.cwd()
+
</syntaxhighlight>ملاحظة: لن تتمكن من استيراد المكتبة على نسخ بايثون الأُقدم من 3.4 (استخدم [[Python/os.path|os.path]] بدلًا منها)
  
Return a new path object representing the current directory (as returned by os.getcwd()):
+
إنشاء كائن والتكرار على المجلدات والملفات التي بداخله باستخدام [[Python/list#List Comprehensions|list comprehension]]:<syntaxhighlight lang="python3">
  
>>>
+
>>> p = Path('.') #إنشاء كائن باستخدام باين الصنف العام
  
>>> Path.cwd()
+
>>> [x for x in p.iterdir() if x.is_dir()] #تعداد المجلدات الموجودة ضمن المسار الحالي
 +
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
 +
PosixPath('__pycache__'), PosixPath('build')]
  
PosixPath('/home/antoine/pathlib')
+
</syntaxhighlight>تعداد ملفات بايثون المصدرية في شجرة المجلدات الحالية باستخدام [[Python/pathlib/Path/glob|glob]]:<syntaxhighlight lang="python3">
 +
>>> list(p.glob('**/*.py'))
  
classmethod Path.home()
+
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 +
PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 +
PosixPath('build/lib/pathlib.py')]
  
Return a new path object representing the user’s home directory (as returned by os.path.expanduser()with ~ construct):
 
  
>>>
+
</syntaxhighlight>يمكن التنقّل ضمن شجرة المجلدات باستخدام عملية القسمة <code>/</code> المُعاد تعريفها:<syntaxhighlight lang="python3">
 +
>>> p = Path('/etc')
 +
>>> q = p / 'init.d' / 'reboot'
 +
>>> q
  
>>> Path.home()
+
PosixPath('/etc/init.d/reboot')
 +
>>> q.resolve()
  
PosixPath('/home/antoine')
+
PosixPath('/etc/rc.d/init.d/halt')
  
New in version 3.5.
 
  
Path.stat()
+
</syntaxhighlight>كما يمكن استخدام قائمة طويلة من التوابع للاستعلام عن خصائص المسار، مثل [[Python/pathlib/Path/exists|exists]] و [[Python/pathlib/Path/is dir|is_dir]]:<syntaxhighlight lang="python3">
 
+
>>> q.exists()
Return information about this path (similarly to os.stat()). The result is looked up at each call to this method.
 
 
 
>>>
 
 
 
>>> p = Path('setup.py')
 
 
 
>>> p.stat().st_size
 
 
 
956
 
 
 
>>> p.stat().st_mtime
 
 
 
1327883547.852554
 
 
 
Path.chmod(mode)
 
 
 
Change the file mode and permissions, like os.chmod():
 
 
 
>>>
 
 
 
>>> p = Path('setup.py')
 
 
 
>>> p.stat().st_mode
 
 
 
33277
 
 
 
>>> p.chmod(0o444)
 
 
 
>>> p.stat().st_mode
 
 
 
33060
 
 
 
Path.exists()
 
 
 
Whether the path points to an existing file or directory:
 
 
 
>>>
 
 
 
>>> Path('.').exists()
 
  
 
True
 
True
  
>>> Path('setup.py').exists()
+
>>> q.is_dir()
 
 
True
 
 
 
>>> Path('/etc').exists()
 
 
 
True
 
 
 
>>> Path('nonexistentfile').exists()
 
  
 
False
 
False
  
Note
 
  
If the path points to a symlink, exists() returns whether the symlink points to an existing file or directory.
+
</syntaxhighlight>يسمح التابع [[Python/pathlib/Path/open|open]] بفتح الملفات بشكل مشابه للتابع [[Python/open|open]] المضمن في بايثون:<syntaxhighlight lang="python3">
 
+
>>> with q.open() as f: f.readline()
Path.expanduser()
 
 
 
Return a new path with expanded ~ and ~user constructs, as returned by os.path.expanduser():
 
  
>>>
+
السطر الأول وأهلا بك
 +
السطر الأخير وداعا
  
>>> p = PosixPath('~/films/Monty Python')
 
  
>>> p.expanduser()
+
</syntaxhighlight>
 
+
==<span> </span>توابع الصنف <code>Path</code>==
PosixPath('/home/eric/films/Monty Python')
+
يتيح الصنف <code>Path</code> استخدام جميع التوابع الخاصة بالصنف <code>PurePath</code> وذلك لأنه يرث منه، ويضيف عليها التوابع التالية، التي تسمح بالقيام باستدعاءات النظام (system calls)، وترمي هذه التوابع استثناء <code>OSError</code> في حال فشل أيٍّ منها (كأن يكون المسار الذي يتم التعامل معه غير موجود مثلًا).
 
+
===[[Python/pathlib/Path/cwd|التابع <code>Path.cwd</code>]]===
New in version 3.5.
+
يعيد كائن مسار جديدًا يمثّل المسار الحالي (بشكل مشابه للتابع [[Python/os/getcwd|os.getcwd]])
 
+
===[[Python/pathlib/Path/home|التابع <code>Path.home</code>]]===
Path.glob(pattern)
+
يعيد كائن مسار جديدًا يمثّل المسار الرئيسي (home directory) للمستخدم (بشكل مشابه للتابع <code>[[Python/os/path/expanduser|os.path.expanduser]]</code> مع الوسيط <code>'~'</code>)
 
+
===[[Python/pathlib/Path/stat|التابع <code>Path.stat</code>]]===
Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):
+
يعيد معلومات عن المسار (مماثل للتابع <code>[[Python/os/stat|os.stat]]</code>) ، ويتم حساب المعلومات في كل مرة يتم فيها استدعاء التابع
 
+
===[[Python/pathlib/Path/chmod|التابع <code>Path.chmod</code>]]===
>>>
+
يقوم هذا التابع بتغيير نمط الملف وصلاحياته (مماثل للتابع <code>[[Python/os/chmod|os.chmod]]</code>)
 
+
===[[Python/pathlib/Path/exists|التابع <code>Path.exists</code>]]===
>>> sorted(Path('.').glob('*.py'))
+
يدلّ على كون المسار مشيرًا إلى ملف أو مجلد حقيقي (موجود على نظام التشغيل) أم لا.
 
+
===[[Python/pathlib/Path/expanduser|التابع <code>Path.expanduser</code>]]===
[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
+
يعيد كائن مسار جديد يمدد فيه المسار الرئيسي للمستخدم الممثل بالرمز <code>'~'</code>، مشابه للتابع <code>[[Python/os/path/expanduser|os.path.expanduser]]</code>
 
+
===[[Python/pathlib/Path/glob|التابع <code>Path.glob</code>]]===
>>> sorted(Path('.').glob('*/*.py'))
+
يعيد جميع الملفات التي تحقق نمط [[wikipedia:Glob_(programming)|glob]] المعطى، مهما كان نوع الملف.
 
+
===[[Python/pathlib/Path/group|التابع <code>Path.group</code>]]===
[PosixPath('docs/conf.py')]
+
يعيد هذا التابع اسم المجموعة التي ينتمي لها الملف، ويرمي بالخطأ <code>KeyError</code>  إذا كان معرف المجموعة <code>[https://whatis.techtarget.com/definition/GID-group-ID-or-global-index-file gid]</code> الخاص بالملف غير موجود في قاعدة بيانات النظام.
 
+
===[[Python/pathlib/Path/is dir|التابع <code>Path.is_dir</code>]]===
The “**” pattern means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing:
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى مجلد (أو إلى اختصار يشير إلى مجلد)، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 
+
===[[Python/pathlib/Path/is file|التابع <code>Path.is_file</code>]]===
>>>
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى ملف نظامي(أو إلى اختصار يشير إلى ملف نظامي)، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 
+
===[[Python/pathlib/Path/is mount|التابع <code>Path.is_mount</code>]]===
>>> sorted(Path('.').glob('**/*.py'))
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار هو نقطة تثبيت [https://unix.stackexchange.com/questions/3247/understanding-mount-as-a-concept-in-the-os (mount point)] .
 
+
===[[Python/pathlib/Path/is symlink|التابع <code>Path.is_symlink</code>]]===
[PosixPath('build/lib/pathlib.py'),
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى رابط دلالي ([https://www.computerhope.com/jargon/s/symblink.htm symbolic link])، و<code>False</code> فيما عدا ذلك.
 
+
===[[Python/pathlib/Path/is socket|التابع <code>Path.is_socket</code>]]===
PosixPath('docs/conf.py'),
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى [[wikipedia:Unix_domain_socket|Unix socket]] (أو إلى اختصار يشير إلى<nowiki/>[[wikipedia:Unix_domain_socket|Unix socket]])، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 
+
===[[Python/pathlib/Path/is fifo|التابع <code>Path.is_fifo</code>]]===
PosixPath('pathlib.py'),
+
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى [[wikipedia:Named_pipe|FIFO]] (أو إلى اختصار يشير إلى [[wikipedia:Named_pipe|FIFO]])، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 
 
PosixPath('setup.py'),
 
 
 
PosixPath('test_pathlib.py')]
 
 
 
Note
 
 
 
Using the “**” pattern in large directory trees may consume an inordinate amount of time.
 
 
 
Path.group()
 
 
 
Return the name of the group owning the file. KeyError is raised if the file’s gid isn’t found in the system database.
 
 
 
Path.is_dir()
 
 
 
Return True if the path points to a directory (or a symbolic link pointing to a directory), False if it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.is_file()
 
 
 
Return True if the path points to a regular file (or a symbolic link pointing to a regular file), False if it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.is_mount()
 
 
 
Return True if the path is a mount point: a point in a file system where a different file system has been mounted. On POSIX, the function checks whether path’s parent, path/.., is on a different device than path, or whether path/.. and path point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants. Not implemented on Windows.
 
 
 
New in version 3.7.
 
 
 
Path.is_symlink()
 
 
 
Return True if the path points to a symbolic link, False otherwise.
 
 
 
False is also returned if the path doesn’t exist; other errors (such as permission errors) are propagated.
 
 
 
Path.is_socket()
 
 
 
Return True if the path points to a Unix socket (or a symbolic link pointing to a Unix socket), False if it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.is_fifo()
 
 
 
Return True if the path points to a FIFO (or a symbolic link pointing to a FIFO), False if it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.is_block_device()
 
 
 
Return True if the path points to a block device (or a symbolic link pointing to a block device), False if it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.is_char_device()
 
 
 
Return True if the path points to a character device (or a symbolic link pointing to a character device), Falseif it points to another kind of file.
 
 
 
False is also returned if the path doesn’t exist or is a broken symlink; other errors (such as permission errors) are propagated.
 
 
 
Path.iterdir()
 
 
 
When the path points to a directory, yield path objects of the directory contents:
 
 
 
>>>
 
 
 
>>> p = Path('docs')
 
 
 
>>> for child in p.iterdir(): child
 
 
 
...
 
 
 
PosixPath('docs/conf.py')
 
 
 
PosixPath('docs/_templates')
 
 
 
PosixPath('docs/make.bat')
 
 
 
PosixPath('docs/index.rst')
 
 
 
PosixPath('docs/_build')
 
 
 
PosixPath('docs/_static')
 
 
 
PosixPath('docs/Makefile')
 
 
 
Path.lchmod(mode)
 
 
 
Like Path.chmod() but, if the path points to a symbolic link, the symbolic link’s mode is changed rather than its target’s.
 
 
 
Path.lstat()
 
 
 
Like Path.stat() but, if the path points to a symbolic link, return the symbolic link’s information rather than its target’s.
 
 
 
Path.mkdir(mode=0o777, parents=False, exist_ok=False)
 
 
 
Create a new directory at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the path already exists, FileExistsError is raised.
 
 
 
If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command).
 
 
 
If parents is false (the default), a missing parent raises FileNotFoundError.
 
 
 
If exist_ok is false (the default), FileExistsError is raised if the target directory already exists.
 
 
 
If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -pcommand), but only if the last path component is not an existing non-directory file.
 
 
 
Changed in version 3.5: The exist_ok parameter was added.
 
 
 
Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
 
 
 
Open the file pointed to by the path, like the built-in open() function does:
 
 
 
>>>
 
 
 
>>> p = Path('setup.py')
 
 
 
>>> with p.open() as f:
 
 
 
...    .readline()
 
 
 
...
 
 
 
'#!/usr/bin/env python3\n'
 
 
 
Path.owner()
 
 
 
Return the name of the user owning the file. KeyError is raised if the file’s uid isn’t found in the system database.
 
 
 
Path.read_bytes()
 
 
 
Return the binary contents of the pointed-to file as a bytes object:
 
 
 
>>>
 
 
 
>>> p = Path('my_binary_file')
 
 
 
>>> p.write_bytes(b'Binary file contents')
 
 
 
20
 
 
 
>>> p.read_bytes()
 
 
 
b'Binary file contents'
 
 
 
New in version 3.5.
 
 
 
Path.read_text(encoding=None, errors=None)
 
 
 
Return the decoded contents of the pointed-to file as a string:
 
 
 
>>>
 
  
 +
كما أنه يعيد <code>False</code> إذا كان المسار غير موجود أو أن الاختصار بشير إلى مسار غير موجود، كما يمكن لأخطاء أخرى (مثل عدم وجود صلاحيات) أن تظهر.
 +
===[[Python/pathlib/Path/is block device|التابع <code>Path.is_block_device</code>]]===
 +
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى [https://unix.stackexchange.com/questions/259193/what-is-a-block-device block device] (أو إلى اختصار يشير إلى [https://unix.stackexchange.com/questions/259193/what-is-a-block-device block device])، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 +
===[[Python/pathlib/Path/is char device|التابع <code>Path.is_char_device</code>]]===
 +
يعيد هذا التابع القيمة المنطقية <code>True</code> إذا كان المسار يشير إلى [https://arstechnica.com/civis/viewtopic.php?t=787631 character device] (أو إلى اختصار يشير إلى [https://arstechnica.com/civis/viewtopic.php?t=787631 character device])، بينما يعيد <code>False</code> في حال كان المسار يشير إلى أي نوع آخر من الملفات.
 +
===[[Python/pathlib/Path/iterdir|التابع <code>Path.iterdir</code>]]===
 +
يُسمح باستخدام هذا التابع على كائنات المسارات التي تشير إلى مجلدات، حيث يُنتج كائنات مسارات لكل محتويات المجلد، وتكون النتيجة قابلة للتكرار.
 +
===[[Python/pathlib/Path/lchmod|التابع <code>Path.lchmod</code>]]===
 +
مشابه للتابع <code>Path.chmod</code>، ولكن عندما يشير المسار إلى اختصار، فإن هذا التابع يقوم بتغيير نمط الاختصار بدلًا من تغيير نمط الملف الذي يشير إليه الاختصار.
 +
===[[Python/pathlib/Path/lstat|التابع <code>Path.lstat</code>]]===
 +
مشابه للتابع <code>Path.stat</code> ، ولكن عندما يشير المسار إلى اختصار، فإن هذا التابع يقوم بإعادة المعلومات المتعلقة بالاختصار ذاته بدلًا من إعادة المعلومات المرتبطة بالملف الذي يشير إليه الاختصار.
 +
===التابع <code>[[Python/pathlib/Path/mkdir|Path.mkdir]]</code>===
 +
يُنشئ هذا التابع مجلّدًا في المسار المُعطى بالخصائص التي يتم تحديدها في باقي الوسطاء.
 +
===[[Python/pathlib/Path/open|التابع <code>Path.open</code>]]===
 +
يفتح الملف الذي يشير إليه المسار، بطريقة مشابهة للتابع المضمن <code>[[Python/open|open]]</code>.
 +
===[[Python/pathlib/Path/owner|التابع <code>Path.owner</code>]]===
 +
يعيد هذا التابع اسم المستخدم المالك للملف. ويرمي استثناءً <code>KeyError</code>  إن كان معرف المستخدم <code>[http://www.linfo.org/uid.html uid]</code> غير موجود في قاعدة بيانات النظام.
 +
===[[Python/pathlib/Path/read bytes|التابع <code>Path.read_bytes</code>]]===
 +
يعيد هذا التابع المحتوى المرمّز للملف المُشار إليه ككائن من النوع <code>[[Python/bytes|bytes]]</code>.
 +
===[[Python/pathlib/Path/read text|التابع <code>Path.read_text</code>]]===
 +
يعيد هذا التابع المحتوى النصي للملف كسلسلة نصية.
 +
===[[Python/pathlib/Path/rename|التابع <code>Path.rename</code>]]===
 +
يعيد تسمية الملف أو المجلد بما يحدده الوسيط المُعطى. في نظام يونكس، وله سلوك مختلف في حال وجود ملف بنفس الاسم الجديد حسب نظام التشغيل.
 +
===[[Python/pathlib/Path/replace|التابع <code>Path.replace</code>]]===
 +
يستبدل الملف الحالي بملف الوجهة، حيث يقوم بإعادة التسمية إن لم تكن الوجهة موجودة، ويقوم بالاستبدال دون أي شرط في حال كانت الوجهة موجودة من قبل.
 +
===التابع <code>[[Python/pathlib/Path/resolve|Path.resolve]]</code>===
 +
يجعل المسار مسارًا مطلقًا.
 +
===[[Python/pathlib/Path/rglob|التابع <code>Path.rglob</code>]]===
 +
مشابه لاستدعاء التابع <code>[[Python/pathlib/Path/glob|glob]]</code> عند إضافة <code>'**'</code> في بداية النمط المُعطى
 +
===التابع <code>[[Python/pathlib/Path/rmdir|Path.rmdir]]</code>===
 +
يحذف المجلد إن كان خاليًا من الملفات.
 +
===التابع <code>[[Python/pathlib/Path/samefile|Path.samefile]]</code>===
 +
يحدد فيما إذا كان المسار المعطى هو مطابقًا للمسار الخاص بالكائن.
 +
===التابع <code>[[Python/pathlib/Path/symlink to|Path.symlink_to]]</code>===
 +
يجعل من الكائن اختصارًا للملف المُعطى.
 +
===التابع <code>[[Python/pathlib/Path/touch|Path.touch]]</code>===
 +
يُنشئ ملفًّا في المسار المُعطى.
 +
===[[Python/pathlib/Path/unlink|التابع <code>Path.unlink</code>]]===
 +
يحذف الملف أو الاختصار الذي يشير إليه الكائن، أما إذا كان المسار يشير إلى مجلد فيجب استخدام التابع <code>[[Python/pathlib/Path/rmdir|Path.rmdir]]</code> بدلًا من هذا التابع.
 +
===[[Python/pathlib/Path/write bytes|التابع <code>Path.write_bytes</code>]]===
 +
يفتح الملف المُشار إليه في نمط <code>[[Python/bytes|bytes]]</code>، ويكتب البيانات فيه ثم يُغلقه.
 +
===التابع <code>[[Python/pathlib/Path/write text|Path.write_text]]</code>===
 +
يفتح الملف المٌشار إليه بالنمط النصي، ويكتب البيانات فيه ثم يُغلقه.<syntaxhighlight lang="python3">
 
>>> p = Path('my_text_file')
 
>>> p = Path('my_text_file')
  
 
>>> p.write_text('Text file contents')
 
>>> p.write_text('Text file contents')
 
 
18
 
18
  
 
>>> p.read_text()
 
>>> p.read_text()
 
 
'Text file contents'
 
'Text file contents'
  
The optional parameters have the same meaning as in open().
+
<span> </span>
  
New in version 3.5.
+
<span> </span>
 
+
</syntaxhighlight>تابع مُستحدث في السخة 3.5.
Path.rename(target)
+
==انظر أيضًا==
 
+
للقيام بمعالجة منخفضة المستوى للمسارات على السلاسل النصية، يمكنك أيضًا استخدام الوحدة [[Python/os/path|os.path]]
Rename this file or directory to the given target. On Unix, if target exists and is a file, it will be replaced silently if the user has permission. target can be either a string or another path object:
+
===التقابل مع الوحدة <code>os</code>===
 
+
يبيّن الجدول التالي تقابل عدة توابع من الوحدة <code>os</code>  مع ما يقابلها و يكافئها من الوحدة  <code>[[Python/pathlib/Path|Path]]</code>.
>>>
+
{| class="wikitable"
 
+
!الوحدة os والوحدة os.path
>>> p = Path('foo')
+
!الوحدة pathlib
 
+
|-
>>> p.open('w').write('some text')
+
|os.path.abspath‎‎()
 
+
|Path.resolve()
9
+
|-
 
+
|‎os.getcwd()
>>> target = Path('bar')
+
|Path.cwd()
 
+
|-
>>> p.rename(target)
+
|os.path.exists()
 
+
|Path.exists()
>>> target.open().read()
+
|-
 
+
|os.path.expanduser()
'some text'
+
|Path.expanduser()‎ و Path.home()
 
+
|-
Path.replace(target)
+
|os.path.isdir()
 
+
|Path.is_dir()
Rename this file or directory to the given target. If target points to an existing file or directory, it will be unconditionally replaced.
+
|-
 
+
|os.path.isfile()
Path.resolve(strict=False)
+
|Path.is_file()
 
+
|-
Make the path absolute, resolving any symlinks. A new path object is returned:
+
|os.path.islink()
 
+
|Path.is_symlink()
>>>
+
|-
 
+
|os.stat()
>>> p = Path()
+
|Path.stat(),Path.owner(), Path.group()
 
+
|}
>>> p
 
 
 
PosixPath('.')
 
 
 
>>> p.resolve()
 
 
 
PosixPath('/home/antoine/pathlib')
 
 
 
“..” components are also eliminated (this is the only method to do so):
 
 
 
>>>
 
 
 
>>> p = Path('docs/../setup.py')
 
 
 
>>> p.resolve()
 
 
 
PosixPath('/home/antoine/pathlib/setup.py')
 
 
 
If the path doesn’t exist and strict is True, FileNotFoundError is raised. If strict is False, the path is resolved as far as possible and any remainder is appended without checking whether it exists. If an infinite loop is encountered along the resolution path, RuntimeError is raised.
 
 
 
New in version 3.6: The strict argument.
 
 
 
Path.rglob(pattern)
 
 
 
This is like calling Path.glob() with “**” added in front of the given pattern:
 
 
 
>>>
 
 
 
>>> sorted(Path().rglob("*.py"))
 
 
 
[PosixPath('build/lib/pathlib.py'),
 
 
 
PosixPath('docs/conf.py'),
 
 
 
PosixPath('pathlib.py'),
 
 
 
PosixPath('setup.py'),
 
 
 
PosixPath('test_pathlib.py')]
 
 
 
Path.rmdir()
 
 
 
Remove this directory. The directory must be empty.
 
 
 
Path.samefile(other_path)
 
 
 
Return whether this path points to the same file as other_path, which can be either a Path object, or a string. The semantics are similar to os.path.samefile() and os.path.samestat().
 
 
 
An OSError can be raised if either file cannot be accessed for some reason.
 
 
 
>>>
 
 
 
>>> p = Path('spam')
 
 
 
>>> q = Path('eggs')
 
 
 
>>> p.samefile(q)
 
 
 
False
 
 
 
>>> p.samefile('spam')
 
 
 
True
 
 
 
New in version 3.5.
 
 
 
Path.symlink_to(target, target_is_directory=False)
 
 
 
Make this path a symbolic link to target. Under Windows, target_is_directory must be true (default False) if the link’s target is a directory. Under POSIX, target_is_directory’s value is ignored.
 
 
 
>>>
 
 
 
>>> p = Path('mylink')
 
 
 
>>> p.symlink_to('setup.py')
 
 
 
>>> p.resolve()
 
 
 
PosixPath('/home/antoine/pathlib/setup.py')
 
 
 
>>> p.stat().st_size
 
 
 
956
 
 
 
>>> p.lstat().st_size
 
 
 
8
 
 
 
Note
 
 
 
The order of arguments (link, target) is the reverse of os.symlink()’s.
 
 
 
Path.touch(mode=0o666, exist_ok=True)
 
 
 
Create a file at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the file already exists, the function succeeds if exist_ok is true (and its modification time is updated to the current time), otherwise FileExistsError is raised.
 
 
 
Path.unlink()
 
 
 
Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead.
 
 
 
Path.write_bytes(data)
 
 
 
Open the file pointed to in bytes mode, write data to it, and close the file:
 
 
 
>>>
 
 
 
>>> p = Path('my_binary_file')
 
 
 
>>> p.write_bytes(b'Binary file contents')
 
 
 
20
 
 
 
>>> p.read_bytes()
 
 
 
b'Binary file contents'
 
 
 
An existing file of the same name is overwritten.
 
 
 
New in version 3.5.
 
 
 
Path.write_text(data, encoding=None, errors=None)
 
 
 
Open the file pointed to in text mode, write data to it, and close the file:
 
 
 
>>>
 
 
 
>>> p = Path('my_text_file')
 
 
 
>>> p.write_text('Text file contents')
 
 
 
18
 
 
 
>>> p.read_text()
 
 
 
'Text file contents'
 
  
New in version 3.5.
+
== المصادر ==
 +
[https://docs.python.org/3/library/pathlib.html قسم الوحدة pathlib في توثيق بايثون الرسمي.]

المراجعة الحالية بتاريخ 10:41، 19 أغسطس 2018

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

هو الصنف العام للأصناف التي تتعامل مع المسارات مع السماح بالقيام بعمليات الإدخال والإخراج، وتُسمّى بأصناف المسارات الصلبة Concrete Paths.

البنية العامة

pathlib.Path(*args,**kwargs)

المعاملات

args*

يمكن تمرير عدد غير محدود من السلاسل النصية أو من كائنات من النوع Path حيث تمثل السلاسل النصية أسماء المجلدات والملفات بالترتيب من المستوى الأعلى للأدنى.

kwargs**

معامل غير مستخدم.

يمكنك مراجعة المقال التالي بخصوص هذا النوع من المعاملات.

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

يمكن إنشاء كائن من الصنف Path بثلاث طرق مختلفة:

1- باستخدام باني الصنف العام Path، وتعتمد هذه الطريقة على إنشاء كائن من الصنف PosixPath أو WindowsPath حسب نظام التشغيل الذي يتم تفسير الشيفرة عليه:

>>> Path('setup.py')

PosixPath('setup.py')

2- باستخدام باني الصنف الخاص بالأنظمة المغايرة لويندوز PosixPath

>>> PosixPath('/etc')

PosixPath('/etc')

3- باستخدام باني الصنف الخاص بنظام ويندوز WindowsPath

>>> WindowsPath('c:/Program Files/')

WindowsPath('c:/Program Files')

كما تجدر الإشارة إلى أنه لا يُسمح لإنشاء أحد الصنفين PosixPath أو WindowsPath على نظام مغاير له، حيث يرمي الباني الخاص بالصنف الاستثناء NotImplementedError كما يوضح المثال التالي:

>>>

>>> import os

>>> os.name
'posix'     #نظام التشغيل الذي يتم تفسير الشيفرة عليه مغاير لويندوز

>>> Path('setup.py')
PosixPath('setup.py')

>>> PosixPath('setup.py')
PosixPath('setup.py')

>>> WindowsPath('setup.py')  #لا تسمح أصناف المسارات الصلبة بإنشاء كائن مغاير لنوع النظام الذي يتم العمل عليه
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "pathlib.py", line 798, in __new__
   % (cls.__name__,))
NotImplementedError: cannot instantiate 'WindowsPath' on your system

أمثلة على الاستخدام

تُوضح الشيفرات التالية أمثلةً لاستخدام هذا الصنف:

استيراد الصنف الأساسي من الوحدة pathlib:

>>> from pathlib import Path #استيراد الصنف الأساسي

ملاحظة: لن تتمكن من استيراد المكتبة على نسخ بايثون الأُقدم من 3.4 (استخدم os.path بدلًا منها) إنشاء كائن والتكرار على المجلدات والملفات التي بداخله باستخدام list comprehension:

>>> p = Path('.') #إنشاء كائن باستخدام باين الصنف العام

>>> [x for x in p.iterdir() if x.is_dir()] #تعداد المجلدات الموجودة ضمن المسار الحالي 
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
PosixPath('__pycache__'), PosixPath('build')]

تعداد ملفات بايثون المصدرية في شجرة المجلدات الحالية باستخدام glob:

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

كما يمكن استخدام قائمة طويلة من التوابع للاستعلام عن خصائص المسار، مثل exists و is_dir:

>>> q.exists()

True

>>> q.is_dir()

False

يسمح التابع open بفتح الملفات بشكل مشابه للتابع open المضمن في بايثون:

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

السطر الأول وأهلا بك
السطر الأخير وداعا

توابع الصنف Path

يتيح الصنف Path استخدام جميع التوابع الخاصة بالصنف PurePath وذلك لأنه يرث منه، ويضيف عليها التوابع التالية، التي تسمح بالقيام باستدعاءات النظام (system calls)، وترمي هذه التوابع استثناء OSError في حال فشل أيٍّ منها (كأن يكون المسار الذي يتم التعامل معه غير موجود مثلًا).

التابع Path.cwd

يعيد كائن مسار جديدًا يمثّل المسار الحالي (بشكل مشابه للتابع os.getcwd)

التابع Path.home

يعيد كائن مسار جديدًا يمثّل المسار الرئيسي (home directory) للمستخدم (بشكل مشابه للتابع os.path.expanduser مع الوسيط '~')

التابع Path.stat

يعيد معلومات عن المسار (مماثل للتابع os.stat) ، ويتم حساب المعلومات في كل مرة يتم فيها استدعاء التابع

التابع Path.chmod

يقوم هذا التابع بتغيير نمط الملف وصلاحياته (مماثل للتابع os.chmod)

التابع Path.exists

يدلّ على كون المسار مشيرًا إلى ملف أو مجلد حقيقي (موجود على نظام التشغيل) أم لا.

التابع Path.expanduser

يعيد كائن مسار جديد يمدد فيه المسار الرئيسي للمستخدم الممثل بالرمز '~'، مشابه للتابع os.path.expanduser

التابع Path.glob

يعيد جميع الملفات التي تحقق نمط glob المعطى، مهما كان نوع الملف.

التابع Path.group

يعيد هذا التابع اسم المجموعة التي ينتمي لها الملف، ويرمي بالخطأ KeyError إذا كان معرف المجموعة gid الخاص بالملف غير موجود في قاعدة بيانات النظام.

التابع Path.is_dir

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى مجلد (أو إلى اختصار يشير إلى مجلد)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

التابع Path.is_file

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى ملف نظامي(أو إلى اختصار يشير إلى ملف نظامي)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

التابع Path.is_mount

يعيد هذا التابع القيمة المنطقية True إذا كان المسار هو نقطة تثبيت (mount point) .

التابع Path.is_symlink

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى رابط دلالي (symbolic link)، وFalse فيما عدا ذلك.

التابع Path.is_socket

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى Unix socket (أو إلى اختصار يشير إلىUnix socket)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

التابع Path.is_fifo

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى FIFO (أو إلى اختصار يشير إلى FIFO)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

كما أنه يعيد False إذا كان المسار غير موجود أو أن الاختصار بشير إلى مسار غير موجود، كما يمكن لأخطاء أخرى (مثل عدم وجود صلاحيات) أن تظهر.

التابع Path.is_block_device

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى block device (أو إلى اختصار يشير إلى block device)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

التابع Path.is_char_device

يعيد هذا التابع القيمة المنطقية True إذا كان المسار يشير إلى character device (أو إلى اختصار يشير إلى character device)، بينما يعيد False في حال كان المسار يشير إلى أي نوع آخر من الملفات.

التابع Path.iterdir

يُسمح باستخدام هذا التابع على كائنات المسارات التي تشير إلى مجلدات، حيث يُنتج كائنات مسارات لكل محتويات المجلد، وتكون النتيجة قابلة للتكرار.

التابع Path.lchmod

مشابه للتابع Path.chmod، ولكن عندما يشير المسار إلى اختصار، فإن هذا التابع يقوم بتغيير نمط الاختصار بدلًا من تغيير نمط الملف الذي يشير إليه الاختصار.

التابع Path.lstat

مشابه للتابع Path.stat ، ولكن عندما يشير المسار إلى اختصار، فإن هذا التابع يقوم بإعادة المعلومات المتعلقة بالاختصار ذاته بدلًا من إعادة المعلومات المرتبطة بالملف الذي يشير إليه الاختصار.

التابع Path.mkdir

يُنشئ هذا التابع مجلّدًا في المسار المُعطى بالخصائص التي يتم تحديدها في باقي الوسطاء.

التابع Path.open

يفتح الملف الذي يشير إليه المسار، بطريقة مشابهة للتابع المضمن open.

التابع Path.owner

يعيد هذا التابع اسم المستخدم المالك للملف. ويرمي استثناءً KeyError إن كان معرف المستخدم uid غير موجود في قاعدة بيانات النظام.

التابع Path.read_bytes

يعيد هذا التابع المحتوى المرمّز للملف المُشار إليه ككائن من النوع bytes.

التابع Path.read_text

يعيد هذا التابع المحتوى النصي للملف كسلسلة نصية.

التابع Path.rename

يعيد تسمية الملف أو المجلد بما يحدده الوسيط المُعطى. في نظام يونكس، وله سلوك مختلف في حال وجود ملف بنفس الاسم الجديد حسب نظام التشغيل.

التابع Path.replace

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

التابع Path.resolve

يجعل المسار مسارًا مطلقًا.

التابع Path.rglob

مشابه لاستدعاء التابع glob عند إضافة '**' في بداية النمط المُعطى

التابع Path.rmdir

يحذف المجلد إن كان خاليًا من الملفات.

التابع Path.samefile

يحدد فيما إذا كان المسار المعطى هو مطابقًا للمسار الخاص بالكائن.

التابع Path.symlink_to

يجعل من الكائن اختصارًا للملف المُعطى.

التابع Path.touch

يُنشئ ملفًّا في المسار المُعطى.

التابع Path.unlink

يحذف الملف أو الاختصار الذي يشير إليه الكائن، أما إذا كان المسار يشير إلى مجلد فيجب استخدام التابع Path.rmdir بدلًا من هذا التابع.

التابع Path.write_bytes

يفتح الملف المُشار إليه في نمط bytes، ويكتب البيانات فيه ثم يُغلقه.

التابع Path.write_text

يفتح الملف المٌشار إليه بالنمط النصي، ويكتب البيانات فيه ثم يُغلقه.

>>> p = Path('my_text_file')

>>> p.write_text('Text file contents')
18

>>> p.read_text()
'Text file contents'

<span> </span>

<span> </span>

تابع مُستحدث في السخة 3.5.

انظر أيضًا

للقيام بمعالجة منخفضة المستوى للمسارات على السلاسل النصية، يمكنك أيضًا استخدام الوحدة os.path

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

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

الوحدة 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()‎

المصادر

قسم الوحدة pathlib في توثيق بايثون الرسمي.