Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197540

can you improve this text-only beginner copy program?

From Ethan Carter <ec1828@somewhere.edu>
Newsgroups comp.lang.python
Subject can you improve this text-only beginner copy program?
Date 2025-08-27 11:03 -0300
Organization A noiseless patient Spider
Message-ID <87a53kdfpx.fsf@somewhere.edu> (permalink)

Show all headers | View raw


The program below only copies text files on purpose---because we haven't
learned about binary files in this course yet.  (So I could catch a
UnicodeDecodeError while writing.)  If an exception is raised while
writing, I need to delete the file that was created.  Can you think of
anything I'm missing?  I'm sure you can improve it by making it more
efficient for not reading the entire file into memory---I'm aware of
that, but also not going in that direction right now.  (I will worry
about that when I write a new version that does binary reading.)  In
summary, don't demand too much of a Python beginner here.  Thanks!

# -*- mode: python; python-indent-offset: 2 -*-
import os
import sys

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

def usage(program):
  return f"""usage: python {program} source.txt destination.txt"""

def main(argc, argv):
  if argc != 3:
    raise SystemExit(usage(argv[0]))
  copy(argv[1], argv[2])

if __name__ == "__main__":
  main(len(sys.argv), sys.argv)

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

can you improve this text-only beginner copy program? Ethan Carter <ec1828@somewhere.edu> - 2025-08-27 11:03 -0300
  Re: can you improve this text-only beginner copy program? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-08-27 15:12 +0000
    Re: can you improve this text-only beginner copy program? Ethan Carter <ec1828@somewhere.edu> - 2025-08-27 13:57 -0300
      Re: can you improve this text-only beginner copy program? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-08-27 17:45 +0000
      Re: can you improve this text-only beginner copy program? Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> - 2025-08-27 22:21 +0200
      Re: can you improve this text-only beginner copy program? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-08-28 21:05 +0100
        Re: can you improve this text-only beginner copy program? (Posting On Python-List Prohibited) Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-08-28 22:17 +0000
  Re: can you improve this text-only beginner copy program? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-08-27 21:09 +0000
    Re: can you improve this text-only beginner copy program? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-08-28 21:15 +0100
      Re: can you improve this text-only beginner copy program? ram@zedat.fu-berlin.de (Stefan Ram) - 2025-08-28 20:50 +0000
  Re: can you improve this text-only beginner copy program? (Posting On Python-List Prohibited) Lawrence D’Oliveiro <ldo@nz.invalid> - 2025-08-28 01:36 +0000

csiph-web