Top-rated Python recipes tagged "files"http://code.activestate.com/recipes/langs/python/tags/files/top/2016-12-17T19:08:33-08:00ActiveState Code RecipesSorting 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>
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>
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>
Simple File Splitter/Combiner module (Python)
2003-11-06T07:14:42-08:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/224800-simple-file-splittercombiner-module/
<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 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>
Recursive file/folder cleaner (Python)
2011-02-12T20:30:59-08:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576643-recursive-filefolder-cleaner/
<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="http://code.activestate.com/recipes/134892/" rel="nofollow">http://code.activestate.com/recipes/134892/</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 Kumarhttp://code.activestate.com/recipes/users/4005268/http://code.activestate.com/recipes/498189-imap-mail-server-attachment-handler/
<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 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>
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>
Watching a directory tree on Unix (Python)
2003-08-11T18:23:58-07:00A.M. Kuchlinghttp://code.activestate.com/recipes/users/681412/http://code.activestate.com/recipes/215418-watching-a-directory-tree-on-unix/
<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 Taylorhttp://code.activestate.com/recipes/users/1250863/http://code.activestate.com/recipes/208993-compute-relative-path-from-one-directory-to-anothe/
<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 Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/194372-build-a-white-list-of-email-address-from-sent-item/
<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: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>
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:
<br/>
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 (>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 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>
helpful 5-liner version of os.makedirs (Python)
2002-03-18T22:17:56-08:00H. Krekelhttp://code.activestate.com/recipes/users/267664/http://code.activestate.com/recipes/117243-helpful-5-liner-version-of-osmakedirs/
<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 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>
[xtopdf] Publish Delimiter-Separated Values (DSV data) to PDF (Python)
2016-12-17T19:08:33-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580736-xtopdf-publish-delimiter-separated-values-dsv-data/
<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 Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580727-convert-wildcard-text-files-to-pdf-with-xtopdf-eg-/
<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="http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html" rel="nofollow">http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html</a></p>
<p>More details on running the program, and sample output, are available here:</p>
<p><a href="http://jugad2.blogspot.in/2016/12/xtopdf-wildcard-text-files-to-pdf-with.html" rel="nofollow">http://jugad2.blogspot.in/2016/12/xtopdf-wildcard-text-files-to-pdf-with.html</a></p>
Batch conversion of text files to PDF with fileinput and xtopdf (Python)
2016-11-07T20:28:01-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580715-batch-conversion-of-text-files-to-pdf-with-fileinp/
<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="https://bitbucket.org/vasudevram/xtopdf" rel="nofollow">https://bitbucket.org/vasudevram/xtopdf</a></p>
<p>and a guide to installing and using xtopdf is here:</p>
<p><a href="http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html" rel="nofollow">http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html</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>