Most viewed recipes tagged "file"http://code.activestate.com/recipes/tags/file/views/2017-04-03T13:37:34-07:00ActiveState Code RecipesGet columns of data from text files (Python) 2010-10-28T16:18:19-07:00aliniumhttp://code.activestate.com/recipes/users/4175605/http://code.activestate.com/recipes/577444-get-columns-of-data-from-text-files/ <p style="color: grey"> Python recipe 577444 by <a href="/recipes/users/4175605/">alinium</a> (<a href="/recipes/tags/columns/">columns</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/parsing/">parsing</a>). </p> <p>Read in a tab-delimited (or any separator-delimited like CSV) file and store each column in a list that can be referenced from a dictionary. The keys for the dictionary are the headings for the columns (if any). All data is read in as strings.</p> Decoding Binary Files (Python) 2011-03-15T00:11:10-07:00Yony Kochinskihttp://code.activestate.com/recipes/users/4175703/http://code.activestate.com/recipes/577610-decoding-binary-files/ <p style="color: grey"> Python recipe 577610 by <a href="/recipes/users/4175703/">Yony Kochinski</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decode/">decode</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>One way to read files that contain binary fields is to use the <code>struct</code> module. However, to do this properly one must learn struct's format characters, which may look especially cryptic when sprinkled around the code. So instead, I use a wrapper object that presents a simple interface as well as type names that are more inline with many <a href="http://en.wikipedia.org/wiki/Interface_description_language">IDLs</a>.</p> Composing a POSTable HTTP request with multipart/form-data Content-Type to simulate a form/file upload. (Python) 2014-03-08T17:34:38-08:00István Pásztorhttp://code.activestate.com/recipes/users/4189380/http://code.activestate.com/recipes/578846-composing-a-postable-http-request-with-multipartfo/ <p style="color: grey"> Python recipe 578846 by <a href="/recipes/users/4189380/">István Pásztor</a> (<a href="/recipes/tags/field/">field</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/form/">form</a>, <a href="/recipes/tags/html/">html</a>, <a href="/recipes/tags/httpclient/">httpclient</a>, <a href="/recipes/tags/mime/">mime</a>, <a href="/recipes/tags/multipart/">multipart</a>, <a href="/recipes/tags/post/">post</a>, <a href="/recipes/tags/upload/">upload</a>, <a href="/recipes/tags/web/">web</a>). Revision 5. </p> <p>This code is useful if you are using a http client and you want to simulate a request similar to that of a browser that submits a form containing several input fields (including file upload fields). I've used this with python 2.x.</p> Find file in subdirectory (Python) 2010-02-02T11:35:53-08:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577027-find-file-in-subdirectory/ <p style="color: grey"> Python recipe 577027 by <a href="/recipes/users/4172918/">Daniel Cohn</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/subdirectory/">subdirectory</a>). Revision 2. </p> <p>Walks subdirectories to find a file and returns . Default start location is the current working directory. Optionally, a different directory can be set as the search's starting location.</p> Sorting big files the Python 2.6 way (Python) 2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/ <p style="color: grey"> Python recipe 576755 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p> Convert HTML text to PDF with Beautiful Soup and xtopdf (Python) 2015-01-28T22:20:53-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579014-convert-html-text-to-pdf-with-beautiful-soup-and-x/ <p style="color: grey"> Python recipe 579014 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/format/">format</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/reportgeneration/">reportgeneration</a>, <a href="/recipes/tags/reporting/">reporting</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to convert the text in an HTML document to PDF. It uses the Beautiful Soup and xtopdf Python libraries. Beautiful Soup is a library for HTML parsing and content extraction. xtopdf is a library for PDF creation from other formats, including text and many others.</p> Multiprocess-safe logging file-handler + interprocess RLock (Python) 2010-09-22T17:30:10-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577395-multiprocess-safe-logging-file-handler-interproces/ <p style="color: grey"> Python recipe 577395 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/rlock/">rlock</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>). Revision 11. </p> <p>A Python 2.x/3.x-compatibile <strong>multiprocess-safe logging file-handler</strong> (logging.FileHandler replacement, designed for logging to a single file from multiple independent processes) together with a simple <strong>interprocess recursive lock</strong> -- universal abstract classes + Unix/Linux implementation.</p> <p><strong>Update:</strong> It's is a deeply revised version. Especially, now it --</p> <ul> <li>is Python 2.4, 2.5, 2.6, 3.1 -compatibile (previously Py>=2.6 was needed); probably works also with 2.7, 3.0 and 3.2 (but not tested if it does);</li> <li>is multiprocess-safe as well as thread-safe (proviously thread safety within a process was missed);</li> <li>is based on public interfaces only (previously FileHandler._open() was called and overriden);</li> <li>implement full RLock instance interface, as documented for threading.RLock (previously non-blocking mode and context-manager interface were missing).</li> </ul> <p>The module contains:</p> <ul> <li>Unix/Linux-only example implementation (with flock-based locking): <strong>FLockRLock</strong> and <strong>FLockFileHandler</strong> classes.</li> <li>universal abstract classes -- which may be useful at developing implementation for non-Unix platforms: <strong>MultiprocessRLock</strong>, <strong>MultiprocessFileHandler</strong>, <strong>LockedFileHandler</strong>,</li> </ul> <p>Also a quick-and-dirty test was added.</p> <p><strong>It is still an alpha version -- I'll be very grateful for any feedback.</strong></p> <hr /> <p><strong>Further updates:</strong></p> <ul> <li><p>2010-09-20: Some corrections, especially: non-blocking mode bug in MultiprocessRLock.acquire() fixed; _test() function improved; plus fixes in the description below.</p></li> <li><p>2010-09-22: _test() improved and moved to description section. Mistaken copyright-notice removed.</p></li> </ul> Safely and atomically write to a file (Python) 2016-03-23T14:14:26-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/579097-safely-and-atomically-write-to-a-file/ <p style="color: grey"> Python recipe 579097 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/save/">save</a>). Revision 3. </p> <p>Saving the user's data is risky. If you write to a file directly, and an error occurs during the write, you may corrupt the file and lose the user's data. One approach to prevent this is to write to a temporary file, then only when you know the file has been written successfully, over-write the original. This function returns a context manager which can make this more convenient.</p> <p>Update: this now uses <code>os.replace</code> when available, and hopefully will work better on Windows.</p> Searching .dll and .exe files in PATH (Python) 2014-10-06T09:22:53-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/576522-searching-dll-and-exe-files-in-path/ <p style="color: grey"> Python recipe 576522 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/dll/">dll</a>, <a href="/recipes/tags/exe/">exe</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/version/">version</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 7. </p> <p>Returns the pathnames of the file (.exe or .dll) which would be loaded/executed in the current environment. It uses some dirs from configuration (SystemDir, WindowsDir) and dirs from PATH.</p> <p>To obtain version info it uses code from: <a href="http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/file/tip/win32/Demos/getfilever.py" rel="nofollow">http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/file/tip/win32/Demos/getfilever.py</a></p> <p>Example of usage:</p> <pre class="prettyprint"><code> c:\tools\pyscripts\scripts&gt;which_dll.py libpq.dll 2008-06-09 02:58:26 167936 [b] c:\postgresql\8.3\bin\libpq.dll ver:8.3.3.8160 2008-03-17 01:47:50 167936 [b] c:\tools\libpq.dll ver:8.3.1.8075 2008-03-17 01:47:50 167936 [b] g:\public\libpq.dll ver:8.3.1.8075 trying to load "libpq.dll" ... c:\postgresql\8.3\bin\libpq.dll loaded </code></pre> Tail a continuously growing file (like tail -f filename) (Python) 2011-05-20T16:32:59-07:00Jason Morrisshttp://code.activestate.com/recipes/users/4174039/http://code.activestate.com/recipes/577710-tail-a-continuously-growing-file-like-tail-f-filen/ <p style="color: grey"> Python recipe 577710 by <a href="/recipes/users/4174039/">Jason Morriss</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/rotate/">rotate</a>, <a href="/recipes/tags/tail/">tail</a>). </p> <p>This is my version of a "File Tail" class for Python3 (will not work on Python2 w/o a couple of modifications). My original inspiration came from the perl File::Tail module.</p> <p>Transparently handles files that get rotated or truncated. </p> <ul> <li>Does not take 100% CPU. </li> <li>Does not take up much memory.</li> <li>Is capable of handling any size log file.</li> <li><em>Not tested on Windows</em></li> </ul> <p>Example:</p> <pre class="prettyprint"><code>from filetail import FileTail tail = FileTail("/var/log/syslog") for line in tail: print(line, end="") </code></pre> fcntl.flock() (Unix file lock) behaviour sampling script (Python) 2010-09-22T00:11:12-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577404-fcntlflock-unix-file-lock-behaviour-sampling-scrip/ <p style="color: grey"> Python recipe 577404 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 3. </p> <p>A quick <em>fcntl.flock(fcntl.LOCK_EX | fcntl.LOCK_NB)</em> call sampling script: with <em>one file object</em> (and descriptor) or <em>separate file objects</em> (and different descriptors) pointing to the same filesystem path -- with/without <strong>threading</strong> or <strong>forking</strong>.</p> <p>It's rather exemplum-and-educational piece of code than utility-script, unless somebody has to few slots in their memory to remember that <strong>flock</strong> is file-descriptor-tacked (then quick run of the script can save web-searching) :)</p> File browser for tkinter using idle GUI library (Python) 2017-04-03T13:37:34-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580772-file-browser-for-tkinter-using-idle-gui-library/ <p style="color: grey"> Python recipe 580772 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/browser/">browser</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/idlelib/">idlelib</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>Idle is installed by default on windows.</p> <p>For Ubuntu, Linux Mint and Debian run:</p> <pre class="prettyprint"><code> sudo apt-get install idle-python2.7 </code></pre> <p>A tree structure is drawn on a Tkinter Canvas object. A tree item is an object with an icon and a text. The item maybe be expandable and/or editable. A tree item has two kind of icons: A normal icon and an icon when the item is selected. To create the tree structure, it's necessary to create a link between tree items, using a parent-child relationship. </p> <p>The canvas is built using a <em>idlelib.TreeWidget.ScrolledCanvas</em> class. The <em>frame</em> attribute of this object contains a <em>Frame</em> Tkinter widget prepared for scrolling. This frame allocates a Tkinter <em>Canvas</em> and Tkinter <em>Scrollbars</em>. This is the signature:</p> <pre class="prettyprint"><code> ScrolledCanvas(master, **options_for_the_tkinter_canvas) </code></pre> <p>It accepts exactly the same arguments than a <em>Canvas</em> widget.</p> <p>A tree item should be a subclass of <em>idlelib.TreeWidget.TreeItem</em>.</p> <p>The parent-child relationship between tree items is established using the <em>idlelib.TreeWidget.TreeNode</em> class.</p> <p>This is the signature for <strong>TreeNode(canvas, parent, item)</strong>:</p> <ul> <li><em>canvas</em> should be a <em>ScrolledCanvas</em> instance. </li> <li><em>parent</em> should be the parent item. Leave that to <em>None</em> to create a root node.</li> <li><em>item</em> should be the child item</li> </ul> <p><em>FileTreeItem</em> is a type of <em>TreeItem</em>. The only argument of a file tree item is a path.</p> <p>Here there is an example of a custom tree item:</p> <p><a href="https://code.activestate.com/recipes/579077-bookmarks-browser-for-firefox/" rel="nofollow">https://code.activestate.com/recipes/579077-bookmarks-browser-for-firefox/</a></p> <p>To create your own tree items, it's required to subclass <em>TreeItem</em>. These are the methods that should be overrided:</p> <ul> <li><em>GetText:</em> It should return the text string to display.</li> <li><em>GetLabelText:</em> It should return label text string to display in front of text (Optional).</li> <li><em>IsExpandable:</em> It should return a boolean indicating whether the istem is expandable</li> <li><em>IsEditable:</em> It should return a boolean indicating whether the item's text may be edited.</li> <li><em>SetText:</em> Get the text to change if the item is is editable</li> <li><em>GetIconName:</em> Return name of icon to be displayed normally (Icons should be included in <em>ICONDIR</em> directory)</li> <li><em>GetSelectedIconName:</em> Return name of icon to be displayed when selected (Icons should be included in <em>ICONDIR</em> directory).</li> <li><em>GetSubList:</em> It should return a list of child items (Optional. If not defined, the element is not expandable)</li> <li><em>OnDoubleClick:</em> Called on a double-click on the item. (Optional)</li> </ul> <p>Icons should be included in "Icons" subdirectory of path to idlelib library. If you want to use other path, just change <em>ICONDIR</em> variable to path to your icons:</p> <pre class="prettyprint"><code> import idlelib idlelib.ICONDIR = "Your path to your icons" </code></pre> <p>Run this to find the path to <em>idlelib</em> module:</p> <pre class="prettyprint"><code> python -c "import idlelib; print(idlelib.__file__)" </code></pre> Python Awesome DD (Python) 2014-07-12T07:29:59-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578907-python-awesome-dd/ <p style="color: grey"> Python recipe 578907 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/dd/">dd</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/output/">output</a>, <a href="/recipes/tags/progress_bar/">progress_bar</a>). Revision 3. </p> <p>Imagine if the *nix utilities 'dd', 'wget', and 'pv' had a baby. That baby would be named 'padd'. A 'dd' replacement (most commonly used options translate with no problem), that gives progress output like 'pv' and supports (currently) http(s) and ftp input sources, with more planned (sftp for one). Output support for ftp, sftp, and http(s) is also planned.</p> File comparison utility in Python (Python) 2016-03-26T18:31:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580631-file-comparison-utility-in-python/ <p style="color: grey"> Python recipe 580631 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>). </p> <p>This is a recipe to compare any two files via a Python command-line program. It is like a basic version of the cmp command of Unix or the fc.exe (file compare) command of Windows.</p> Storing a Single Username and Encrypted Password to a File (Python) 2013-01-31T02:07:25-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578443-storing-a-single-username-and-encrypted-password-t/ <p style="color: grey"> Python recipe 578443 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote to check user passwords. It stores data using SHA one-way function. For more information <a href="http://thelivingpearl.com/2013/01/29/authentication-of-users-and-passwords-in-python/">Authentication Of Users And Passwords In Python</a></p> Make unique file name (Python) 2010-04-18T21:07:52-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577200-make-unique-file-name/ <p style="color: grey"> Python recipe 577200 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/name/">name</a>, <a href="/recipes/tags/rename/">rename</a>). Revision 2. </p> <p>Sometimes it is important to save data in the file but the file with the specified name already exists. This function creates a file name that is similar to the original by adding a unique numeric suffix. This avoids the renaming of existing files.</p> Python one-liner to compare two files (Python) 2016-03-28T21:36:31-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580632-python-one-liner-to-compare-two-files/ <p style="color: grey"> Python recipe 580632 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/programming/">programming</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>As the title says ...</p> <p>It prints True if the files compared are the same, and False if they differ (either in size or in content).</p> Find Duplicate Files (Python) 2014-10-12T21:14:05-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578950-find-duplicate-files/ <p style="color: grey"> Python recipe 578950 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Finds duplicate files which have same size and same content in the same directory or two different directories.</p> Sort sections and keys in .ini file (Python) 2008-12-19T05:46:05-08:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/576587-sort-sections-and-keys-in-ini-file/ <p style="color: grey"> Python recipe 576587 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/config/">config</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/ini/">ini</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 3. </p> <p>I use this program when I want to make .ini file more readable or compare two .ini files</p> File read/write routines (Python) 2010-04-18T17:18:50-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577199-file-readwrite-routines/ <p style="color: grey"> Python recipe 577199 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/read/">read</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/write/">write</a>). Revision 2. </p> <p>For small dirty hacks in Perl has a module File::Slurp. I wrote two simple functions when I moving from Perl to Python - one for reading files and second for writing files. Valuable data is list of lines or blob (additionally specified argument binmode=1).</p>