From 03fe5e2a943f53cbaa984de21a4451d4650772a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 9 Jul 2019 15:53:20 +0200 Subject: [PATCH] Command execution --- README.md | 42 +++++++++++++++++++++++++++++++++++++++--- index.html | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fc34f98..4bb0cb1 100644 --- a/README.md +++ b/README.md @@ -479,7 +479,7 @@ Numbers = complex(real=0, imag=0) # Or: + j = fractions.Fraction(numerator=0, denominator=1) ``` -* **`'int()'` and `'float()'` raise ValueError on malformed strings.** +* **`'int()'` and `'float()'` raise 'ValueError' on malformed strings.** ### Basic Functions ```python @@ -1583,12 +1583,48 @@ cwd = Path() Command Execution ----------------- +### Files and Directories Commands +* **Paths can be either strings or Path objects.** +* **All exceptions are either 'OSError' or its subclasses.** + +```python +import os +os.chdir() # Changes the current working directory. + = os.getcwd() # Returns current working directory. +``` + +```python +os.remove() # Deletes the file. +os.rmdir() # Deletes empty directory. +shutil.rmtree() # Deletes an entire directory tree. +``` + +```python +os.rename(from, to) # Renames the file or directory. +os.replace(from, to) # Same, but overwrites 'to' if it exists. +``` + +```python +os.mkdir(, mode=0o777) # Creates a directory. + = os.scandir(path='.') # Returns os.DirEntry objects located at path. +``` + +#### DirEntry: +```pyton + = .name + = .path + = .is_file() + = .is_dir() + = .is_symlink() +``` + +### Shell Commands ```python import os - = os.popen().read() + = os.popen('').read() ``` -### Subprocess +#### Using subprocess: ```python >>> import subprocess, shlex >>> a = subprocess.run(shlex.split('ls -a'), stdout=subprocess.PIPE) diff --git a/index.html b/index.html index 0e1c701..858ac68 100644 --- a/index.html +++ b/index.html @@ -547,7 +547,7 @@ to_exclusive = <range>.stop <Fraction> = fractions.Fraction(numerator=0, denominator=1)
    -
  • 'int(<str>)' and 'float(<str>)' raise ValueError on malformed strings.
  • +
  • 'int(<str>)' and 'float(<str>)' raise 'ValueError' on malformed strings.

Basic Functions

<num>  = pow(<num>, <num>)            # Or: <num> ** <num>
@@ -1425,10 +1425,37 @@ value = args.<name>
 <Path> = <Path>.parent             # Path without final component.
 

#Command Execution

+

Files and Directories Commands

+
    +
  • Paths can be either strings or Path objects.
  • +
  • All exceptions are either 'OSError' or its subclasses.
  • +
+
import os
+os.chdir(<path>)       # Changes the current working directory.
+<str> = os.getcwd()    # Returns current working directory.
+
+
os.remove(<path>)      # Deletes the file.
+os.rmdir(<path>)       # Deletes empty directory.
+shutil.rmtree(<path>)  # Deletes an entire directory tree.
+
+
os.rename(from, to)    # Renames the file or directory.
+os.replace(from, to)   # Same, but overwrites 'to' if it exists.
+
+
os.mkdir(<path>, mode=0o777)   # Creates a directory.
+<iter> = os.scandir(path='.')  # Returns os.DirEntry objects located at path.
+
+

DirEntry:

+
<str>  = <DirEntry>.name
+<str>  = <DirEntry>.path
+<bool> = <DirEntry>.is_file()
+<bool> = <DirEntry>.is_dir()
+<bool> = <DirEntry>.is_symlink() 
+
+

Shell Commands

import os
-<str> = os.popen(<command>).read()
+<str> = os.popen('<shell_command>').read()
 
-

Subprocess

+

Using subprocess:

>>> import subprocess, shlex
 >>> a = subprocess.run(shlex.split('ls -a'), stdout=subprocess.PIPE)
 >>> a.stdout