Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Getting files into an archive without the directory?
    primarykey
    data
    text
    <p>I've been learning python for about 3 weeks now, and I'm currently trying to write a little script for sorting files (about 10.000) by keywords and date appearing in the filename. Files before a given date should be added to an archive. The sorting works fine, but not the archiving</p> <p>It creates an archive - the name is fine - but in the archive is the complete path to the files. If i open it, it looks like: <code>folder1 -&gt; folder2 -&gt; folder3 -&gt; files</code>.</p> <p>How can I change it such that the archive only contains the files and not the whole structure?</p> <p>Below is a snippet with my zip function, <code>node</code> is the path where the files were before sorting, <code>folder</code> is a subfolder with the files sorted by a keyword in the name, <code>items</code> are the folders with files sorted by date.</p> <p>I am using Python 2.6</p> <pre><code>def ZipFolder(node, zipdate): xynode = node + '/xy' yznode = node + '/yz' for folder in [xynode,yznode]: items = os.listdir(folder) for item in items: itemdate = re.findall('(?&lt;=_)\d\d\d\d-\d\d', item) print item if itemdate[0] &lt;= zipdate: arcname = str(item) + '.zip' x = zipfile.ZipFile(folder + '/' + arcname, mode='w', compression = zipfile.ZIP_DEFLATED) files = os.listdir(folder + '/' + item) for f in files: x.write(folder + '/' + item + '/' + f) print 'writing ' + str(folder + '/' + item + '/' + f) + ' in ' + str(item) x.close() shutil.rmtree(folder + '/' + item) return </code></pre> <p>I am also open to any suggestions and improvements.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload