Ich habe ein Problem!
Ich möchte ein "wxGauge" Widget mit der Funktion "shutil.copy" verbinden.
Sozusagen eine Fortschrittsanzeige für eine Kopierfunktion von Files.
GUI-Tool ist WxPython.
Wie geht das?

Danke!
Harry
Da wirst Du wohl eine eigene copy()-Funktion schreiben müssen. shutil.copy() kehrt doch erst wieder zurück, wenn der Kopiervorgang beendet ist und bietet keine Möglichkeit, eine Anzeigefunktion einzuklinken.HarryH hat geschrieben: Sozusagen eine Fortschrittsanzeige für eine Kopierfunktion von Files.
Code: Alles auswählen
#!/usr/bin/env python
import os, sys, stat
def progress(p):
"""draw a little progress bar showing the percentage p"""
x = int(p/2)
y = 50 - x
sys.stdout.write('\r|%s%s| % 3d%% |' % ('#'*x, '_'*y, p))
if p == 100:
sys.stdout.write(' Done!\n')
sys.stdout.flush()
def copy(src, dst, pfunc=None, buflen=1024*16):
"""copy a file src to dst, calling the progress function
pfunc to give a little feedback, copy buflen bytes at once"""
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
if pfunc:
size = os.stat(src)[stat.ST_SIZE]
bytes = 0
fsrc = None
fdst = None
try:
fsrc = open(src, 'rb')
fdst = open(dst, 'wb')
while 1:
buf = fsrc.read(buflen)
if not buf:
break
fdst.write(buf)
if pfunc:
bytes += len(buf)
pfunc(100*bytes/size)
finally:
if fdst:
fdst.close()
if fsrc:
fsrc.close()
if hasattr(os, 'chmod'):
st = os.stat(src)
mode = stat.S_IMODE(st[stat.ST_MODE])
os.chmod(dst, mode)
if __name__ == '__main__':
if len(sys.argv) < 3:
print "Usage: %s <source> <destination>" % (sys.argv[0])
sys.exit(42)
src, dst = sys.argv[1:3]
copy(src, dst, progress)