Re: can you improve this text-only beginner copy program?
[email protected] (Stefan Ram) 27 Aug 2025 21:09:56 GMT
| Newsgroups | comp.lang.python |
|---|---|
| Organization | Stefan Ram |
| Message-ID | <[email protected]> |
Ethan Carter <[email protected]> wrote or quoted: >def copy(s, d): > """Copies text file named S to text file named D.""" > with open(s) as src: > with open(d, "w") as dst: > try: > dst.write(src.read()) > except Exception: > os.remove(d) > raise In Python there are several ways to format docstrings. One possibility is to use conventions from "Sphinx" and to use "reStructuredText". Then, it might look as follows, giving some prominence to the names of parameters, though some find that this is already too much markup in the source code: """ Copy the contents of the text file whose path is given by the parameter ``s`` to the text file whose path is given by ``d``. :param s: Path to the source text file to copy from. :type s: str :param d: Path to the destination text file to copy to. :type d: str :raises OSError: If an I/O error occurs during writing. On error, the destination file will be removed if it was partially written. """ Sphinx can then extract such documentation from your source code and generate webpages or PDF books from it.