On this page
tokenize.open
Prev: tokenize.generate_tokens | Up: tokenize | Next: tokenize.TokenError
Rel: tokenize.detect_encoding, tokenize.generate_tokens, tokenize.TokenError, tokenize.TokenInfo
Ref: docs.python.org — tokenize.open
Tags: #public #python3 #stdlib #tokenize
Import
import tokenize
from tokenize import open
Signature & Overview
tokenize.open(filename)
Open a file in read only mode using the encoding detected by detect_encoding().
Usage
from pathlib import Path
import tempfile, tokenize
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / 'src.py'
p.write_text('# coding: utf-8\nx = 1\n', encoding='utf-8')
with tokenize.open(p) as f:
assert 'x = 1' in f.read()
Gotchas
- Auto-detects PEP 263 source file encoding—best way to open arbitrary
.pyfiles as text.
REPL Inspect
from tokenize import open
help(open)
Linked from
- tuts/python3/stdlib_module/tokenize
- tuts/python3/stdlib_module/tokenize/detect_encoding
- tuts/python3/stdlib_module/tokenize/generate_tokens
- tuts/python3/stdlib_module/tokenize/TokenError
- tuts/python3/stdlib_module/tokenize/TokenInfo
- tuts/python3/stdlib_module/tokenize/tokenize
- tuts/python3/stdlib_module/tokenize/untokenize
Related notes
- tuts/python3/stdlib_module/tokenize/detect_encoding Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/generate_tokens Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/TokenError Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/TokenInfo Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/tokenize Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/untokenize links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib