Most viewed recipes tagged "files"http://code.activestate.com/recipes/tags/files/views/2010-02-24T08:30:25-08:00ActiveState Code Recipestail -f in Python (Python) 2002-10-16T20:17:44-07:00Erik Max Francishttp://code.activestate.com/recipes/users/752960/http://code.activestate.com/recipes/157035-tail-f-in-python/ <p style="color: grey"> Python recipe 157035 by <a href="/recipes/users/752960/">Erik Max Francis</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>A simple implementation of the standard UNIX utility tail -f in Python.</p> Locating files throughout a directory tree (Python) 2009-12-02T07:30:27-08:00Simon Brunninghttp://code.activestate.com/recipes/users/98010/http://code.activestate.com/recipes/499305-locating-files-throughout-a-directory-tree/ <p style="color: grey"> Python recipe 499305 by <a href="/recipes/users/98010/">Simon Brunning</a> (<a href="/recipes/tags/files/">files</a>). Revision 3. </p> <p>os.walk is a very nice replacement for os.path.walk, which I never did feel comfortable with. There's one very common pattern of usage, though, which still benefits from a simple helper-function; locating all files matching a given file-name pattern within a directory tree.</p> Dupinator -- detect and delete duplicate files (Python) 2005-01-09T12:31:21-08:00Bill Bumgarnerhttp://code.activestate.com/recipes/users/2250923/http://code.activestate.com/recipes/362459-dupinator-detect-and-delete-duplicate-files/ <p style="color: grey"> Python recipe 362459 by <a href="/recipes/users/2250923/">Bill Bumgarner</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Point this script at a folder or several folders and it will find and delete all duplicate files within the folders, leaving behind the first file found of any set of duplicates. It is designed to handle hundreds of thousands of files of any size at a time and to do so quickly. It was written to eliminate duplicates across several photo libraries that had been shared between users. As the script was a one-off to solve a very particular problem, there are no options nor is it refactoring into any kind of modules or reusable functions.</p> portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python) 2008-05-16T21:12:08-07:00Jonathan Feinberghttp://code.activestate.com/recipes/users/1511/http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/ <p style="color: grey"> Python recipe 65203 by <a href="/recipes/users/1511/">Jonathan Feinberg</a> (<a href="/recipes/tags/files/">files</a>). Revision 7. </p> <p>Synopsis:</p> <p>import portalocker file = open("somefile", "r+") portalocker.lock(file, portalocker.LOCK_EX) file.seek(12) file.write("foo") file.close()</p> tree.py - graphically displays the directory structure of a specified path (Python) 2003-08-21T00:50:43-07:00Doug Dahmshttp://code.activestate.com/recipes/users/129202/http://code.activestate.com/recipes/217212-treepy-graphically-displays-the-directory-structur/ <p style="color: grey"> Python recipe 217212 by <a href="/recipes/users/129202/">Doug Dahms</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>The following program displays the directory structure of a specified path using ASCII characters. The program can optionally display files in addition to directories. This program functions similar to the windows 'tree' command.</p> Find the oldest (or yougest) of a list of files (Python) 2009-06-10T16:08:13-07:00Micah Elliotthttp://code.activestate.com/recipes/users/2649403/http://code.activestate.com/recipes/576804-find-the-oldest-or-yougest-of-a-list-of-files/ <p style="color: grey"> Python recipe 576804 by <a href="/recipes/users/2649403/">Micah Elliott</a> (<a href="/recipes/tags/age/">age</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/queue/">queue</a>). </p> <p>Sometimes you need to perform an operation on the oldest of a set of files. Using <em>get_oldest_file</em> you could implement an age-based priority queue that processes files from oldest to newest. The list of files you pass in may be from a <em>glob</em> of a single directory or some more elaborate search routine.</p> A python replacement for java.util.Properties (Python) 2006-07-28T08:52:19-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/496795-a-python-replacement-for-javautilproperties/ <p style="color: grey"> Python recipe 496795 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/files/">files</a>). Revision 5. </p> <p>This recipe provides a quick and easy way to process Java properties files using pure Python. Of course, Jython can always be used, but in situations where Jython cannot be used, this recipe provides a sure-fire drop-in replacement. The Properties class is modelled to duplicate the behaviour of the original as closely as possible.</p> File Unzip (Python) 2005-04-29T05:29:48-07:00Doug Toltonhttp://code.activestate.com/recipes/users/468468/http://code.activestate.com/recipes/252508-file-unzip/ <p style="color: grey"> Python recipe 252508 by <a href="/recipes/users/468468/">Doug Tolton</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>A Python class to extract zip files. It's also written for easy use as a standalone script from the commandline.</p> easy file parsing (Python) 2006-09-18T18:25:42-07:00Michael Haimeshttp://code.activestate.com/recipes/users/2955668/http://code.activestate.com/recipes/496910-easy-file-parsing/ <p style="color: grey"> Python recipe 496910 by <a href="/recipes/users/2955668/">Michael Haimes</a> (<a href="/recipes/tags/files/">files</a>). Revision 3. </p> <p>This is useful for making easy and human to write definition files for whatever use. For example, lets say you are writing a simulator and you need an easy, human way to define parts about a given world like light sources, obstacles, and dimensions. see the discussion for this example fleshed out in code.</p> Backup your files (Python) 2008-04-08T10:16:26-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/191017-backup-your-files/ <p style="color: grey"> Python recipe 191017 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/files/">files</a>). Revision 5. </p> <p>Makes backup versions of files</p> Python robocopier - Advanced Directory Synchronization module (Python) 2003-12-04T08:04:57-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/231501-python-robocopier-advanced-directory-synchronizati/ <p style="color: grey"> Python recipe 231501 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/files/">files</a>). Revision 6. </p> <p>This program is an advanced directory synchronization and update tool. It can be used to update content between two directories, synchronize them, or just report the difference in content between them. It uses the syntax of the 'diff' program in printing the difference.</p> Sorting big files the Python 2.4 way (Python) 2006-04-13T10:43:13-07:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/466302-sorting-big-files-the-python-24-way/ <p style="color: grey"> Python recipe 466302 by <a href="/recipes/users/1599156/">Nicolas Lehuen</a> (<a href="/recipes/tags/files/">files</a>). Revision 6. </p> <p>This recipe can be used to sort big files (much bigger than the available RAM) according to a key. The sort is guaranteed to be stable on Python 2.3.</p> Converting Word documents to text (Python) 2004-09-02T11:59:38-07:00Simon Brunninghttp://code.activestate.com/recipes/users/98010/http://code.activestate.com/recipes/279003-converting-word-documents-to-text/ <p style="color: grey"> Python recipe 279003 by <a href="/recipes/users/98010/">Simon Brunning</a> (<a href="/recipes/tags/files/">files</a>). Revision 4. </p> <p>Here's a script to save all Word documents in and below a given directory to text.</p> Clean up a directory tree (Python) 2003-04-03T08:06:50-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/193736-clean-up-a-directory-tree/ <p style="color: grey"> Python recipe 193736 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This recipe can be used to clean up a directory tree irrespective of whether the directory tree contains non-empty directories. As long as the user has permission to remove the files, this will work.</p> Simple conversion of excel files into CSV and YAML (Python) 2008-02-14T00:39:22-08:00Philip Kromerhttp://code.activestate.com/recipes/users/552075/http://code.activestate.com/recipes/546518-simple-conversion-of-excel-files-into-csv-and-yaml/ <p style="color: grey"> Python recipe 546518 by <a href="/recipes/users/552075/">Philip Kromer</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Takes an excel file, dumps out a series of CSV files (one for each sheet, named for the file and sheet) and a YAML file (an array of sheets, each sheet a dict containing the table_name and the table_data, a 2-d array of cell values).</p> <p>Inspired by Bryan Niederberger's "Easy Cross Platform Excel Parsing With Xlrd", <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/483742" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/483742</a> As opposed to his code, this script makes no attempt to understand the structure of the sheet (look for header cells, etc) -- it simply reads, converts, dumps.</p> Progress bar class (Python) 2002-12-11T12:53:21-08:00Randy Pargmanhttp://code.activestate.com/recipes/users/866475/http://code.activestate.com/recipes/168639-progress-bar-class/ <p style="color: grey"> Python recipe 168639 by <a href="/recipes/users/866475/">Randy Pargman</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Here is a little class that lets you present percent complete information in the form of a progress bar using the '#' character to represent completed portions, space to represent incomplete portions, and the actual percent done (rounded to integer) displayed in the middle:</p> <p>[############# 33% &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]</p> <p>When you initialize the class, you specify the minimum number (usually 0), the maximum number (your file size, for example), and the number of characters wide you would like the progress bar to be. Note that width includes the brackets [] on the ends of the progress bar as well.</p> <p>You'd probably want to use this in conjuction with the curses module, or something like that so you can over-write the same portion of the screen to make your updates 'animated'.</p> a simple pycrypto-Blowfish encryption script (Python) 2006-06-01T21:46:24-07:00Guy Incognitohttp://code.activestate.com/recipes/users/2908715/http://code.activestate.com/recipes/496763-a-simple-pycrypto-blowfish-encryption-script/ <p style="color: grey"> Python recipe 496763 by <a href="/recipes/users/2908715/">Guy Incognito</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>Nothing fancy, just a script for encrypting/decrypting small files. The -c option is handy for those password files you'd rather leave obfuscated.</p> <p>Any suggestions on making it pipeable, more secure, or suitable for large files are welcome.</p> test if a file or string is text or binary (Python) 2003-01-11T01:13:40-08:00Andrew Dalkehttp://code.activestate.com/recipes/users/912777/http://code.activestate.com/recipes/173220-test-if-a-file-or-string-is-text-or-binary/ <p style="color: grey"> Python recipe 173220 by <a href="/recipes/users/912777/">Andrew Dalke</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>Here's a quick test to see if a file or string contains text or is binary. The difference between text and binary is ill-defined, so this duplicates the definition used by Perl's -T flag, which is: &lt;br/&gt; The first block or so of the file is examined for odd characters such as strange control codes or characters with the high bit set. If too many strange characters (&gt;30%) are found, it's a -B file, otherwise it's a -T file. Also, any file containing null in the first block is considered a binary file.</p> Read data from .zip files (Python) 2001-03-14T17:22:33-08:00Paul Prescodhttp://code.activestate.com/recipes/users/11203/http://code.activestate.com/recipes/52265-read-data-from-zip-files/ <p style="color: grey"> Python recipe 52265 by <a href="/recipes/users/11203/">Paul Prescod</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Python can work directly with data in zip files. You can look at the list of items in the directory and work with the data files themselves.</p> Access grep from python (Python) 2010-02-24T08:30:25-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577069-access-grep-from-python/ <p style="color: grey"> Python recipe 577069 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/linux/">linux</a>). </p> <p>The recipe below passes a filename and an argument to grep, returning the stdout and stderr. Each line in the stdout will have its line number prepended.</p>