Popular recipes tagged "file"http://code.activestate.com/recipes/tags/file/2017-04-08T16:40:25-07:00ActiveState Code RecipesFile 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> Recursive fc/diff for Windows, optionally copy missing/different files (Tcl) 2017-04-08T16:40:25-07:00John Brearleyhttp://code.activestate.com/recipes/users/4184423/http://code.activestate.com/recipes/580776-recursive-fcdiff-for-windows-optionally-copy-missi/ <p style="color: grey"> Tcl recipe 580776 by <a href="/recipes/users/4184423/">John Brearley</a> (<a href="/recipes/tags/compare/">compare</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/tcl/">tcl</a>). </p> <p>Utility to add recursive capability to fc.exe on Windows. Will optionally copy/update missing or older files as needed. If you have a diff.exe in your path, say from having installed TkDiff utility, the diff.exe will be used as the prirmary compare tool, with fc.exe as the backup tool.</p> Tkinter file autocomplete entry (Python) 2017-04-03T15:18:36-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580771-tkinter-file-autocomplete-entry/ <p style="color: grey"> Python recipe 580771 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/autocomplete/">autocomplete</a>, <a href="/recipes/tags/entry/">entry</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 9. </p> <p>I define a "File_Entry" widget to make more easier to the final user to write and search for a path. I am using my own version of combobox widget:</p> <p><a href="https://code.activestate.com/recipes/580770-combobox-autocomplete/" rel="nofollow">https://code.activestate.com/recipes/580770-combobox-autocomplete/</a></p> Context Manager for an Arbitrary Number of Files in Python (Python) 2017-03-13T13:27:50-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580763-context-manager-for-an-arbitrary-number-of-files-i/ <p style="color: grey"> Python recipe 580763 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/open/">open</a>). Revision 2. </p> <p>The pattern using <code>with</code> together with <code>open()</code> to automatically close a file after leaving the context is well known. To open a fixed number you can simply nest these statements or use the comma notation. For having a context which represents an arbitrary number of open files you can use the <code>ExitStack</code> class, but only for Python 3.3+.</p> <p>For other Python versions I'm using the following class which I named <code>Files</code>. The presented implementation is only for reading files (for keeping it clear). Extending it for having various file modes should not pose a problem.</p> Simple directory list command with filename wildcard support (Python) 2016-12-02T20:52:56-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580724-simple-directory-list-command-with-filename-wildca/ <p style="color: grey"> Python recipe 580724 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/filesystem/">filesystem</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utilities/">utilities</a>). </p> <p>This recipe shows a simple directory listing program. It can accept multiple command-line arguments specifying filenames. These filenames can include wildcard characters like * (asterisk) and ? (question mark), as is common in OS command shells like bash (Unix) and CMD (Windows). Tested on Windows but should work on Unix too, since it uses no OS-specific functions, or rather, it does use them, but that happens under the hood, within the libraries used.</p> Process Delimiter-Separated Values data with Python (Python) 2016-11-24T23:57:35-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580722-process-delimiter-separated-values-data-with-pytho/ <p style="color: grey"> Python recipe 580722 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/dsv/">dsv</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This recipe shows how to read and process delimiter-separated values (DSV) data with a Python command-line program. It provides two ways of specifying the delimiter character, by an ASCII character or an ASCII code, which makes it more flexible than allowing only a character. It allows the DSV data to be specified as one or more filenames on the command line, or given via the standard input of the program.</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> 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> 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> 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> Shortcut Utility (Python) 2015-03-29T20:45:57-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579041-shortcut-utility/ <p style="color: grey"> Python recipe 579041 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/desktop/">desktop</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/ui/">ui</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>I dislike filling my desktop with shortcut icons in Windows. This code enables creating shortcuts in a CSV file.</p> SystemMutex (Python) 2015-01-09T10:14:15-08:00Fabio Zadroznyhttp://code.activestate.com/recipes/users/4180406/http://code.activestate.com/recipes/578998-systemmutex/ <p style="color: grey"> Python recipe 578998 by <a href="/recipes/users/4180406/">Fabio Zadrozny</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/filelock/">filelock</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/mutex/">mutex</a>). </p> <p>This module provides a way to create a mutex which is valid for the system (i.e.: it can be seen by multiple processes).</p> <p>Note that the mutex is kept until release_mutex() is called or when it's garbage-collected.</p> Split file in place into two parts (Python) 2015-02-11T20:08:35-08:00shavy89http://code.activestate.com/recipes/users/4191634/http://code.activestate.com/recipes/579025-split-file-in-place-into-two-parts/ <p style="color: grey"> Python recipe 579025 by <a href="/recipes/users/4191634/">shavy89</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/splitting/">splitting</a>). Revision 2. </p> <p>Split file in place into two parts </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> 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> 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> Wrap a a file-like object in another that calls a user callback whenever read() is called on it. (Python) 2013-09-25T01:54:53-07:00Martin Millerhttp://code.activestate.com/recipes/users/155538/http://code.activestate.com/recipes/578674-wrap-a-a-file-like-object-in-another-that-calls-a-/ <p style="color: grey"> Python recipe 578674 by <a href="/recipes/users/155538/">Martin Miller</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>Wraps a file-like object in another, but also calls a user callback with the number of bytes read whenever its <code>read()</code> method is called. Used for tracking upload progress, for example for a progress bar in a UI application.</p> Wrap a string in a file-like object that calls a user callback whenever read() is called on the stream (Python) 2013-09-22T21:48:03-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/578669-wrap-a-string-in-a-file-like-object-that-calls-a-u/ <p style="color: grey"> Python recipe 578669 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/upload/">upload</a>). </p> <p>Wraps a string in a read-only file-like object, but also calls a user callback with the number of bytes read whenever <code>read()</code> is called on the stream. Used for tracking upload progress, for example for a progress bar in a UI application.</p> InMemoryZip class (Python) 2013-09-23T06:44:23-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578667-inmemoryzip-class/ <p style="color: grey"> Python recipe 578667 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/zip/">zip</a>). </p> <p><strong>Why implementing this?</strong></p> <ul> <li>transfering a file or a folder (including sub folders) to another machine</li> <li>therefore zipping content to one compressed buffer</li> <li>keeping the relating ZIP in memory only but ...</li> <li>being able to save or load too</li> <li>and being able to unzip again at target destination</li> </ul> Wav audio file dynamic range compressor (C) 2013-04-18T15:04:25-07:00Granning Stolinehttp://code.activestate.com/recipes/users/4186069/http://code.activestate.com/recipes/578510-wav-audio-file-dynamic-range-compressor/ <p style="color: grey"> C recipe 578510 by <a href="/recipes/users/4186069/">Granning Stoline</a> (<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/compressor/">compressor</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/range/">range</a>, <a href="/recipes/tags/wav/">wav</a>). Revision 4. </p> <p>Wav audio file dynamic range compressor</p>