Top-rated recipes tagged "files" Code RecipesSorting big files the Python 2.4 way (Python) 2006-04-13T10:43:13-07:00Nicolas Lehuen <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> Locating files throughout a directory tree (Python) 2009-12-02T07:30:27-08:00Simon Brunning <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> File Unzip (Python) 2005-04-29T05:29:48-07:00Doug Tolton <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> Simple File Splitter/Combiner module (Python) 2003-11-06T07:14:42-08:00Anand <p style="color: grey"> Python recipe 224800 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/files/">files</a>). Revision 4. </p> <p>This module can be used to split any file, text or binary to equal sized chunks. It can also combine the chunks back to recreate the original file.</p> Read data from .zip files (Python) 2001-03-14T17:22:33-08:00Paul Prescod <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> Recursive file/folder cleaner (Python) 2011-02-12T20:30:59-08:00Alia Khouri <p style="color: grey"> Python recipe 576643 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/cleaner/">cleaner</a>, <a href="/recipes/tags/cleaning/">cleaning</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/folder/">folder</a>, <a href="/recipes/tags/pyc/">pyc</a>, <a href="/recipes/tags/recursion/">recursion</a>, <a href="/recipes/tags/svn/">svn</a>). Revision 24. </p> <p>This script recursively scans a given path and applies a cleaning 'action' to matching files and folders. By default files and folders matching the specified (.endswith) patterns are deleted. Alternatively, _quoted_ glob patterns can used with the '-g' or '--glob' option.</p> <p>By design, the script lists targets and asks permission before applying cleaning actions. It should be easy to extend this script with further actions and also more intelligent pattern matching functions.</p> <p>The getch (single key confirmation) functionality comes courtesy of <a href="" rel="nofollow"></a></p> <p>To use it, place the script in your path and call it something like 'clean':</p> <pre class="prettyprint"><code>Usage: clean [options] patterns deletes files/folder patterns: clean .svn .pyc clean -p /tmp/folder .svn .csv .bzr .pyc clean -g "*.pyc" clean -ng "*.py" converts line endings from windows to unix: clean -e .py clean -e -p /tmp/folder .py Options: -h, --help show this help message and exit -p PATH, --path=PATH set path -n, --negated clean everything except specified patterns -e, --endings clean line endings -g, --glob clean with glob patterns -v, --verbose </code></pre> IMAP mail server attachment handler (Python) 2006-10-10T03:39:22-07:00Suresh Kumar <p style="color: grey"> Python recipe 498189 by <a href="/recipes/users/4005268/">Suresh Kumar</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This python script monitors the IMAP mail server for the given account and moves the mails with attachments to "Downloadedmails" folder in server after downloading the attachments to the individual directories on localmachine with the timestamp.</p> a simple pycrypto-Blowfish encryption script (Python) 2006-06-01T21:46:24-07:00Guy Incognito <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> Dupinator -- detect and delete duplicate files (Python) 2005-01-09T12:31:21-08:00Bill Bumgarner <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> Watching a directory tree on Unix (Python) 2003-08-11T18:23:58-07:00A.M. Kuchling <p style="color: grey"> Python recipe 215418 by <a href="/recipes/users/681412/">A.M. Kuchling</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>The watch_directories() function takes a list of paths and a callable object, and then repeatedly traverses the directory trees rooted at those paths, watching for files that get deleted or have their modification time changed. The callable object is then passed two lists containing the files that have changed and the files that have been removed.</p> Compute relative path from one directory to another (Python) 2003-07-06T02:05:21-07:00Cimarron Taylor <p style="color: grey"> Python recipe 208993 by <a href="/recipes/users/1250863/">Cimarron Taylor</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Suppose your application needs to know the relative path from one path to another (say because you want to create a symbolic link, a relative reference in a URL, etc). These functions may be of help.</p> Build a white list of email address from "Sent Items" mailbox. (Python) 2003-04-16T17:48:52-07:00Noah Spurrier <p style="color: grey"> Python recipe 194372 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This extracts all of the To addresses from a file in standard mbox format. It is used on a "Sent Items" mailbox to build an address white list. Presumably everyone you send email to is a candidate for an email white list.</p> Backup your files (Python) 2008-04-08T10:16:26-07:00Anand <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> test if a file or string is text or binary (Python) 2003-01-11T01:13:40-08:00Andrew Dalke <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> tail -f in Python (Python) 2002-10-16T20:17:44-07:00Erik Max Francis <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> helpful 5-liner version of os.makedirs (Python) 2002-03-18T22:17:56-08:00H. Krekel <p style="color: grey"> Python recipe 117243 by <a href="/recipes/users/267664/">H. Krekel</a> (<a href="/recipes/tags/files/">files</a>). Revision 2. </p> <p>"makepath(path)" creates missing directories for path and returns a normalized absolute version of the path. As often the case with python, the documentation is more important than the code.</p> portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python) 2008-05-16T21:12:08-07:00Jonathan Feinberg <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.write("foo") file.close()</p> [xtopdf] Publish Delimiter-Separated Values (DSV data) to PDF (Python) 2016-12-17T19:08:33-08:00Vasudev Ram <p style="color: grey"> Python recipe 580736 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/formats/">formats</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tsv/">tsv</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to publish delimiter-separated values (a commonly used tabular data format) to PDF, using the xtopdf toolkit for PDF creation. It lets the user specify the delimiter via one of two command-line options - an ASCII code or an ASCII character. As Unix filters tend to do, it can operate either on standard input or on input filenames given as command-line arguments. In the case of multiple inputs via files, each input goes to a separate PDF output file.</p> Convert wildcard text files to PDF with xtopdf (e.g. report*.txt) (Python) 2016-12-06T20:37:30-08:00Vasudev Ram <p style="color: grey"> Python recipe 580727 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/globbing/">globbing</a>, <a href="/recipes/tags/patterns/">patterns</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/wildcard/">wildcard</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to convert all text files matching a filename wildcard to PDF, using the xtopdf PDF creation toolkit. For example, if you specify report<em>.txt as the wildcard, all files in the current directory that match report</em>.txt, will be converted to PDF, each in a separate PDF file. The original text files are not changed.</p> <p>Here is a guide to installing and using xtopdf:</p> <p><a href="" rel="nofollow"></a></p> <p>More details on running the program, and sample output, are available here:</p> <p><a href="" rel="nofollow"></a></p> Batch conversion of text files to PDF with fileinput and xtopdf (Python) 2016-11-07T20:28:01-08:00Vasudev Ram <p style="color: grey"> Python recipe 580715 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/batch/">batch</a>, <a href="/recipes/tags/batchmode/">batchmode</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to do a batch conversion of the content of multiple text files into a single PDF file, with a) an automatic page break after the content of each text file (in the PDF output), b) page numbering, and c) a header and footer on each page.</p> <p>It uses the fileinput module (part of the Python standard library), and xtopdf, a Python library for conversion of other formats to PDF.</p> <p>xtopdf is available here: <a href="" rel="nofollow"></a></p> <p>and a guide to installing and using xtopdf is here:</p> <p><a href="" rel="nofollow"></a></p> <p>Here is a sample run of the program:</p> <p>python BTTP123.pdf text1.txt text2.txt text3.txt</p> <p>This will read the content from the three text files specified and write it into the PDF file specified, neatly formatted.</p>