Python Zero to Heros

Online Absolute Beginner Python Tutorials 

Every Sunday 2pm (UK time/ BST)

Get this slide deck:

slides.com/cheukting_ho/python-os-subprocess

 

Recap

Beginner topics:

Python objects, Control flows,

Functions, modeuls, classes and decorators

strings operations and regex with re

Testing:

pytest with fixtures and mock

property-based testing

python linters & auto-formatters

TDD, type hinting

Intermedite topics:

Iterators, generators, async

Packaging:

generating docs, pypi

Others:
Functional Programming

Read and Write a file

 

In Python you can use open() to get access to a file

https://www.w3schools.com/python/python_file_handling.asp

 

open() returns a file object which provide lot's of various operations

https://www.programiz.com/python-programming/file-operation

os — Miscellaneous operating system interfaces

 

This module provides a portable way of using operating system dependent functionality.

Various OS operations

 

  • Creating Directory
  • Changing the Current Working Directory
  • Removing a Directory
  • List Files and Sub-directories

 

https://www.tutorialsteacher.com/python/os-module

os.path methods

 

  • os.path.abspath(path)
  • os.path.realpath(path)
  • os.path.dirname(path)
  • os.path.join(path, *paths)

 

subprocess — Subprocess managemen

 

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

output = subprocess.run(
  [
    "docker-compose",
    "--file",
    os.path.dirname(os.path.realpath(__file__)) + "/test-docker-compose.yml",
    "up",
    "-d",
  ],
  stderr=subprocess.PIPE,
)
if output.returncode != 0:
  raise RuntimeError(output.stderr)

Next week:
TBC

Sunday 2pm (UK time/ BST)

There are also Mid Meet Py every Wednesday 6pm

Python OS and Subprocessing

By Cheuk Ting Ho

Python OS and Subprocessing

  • 307
Loading comments...

More from Cheuk Ting Ho