3

Python - archiving the file before moving to another directory?

 2 years ago
source link: https://www.codesd.com/item/python-archiving-the-file-before-moving-to-another-directory.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python - archiving the file before moving to another directory?

advertisements

This question already has an answer here:

  • How to create full compressed tar file using Python? 3 answers

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)


Archives don't really make sense if all you going to do is put one file into each of them.

Or did you mean to compress them, e.g. using the gzip or bz2 module?

If you indeed really want archives with only a single file, create a tar or ZIP object and just add it straight away, e.g. for TarFile.add.

Note that while it is very common when using unix-like operating systems to compress single files using bz2 or gzip doing so is very uncommon on other platforms, e.g. Windows. There the recommendation would be to use ZIP files, even for single files, since they are handled well by applications (Windows Explorer and others).

To put a single file into a ZIP file do something similar to this:

import zipfile
# ...

with zipfile.ZipFile(nameOfOrginalFile + ".zip", "w", zipfile.ZIP_DEFLATED) as zip_file:
    zip_file.write(nameOfOriginalFile)

Not passing ZIP_DEFLATED to ZipFile will result in an uncompressed zip file.

To compress a single file using e.g. gzip:

import gzip

with gzip.GzipFile(nameoforiginalFile + ".gz", "w") as gz:
    with open(nameoforignalfile) as inp_file;
        shutil.copyfileobj(inp_file, gz)

The bz2 and lzma (not available for Python 2) APIs are the same, just import bz2/lzma and use bz2.BZ2File instead.

After both with blocks you can delete the original file (os.remove(file)) and move the archive file to the correct location. Alternatively, create the archive file directly in the correct location (os.path.join the target location and the archive name).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK