diff --git a/README.md b/README.md index 78172d3..96bb5be 100644 --- a/README.md +++ b/README.md @@ -1625,14 +1625,14 @@ def write_to_file(filename, text): Paths ----- ```python -from os import getcwd, path, listdir, scandir -from glob import glob +import os, os.path as path, glob +from pathlib import Path ``` ```python - = getcwd() # Returns the current working directory. + = os.getcwd() # Returns the current working directory. = path.join(, ...) # Joins two or more pathname components. - = path.abspath() # Returns absolute path. + = path.realpath() # Resolves symlinks and calls path.abspath(). ``` ```python @@ -1642,8 +1642,8 @@ from glob import glob ``` ```python - = listdir(path='.') # Returns filenames located at the path. - = glob('') # Returns paths matching the wildcard pattern. + = os.listdir(path='.') # Returns filenames located at the path. + = glob.glob('') # Returns paths matching the wildcard pattern. ``` ```python @@ -1661,20 +1661,17 @@ from glob import glob **Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.** ```python - = scandir(path='.') # Returns DirEntry objects located at the path. + = os.scandir(path='.') # Returns DirEntry objects located at the path. = .path # Returns the whole 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 -``` - ```python = Path( [, ...]) # Accepts strings, Paths and DirEntry objects. = / [/ ...] # First or second path must be a Path object. + = .resolve() # Resolves symlinks and calls .absolute(). ``` ```python @@ -1717,12 +1714,14 @@ os.makedirs(, mode=0o777) # Creates all path's dirs. Also `exist_ok=Fa ```python shutil.copy(from, to) # Copies the file. 'to' can exist or be a dir. +shutil.copy2(from, to) # Also copies creation and modification time. shutil.copytree(from, to) # Copies the directory. 'to' must not exist. ``` ```python os.rename(from, to) # Renames/moves the file or directory. -os.replace(from, to) # Same, but overwrites 'to' if it exists. +os.replace(from, to) # Same, but overwrites file 'to' even on Windows. +shutil.move(from, to) # Rename() that moves into 'to' if it's a dir. ``` ```python diff --git a/index.html b/index.html index eeba12b..c6d711c 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -1382,20 +1382,20 @@ value = args.<name> file.write(text) -

#Paths

from os import getcwd, path, listdir, scandir
-from glob import glob
+

#Paths

import os, os.path as path, glob
+from pathlib import Path
 
-
<str>  = getcwd()                   # Returns the current working directory.
+
<str>  = os.getcwd()                # Returns the current working directory.
 <str>  = path.join(<path>, ...)     # Joins two or more pathname components.
-<str>  = path.abspath(<path>)       # Returns absolute path.
+<str>  = path.realpath(<path>)      # Resolves symlinks and calls path.abspath().
 
<str>  = path.basename(<path>)      # Returns final component of the path.
 <str>  = path.dirname(<path>)       # Returns path without the final component.
 <tup.> = path.splitext(<path>)      # Splits on last period of the final component.
 
-
<list> = listdir(path='.')          # Returns filenames located at the path.
-<list> = glob('<pattern>')          # Returns paths matching the wildcard pattern.
+
<list> = os.listdir(path='.')       # Returns filenames located at the path.
+<list> = glob.glob('<pattern>')     # Returns paths matching the wildcard pattern.
 
<bool> = path.exists(<path>)        # Or: <Path>.exists()
 <bool> = path.isfile(<path>)        # Or: <DirEntry/Path>.is_file()
@@ -1404,19 +1404,18 @@ value = args.<name>
 
<stat> = os.stat(<path>)            # Or: <DirEntry/Path>.stat()
 <real> = <stat>.st_mtime/st_size/…  # Modification time, size in bytes, ...
 
-

DirEntry

Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.

<iter> = scandir(path='.')          # Returns DirEntry objects located at the path.
+

DirEntry

Unlike listdir(), scandir() returns DirEntry objects that cache isfile, isdir and on Windows also stat information, thus significantly increasing the performance of code that requires it.

<iter> = os.scandir(path='.')       # Returns DirEntry objects located at the path.
 <str>  = <DirEntry>.path            # Returns the whole 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
+

Path Object

<Path> = Path(<path> [, ...])       # Accepts strings, Paths and DirEntry objects.
+<Path> = <path> / <path> [/ ...]    # First or second path must be a Path object.
+<Path> = <Path>.resolve()           # Resolves symlinks and calls <Path>.absolute().
 
-
<Path> = Path(<path> [, ...])       # Accepts strings, Paths and DirEntry objects.
-<Path> = <path> / <path> [/ ...]    # First or second path must be a Path object.
-
<Path> = Path()                     # Returns relative cwd. Also Path('.').
 <Path> = Path.cwd()                 # Returns absolute cwd. Also Path().resolve().
 <Path> = Path.home()                # Returns user's home directory (absolute).
@@ -1442,10 +1441,12 @@ os.mkdir(<path>, mode=0o777)        0o777)     # Creates all path's dirs. Also `exist_ok=False`.
 
shutil.copy(from, to)               # Copies the file. 'to' can exist or be a dir.
+shutil.copy2(from, to)              # Also copies creation and modification time.
 shutil.copytree(from, to)           # Copies the directory. 'to' must not exist.
 
os.rename(from, to)                 # Renames/moves the file or directory.
-os.replace(from, to)                # Same, but overwrites 'to' if it exists.
+os.replace(from, to)                # Same, but overwrites file 'to' even on Windows.
+shutil.move(from, to)               # Rename() that moves into 'to' if it's a dir.
 
os.remove(<path>)                   # Deletes the file.
 os.rmdir(<path>)                    # Deletes the empty directory.
@@ -2934,7 +2935,7 @@ $ pyinstaller script.py --add-data '<path>:.'  
  
 
   
 
diff --git a/parse.js b/parse.js
index 0be7718..216bd0c 100755
--- a/parse.js
+++ b/parse.js
@@ -88,11 +88,13 @@ const DATACLASS =
 
 const SHUTIL_COPY =
   'shutil.copy(from, to)               # Copies the file. \'to\' can exist or be a dir.\n' +
+  'shutil.copy2(from, to)              # Also copies creation and modification time.\n' +
   'shutil.copytree(from, to)           # Copies the directory. \'to\' must not exist.\n';
 
 const OS_RENAME =
   'os.rename(from, to)                 # Renames/moves the file or directory.\n' +
-  'os.replace(from, to)                # Same, but overwrites \'to\' if it exists.\n';
+  'os.replace(from, to)                # Same, but overwrites file \'to\' even on Windows.\n' +
+  'shutil.move(from, to)               # Rename() that moves into \'to\' if it\'s a dir.\n';
 
 const STRUCT_FORMAT =
   '\'<n>s\'';