On this page
  1. tokenize.open
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from tokenize import open
help(open)

Linked from

Related notes