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

fileinput.filename

Prev: fileinput.filelineno | Up: fileinput | Next: fileinput.fileno
Rel: fileinput.close, fileinput.FileInput, fileinput.filelineno, fileinput.fileno
Ref: docs.python.org — fileinput.filename
Tags: #public #python3 #stdlib #fileinput


Import

import fileinput
from fileinput import filename

Signature & Overview

fileinput.filename()

Return the name of the file currently being read.

Usage

import fileinput
from pathlib import Path
import tempfile

with tempfile.TemporaryDirectory() as tmp:
    p = Path(tmp) / "a.txt"
    p.write_text("hi\n", encoding="utf-8")
    with fileinput.input(files=(p,), encoding="utf-8") as f:
        next(f)
        assert Path(f.filename()).name == "a.txt"

Gotchas

REPL Inspect

from fileinput import filename
help(filename)

Linked from

Related notes