From 493e5f00298f2c4e65fe56123795fef953c80c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 24 Dec 2019 15:33:41 +0100 Subject: [PATCH] Working on path --- README.md | 56 +++++++++++++++++++++++------------------------------- index.html | 39 +++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 0555285..fa4ab2e 100644 --- a/README.md +++ b/README.md @@ -1561,7 +1561,7 @@ def write_to_file(filename, text): Path ---- ```python -from os import getcwd, path, listdir +from os import getcwd, path, listdir, scandir from glob import glob ``` @@ -1578,10 +1578,29 @@ from glob import glob ``` ```python - = listdir() # Returns filenames located at path. + = listdir(path='.') # Returns filenames located at path. = glob('') # Returns paths matching the wildcard pattern. ``` +```python + = path.exists() # Or: .exists() + = path.isfile() # Or: .is_file() + = path.isdir() # Or: .is_dir() +``` + +### DirEntry +**Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.** + +```python + = scandir(path='.') # Returns DirEntry objects located at path. +``` + +```python + = .path # Returns path as a string. + = .name # Returns final component as a string. + = open() # Opens the file and returns a file object. +``` + ### Path Object ```python from pathlib import Path @@ -1600,47 +1619,20 @@ from pathlib import Path ```python = .parent # Returns Path without final component. - = .name # Returns final component as string. + = .name # Returns final component as a string. = .stem # Returns final component without extension. = .suffix # Returns final component's extension. = .parts # Returns all components as strings. ``` -```python - = .iterdir() # Returns dir contents as Path objects. - = .glob('') # Returns Paths matching the wildcard pattern. -``` - -```python - = .exists() # Or: path.exists() - = .is_file() # Or: path.isfile() - = .is_dir() # Or: path.isdir() -``` - ```python = str() # Returns Path as a string. = open() # Opens the file and returns a file object. ``` -### DirEntry -**Using scandir() instead of listdir() or iterdir() can significantly increase the performance of code that also needs file type or file attribute information.** -```python - = os.scandir(path='.') # Returns DirEntry objects located at path. -``` - -```python - = .is_file() - = .is_dir() -``` - -```python - = .path # Returns relative path as a string. - = .name # Returns final component. -``` - ```python - = Path() # Returns relative Path object. - = open() # Opens the file and returns a file object. + = .iterdir() # Returns dir contents as Path objects. + = .glob('') # Returns Paths matching the wildcard pattern. ``` diff --git a/index.html b/index.html index 1775451..4252204 100644 --- a/index.html +++ b/index.html @@ -1443,7 +1443,7 @@ value = args.<name> file.write(text) -

#Path

from os import getcwd, path, listdir
+

#Path

from os import getcwd, path, listdir, scandir
 from glob import glob
 
@@ -1455,9 +1455,21 @@ value = args.<name> <str> = path.dirname(<path>) # Returns path without final component. <tup.> = path.splitext(<path>) # Splits on last period of final component.
-
<list> = listdir(<path>)            # Returns filenames located at path.
+
<list> = listdir(path='.')          # Returns filenames located at path.
 <list> = glob('<pattern>')          # Returns paths matching the wildcard pattern.
 
+
<bool> = path.exists(<path>)        # Or: <Path>.exists()
+<bool> = path.isfile(<path>)        # Or: <DirEntry/Path>.is_file()
+<bool> = path.isdir(<path>)         # Or: <DirEntry/Path>.is_dir()
+
+

DirEntry

Using scandir() instead of listdir() can significantly increase the performance of code that also needs file type information.

<iter> = scandir(path='.')          # Returns DirEntry objects located at path.
+
+ + +
<str>  = <DirEntry>.path            # Returns path as a string.
+<str>  = <DirEntry>.name            # Returns final component as a string.
+<file> = open(<DirEntry>)           # Opens the file and returns a file object.
+

Path Object

from pathlib import Path
 
@@ -1469,33 +1481,16 @@ value = args.<name> <Path> = <Path>.resolve() # Returns absolute Path without symlinks.
<Path> = <Path>.parent              # Returns Path without final component.
-<str>  = <Path>.name                # Returns final component as string.
+<str>  = <Path>.name                # Returns final component as a string.
 <str>  = <Path>.stem                # Returns final component without extension.
 <str>  = <Path>.suffix              # Returns final component's extension.
 <tup.> = <Path>.parts               # Returns all components as strings.
 
-
<iter> = <Path>.iterdir()           # Returns dir contents as Path objects.
-<iter> = <Path>.glob('<pattern>')   # Returns Paths matching the wildcard pattern.
-
-
<bool> = <Path>.exists()            # Or: path.exists(<path>)
-<bool> = <Path>.is_file()           # Or: path.isfile(<path>)
-<bool> = <Path>.is_dir()            # Or: path.isdir(<path>)
-
<str>  = str(<Path>)                # Returns Path as a string.
 <file> = open(<Path>)               # Opens the file and returns a file object.
 
-

DirEntry

Using scandir() instead of listdir() or iterdir() can significantly increase the performance of code that also needs file type or file attribute information.

<iter> = os.scandir(path='.')       # Returns DirEntry objects located at path.
-
- - -
<bool> = <DirEntry>.is_file()
-<bool> = <DirEntry>.is_dir()
-
-
<str>  = <DirEntry>.path            # Returns relative path as a string.
-<str>  = <DirEntry>.name            # Returns final component.
-
-
<Path> = Path(<DirEntry>)           # Returns relative Path object.
-<file> = open(<DirEntry>)           # Opens the file and returns a file object.
+
<iter> = <Path>.iterdir()           # Returns dir contents as Path objects.
+<iter> = <Path>.glob('<pattern>')   # Returns Paths matching the wildcard pattern.
 

#OS Commands

Files and Directories

  • Paths can be either strings, Paths, or DirEntry objects.