image - Copying files with python -
i'm relatively new python, , part of way through "learning python hard way," have question.
so, i've read, if want make copy of text file, can open , read contents variable, write variable different file. i've tested out images, , seems work. there downsides method of copying i'll run later, , there file types won't work for?
thank much!
you should use shutil.copyfile()
or shutil.copyfileobj()
instead, efficiently , correctly using buffer.
not particularly hard, shutil.copyfileobj()
implemented as:
def copyfileobj(fsrc, fdst, length=16*1024): """copy data file-like object fsrc file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf)
this makes sure memory isn't filled big file, reading file in chunks instead. also, .read()
not guaranteed return of data of file, end not copying of data if don't loop until .read()
returns empty string.
Comments
Post a Comment