Let say I have this code that moves files in another directory:
import os, shutil
try:
os.chdir(rec.gec_daily_dir)
direct = os.listdir(/src/dir/)
for f in direct:
if f[-3:] == 'xls':
shutil.move(f, /archive/dir/)
except TypeError:
print "Directory or file not found"
So how could I archive (or compress) file just before moving it to antoher directory (does not matter zip or tar.gz)? (original file should only be in archive state, not like when you manually archive it, it leaves original file un-archived)
I saw there a lib tarfile
, where you could do something like that, but I saw some examples ofr archiving whole directory, but what I need is to archive each file separately.
Example
Init state
src/dir/
files in there:
file1.xls, file2.xls
archive/dir
file in there: none
Now compressing files and moving them to another directory:
Final state
src/dir/
file in there: none
archive/dir
files in there:
file1.zip, file2.zip (or .gz)