Python/pathlib/Path/mkdir

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث

التابع Path.mkdir

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.

>>> p=Path(r"D:\Users\MOUSTAFA\Desktop\my_folder")

>>> p
WindowsPath('D:/Users/MOUSTAFA/Desktop/my_folder')

>>> p.exists()
False#المسار المحدد غير موجود في نظام الملفات

>>> p.mkdir()

>>> p.exists()
True#تم إنشاء الملف في نظام الملفات