misc:code_snippets:python
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| misc:code_snippets:python [2017/03/06 21:40] – mehr Beispielcode hinzugefügt sascha | misc:code_snippets:python [2017/04/11 15:13] (current) – Fix sascha | ||
|---|---|---|---|
| Line 72: | Line 72: | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | with tempfile.TemporaryDirectory() as tmp_dir: | ||
| pass # do stuff | pass # do stuff | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Mittels Pipe übergebene Daten lesen ==== | ||
| + | |||
| + | <code python> | ||
| + | import sys | ||
| + | |||
| + | # ensure the script only reads piped data and doesn' | ||
| + | if not sys.stdin.isatty(): | ||
| + | lst = [l.strip() for l in sys.stdin.readlines() if not l.startswith('#' | ||
| + | print(lst) | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Dateien aus einem Verzeichnis erhalten oder aus Datei lesen ==== | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | from os.path import expanduser, isdir, isfile, abspath, join as pjoin | ||
| + | |||
| + | def get_files(path=/ | ||
| + | files = [] | ||
| + | path = expanduser(path) | ||
| + | |||
| + | if isdir(path): | ||
| + | files = [pjoin(abspath(path), | ||
| + | | ||
| + | elif isfile(path): | ||
| + | with open(path, ' | ||
| + | files = [l.strip() for l in lst.readlines() if not l.startswith('#' | ||
| + | |||
| + | return files | ||
| </ | </ | ||
misc/code_snippets/python.1488836429.txt.gz · Last modified: by sascha
