Hey, I have one question though, how do you write on a txt file???

Dani AI

Generated

posted that the problem was solved and shared a simple open/write/close example. For readers who land here later, a few concise, practical tips and safer patterns that go beyond that minimal example.

Use a context manager so files are always closed even on errors:

# Python 3 - recommended
text = "First line\nSecond line\n"
with open("example.txt", "w", encoding="utf-8", newline="\n") as f:
    f.write(text)

Alternatively, for short scripts the Path API is handy:

from pathlib import Path
Path("example.txt").write_text(text, encoding="utf-8")

Quick reminders and common pitfalls:

  • Modes: w (truncate/create), a (append), x (create-only), r+ (read/write). Use b when writing bytes (e.g., wb).
  • Encoding: always set encoding="utf-8" unless you have a specific reason not to; default encodings vary by platform.
  • Paths: writing to a file in a directory that does not exist raises an error; use Path(...).parent.mkdir(parents=True, exist_ok=True) if needed.
  • Permissions: a PermissionError means the process cannot write to that location; try a different folder.
  • Atomic writes: if partial writes are a concern, write to a temp file then rename with os.replace() to avoid corrupted files.

If still using Python 2, use io.open or codecs.open to control encoding. For more details see the official Python I/O tutorial and the Pathlib write_text reference: Python I/O tutorial and pathlib.Path.write_text.

never mind I figured it out

s = """Sign in an Irish pub:
We are open from 10 a.m. until 11 p.m. 
and if you haven't had enough to drink 
at that hour the management feels that 
you haven't really been trying."""

fh = open("IrishSign.txt", "w")
fh.write(s)
fh.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.