σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademySkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
Skills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 1 · Python foundations for quants›Topic 1.16·~40 minBeginner

Reading and writing text files

Skill positionModule 1 of 11 · Topic 16 of 33
1.16 Reading and writing text files
—Lab score— / 100Total XP—Streak—

Opening a file with `with`

Variables disappear when a program ends; files do not. open(path, mode) gives you a file object, and mode says what you want: "r" to read (the default), "w" to write and "a" to append. Always open files with a with block: with open(path) as file:. When the block ends, Python closes the file for you, even if an error happened inside, so data is not lost or left half-written. Pass encoding="utf-8" so text is read and written the same way on every computer.

# Write a file, then read it back
with open("fills.txt", "w", encoding="utf-8") as file:
    file.write("AAPL,184.25,200\n")
    file.write("MSFT,410.20,50\n")

with open("fills.txt", encoding="utf-8") as file:
    text = file.read()
print(repr(text))  # 'AAPL,184.25,200\nMSFT,410.20,50\n'

Reading line by line

file.read() returns the whole file as one string. More often you loop over the file object, which gives one line at a time and works for files too big for memory: for line in file:. Each line still ends with \n, so clean it with line.strip(). A blank line becomes the empty string "" after stripping, which is false, so if text: skips it. file.readlines() and text.splitlines() give lists of lines when you want them all at once.

# Read line by line, skipping blank lines
with open("fills.txt", "a", encoding="utf-8") as file:
    file.write("\n")                # a blank line

total = 0.0
with open("fills.txt", encoding="utf-8") as file:
    for line in file:
        text = line.strip()
        if not text:
            continue
        symbol, price, quantity = text.split(",")
        total += float(price) * int(quantity)
print(round(total, 2))              # 57360.0

Writing and appending

file.write(text) writes exactly the string you give it and returns the number of characters. It does not add a line break, so end each line with "\n" yourself. Mode "w" first empties the file (and creates it if it does not exist), so opening an existing file with "w" throws its old content away. Mode "a" keeps what is there and adds to the end. Only strings can be written: convert numbers first, for example with an f-string such as f"{price:.2f}".

# "w" starts over, "a" adds to the end
with open("log.txt", "w", encoding="utf-8") as file:
    file.write("start\n")
with open("log.txt", "a", encoding="utf-8") as file:
    file.write("order 1 filled\n")
    file.write(f"pnl {12.5:+.2f}\n")

with open("log.txt", encoding="utf-8") as file:
    print(file.read().splitlines())  # ['start', 'order 1 filled', 'pnl +12.50']

with open("log.txt", "w", encoding="utf-8") as file:
    pass                             # opening with "w" emptied it
with open("log.txt", encoding="utf-8") as file:
    print(repr(file.read()))         # ''

Paths and missing files

A relative path such as "fills.txt" is looked up in the program's current folder; use os.path.join(folder, name) to build paths without worrying about separators. Opening a file that does not exist for reading raises FileNotFoundError, which you can catch with try/except when a missing file is an expected case. For small files, pathlib.Path(path).read_text() and .write_text(text) do the open, read or write and close in one call.

import os
from pathlib import Path

# A missing file is an exception you can handle
def count_lines(path):
    try:
        with open(path, encoding="utf-8") as file:
            return len(file.read().splitlines())
    except FileNotFoundError:
        return 0

print(count_lines("fills.txt"))      # 3
print(count_lines("nope.txt"))       # 0

# pathlib does open + read + close in one call
path = Path(os.path.join(".", "fills.txt"))
print(path.read_text(encoding="utf-8").splitlines()[0])  # AAPL,184.25,200
"w" erases, and a file is only as good as its formatDouble-check the mode before you run code that writes: "w" on the wrong path destroys data with no undo. Decide a simple format (one record per line, fields separated by commas) and write the reader and the writer together so they agree about it. Next you will meet the CSV format, which formalises exactly this idea.
  • with open(path, mode, encoding="utf-8") as file: opens and always closes the file.
  • for line in file: reads one line at a time; line.strip() removes the line break.
  • "w" empties the file, "a" appends; write adds no line break.

Next up: CSV files, the standard way to store tables of prices and trades.

Lab · what's graded

  • Load a watchlist from a file with read_symbols25 pts
  • Turn a file of fills into tuples with parse_trades25 pts
  • Save a profit and loss report with write_report25 pts
  • Add to a log without losing it with append_lines25 pts
Explorer · lab-16-1
▾ lab-16-1
…
bash · in-browser
learner@quantcraft:~/lab-16-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-16-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes

Log in to run your code.

Every lecture is free to read. Create an account or log in to run code, run tests and submit this lab.

  • In-browser IDE with a bash terminal — nothing to install
  • Instant grading, XP and your place on the skill
Create an accountLog in
OR
Continue with GitHub