Popular recipes tagged "files" but not "globbing"http://code.activestate.com/recipes/tags/files-globbing/2016-12-17T19:08:33-08:00ActiveState Code Recipes[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> 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> Read CSV with D and write it to PDF with Python (Python) 2016-10-26T17:49:00-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580710-read-csv-with-d-and-write-it-to-pdf-with-python/ <p style="color: grey"> Python recipe 580710 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/conversion/">conversion</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/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to read data from a CSV file with a D program and write that data to a PDF file with a Python program - all in a single command-line invocation (after writing the individual programs, of course).</p> <p>It requires the xtopdf toolkit, which you can get from:</p> <p><a href="https://bitbucket.org/vasudevram/xtopdf" rel="nofollow">https://bitbucket.org/vasudevram/xtopdf</a></p> <p>Instructions for installing 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>xtopdf in turn requires the open source version of the ReportLab toolkit, which you can get from:</p> <p><a href="http://www.reportlab.com/ftp" rel="nofollow">http://www.reportlab.com/ftp</a> (<a href="http://www.reportlab.com/ftp/reportlab-1.21.1.tar.gz%29" rel="nofollow">http://www.reportlab.com/ftp/reportlab-1.21.1.tar.gz)</a></p> <p>It also requires the DMD compiler to compile the D program - this was the version used:</p> <p>DMD32 D Compiler v2.071.2</p> Recursive find replace in files using regex (Python) 2016-04-28T13:24:15-07:00ccpizzahttp://code.activestate.com/recipes/users/4170754/http://code.activestate.com/recipes/580653-recursive-find-replace-in-files-using-regex/ <p style="color: grey"> Python recipe 580653 by <a href="/recipes/users/4170754/">ccpizza</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/replace/">replace</a>, <a href="/recipes/tags/search/">search</a>). Revision 5. </p> <h4 id="recursively-find-and-replace-text-in-files-under-a-specific-folder-with-preview-of-changed-data-in-dry-run-mode">Recursively find and replace text in files under a specific folder with preview of changed data in dry-run mode</h4> <h5 id="example-usage">Example Usage</h5> <p><strong>See what is going to change (dry run):</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --dryrun </code></pre> <p><strong>Do actual replacement:</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" </code></pre> <p><strong>Do actual replacement and create backup files:</strong></p> <pre class="prettyprint"><code>find_replace.py --dir project/myfolder --search-regex "\d{4}-\d{2}-\d{2}" --replace-regex "2012-12-12" --create-backup </code></pre> <p><strong>Same action as previous command with short-hand syntax:</strong></p> <pre class="prettyprint"><code>find_replace.py -d project/myfolder -s "\d{4}-\d{2}-\d{2}" -r "2012-12-12" -b </code></pre> <p>Output of <code>find_replace.py -h</code>:</p> <pre class="prettyprint"><code>DESCRIPTION: Find and replace recursively from the given folder using regular expressions optional arguments: -h, --help show this help message and exit --dir DIR, -d DIR folder to search in; by default current folder --search-regex SEARCH_REGEX, -s SEARCH_REGEX search regex --replace-regex REPLACE_REGEX, -r REPLACE_REGEX replacement regex --glob GLOB, -g GLOB glob pattern, i.e. *.html --dryrun, -dr don't replace anything just show what is going to be done --create-backup, -b Create backup files USAGE: find_replace.py -d [my_folder] -s &lt;search_regex&gt; -r &lt;replace_regex&gt; -g [glob_pattern] </code></pre> 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> Catalog multiple drives (Python) 2016-03-11T03:39:32-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580619-catalog-multiple-drives/ <p style="color: grey"> Python recipe 580619 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/drives/">drives</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>As one accumulates multiple drives, hard and flash, containing, thousands even millions of files, it becomes useful to have a text file containing an alphabetized catalog list of all files and their locations by drive and directory.</p> <p>The list can be searched by eye or by an editor to locate particular files.</p> <p>The list can also be loaded into a script to be filtered programmatically as desired.</p> generates a set of binary files by doing ASCII replacements on a master binary file, controlled by a csv file (Python) 2015-09-16T16:25:49-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579099-generates-a-set-of-binary-files-by-doing-ascii-rep/ <p style="color: grey"> Python recipe 579099 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/replace/">replace</a>). </p> <p>A hack that saved me a lot of time. Well perhaps not so much, but it was funnier than to do it manually. </p> CSV to Flat File Converter (Python) 2015-08-02T17:31:59-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/579091-csv-to-flat-file-converter/ <p style="color: grey"> Python recipe 579091 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/csv/">csv</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Converts a CSV file to a flat file (table).</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> 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> search file extensions in directory (Python) 2014-04-17T22:33:39-07:00Keisuke URAGOhttp://code.activestate.com/recipes/users/668964/http://code.activestate.com/recipes/578862-search-file-extensions-in-directory/ <p style="color: grey"> Python recipe 578862 by <a href="/recipes/users/668964/">Keisuke URAGO</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/finder/">finder</a>). </p> <p>This code is find and print file extensions from directory.</p> Reimplementation of rmtree supporting Windows reparse points (Python) 2014-03-08T21:17:41-08:00Charles Grunwaldhttp://code.activestate.com/recipes/users/4175823/http://code.activestate.com/recipes/578849-reimplementation-of-rmtree-supporting-windows-repa/ <p style="color: grey"> Python recipe 578849 by <a href="/recipes/users/4175823/">Charles Grunwald</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/remove/">remove</a>, <a href="/recipes/tags/win32/">win32</a>). Revision 3. </p> <p>Ctypes-based implementation of shutil.rmtree that correctly handles Windows reparse point folders. (symbolic links, junctions, etc)</p> lndir.py (short python version of the BSD/X11 lndir utility) (Python) 2013-05-28T07:21:28-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578535-lndirpy-short-python-version-of-the-bsdx11-lndir-u/ <p style="color: grey"> Python recipe 578535 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/os_walk/">os_walk</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/walk/">walk</a>). Revision 2. </p> <p>This should be valid for Python 2.6 and up, including the 3.x series.</p> Progress bar class (Python) 2012-08-09T17:39:10-07:00Xavier L.http://code.activestate.com/recipes/users/4171602/http://code.activestate.com/recipes/578228-progress-bar-class/ <p style="color: grey"> Python recipe 578228 by <a href="/recipes/users/4171602/">Xavier L.</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/module/">module</a>, <a href="/recipes/tags/progress/">progress</a>). Revision 5. </p> <p>See <a href="https://gist.github.com/3306295">gist:3306295</a> for future developments.</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, spaces to represent incomplete portions, '>' to represent the current portion and the actual percent done (rounded to integer) displayed at the end:</p> <p>[===========&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;] 60%</p> <p>When you initialize the class, you specify the minimum number (defaults to 0), the maximum number (defaults to 100), and the desired width of the progress bar. The brackets <code>[]</code> are included in the size of the progress bar, but you must allow for up to 4 characters extra to display the percentage.</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> Move files with rename if required (Python) 2012-04-30T11:09:05-07:00John Reidhttp://code.activestate.com/recipes/users/4023487/http://code.activestate.com/recipes/578116-move-files-with-rename-if-required/ <p style="color: grey"> Python recipe 578116 by <a href="/recipes/users/4023487/">John Reid</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/move/">move</a>, <a href="/recipes/tags/rename/">rename</a>). Revision 2. </p> <p>A python script that renames (moves) files to a destination directory. However it will not overwrite existing files. The script uses a renaming strategy where a count is incremented in order to avoid naming conflicts.</p> <p>Example usage::</p> <pre class="prettyprint"><code>mv-rename a.ext b.ext target-dir/ </code></pre> <p>would mv a.ext and b.ext into the target directory. If::</p> <pre class="prettyprint"><code>target-dir/a.ext target-dir/b.ext </code></pre> <p>already exist, the newly moved files would be named::</p> <pre class="prettyprint"><code>target-dir/a-&lt;N&gt;.ext target-dir/b-&lt;M&gt;.ext </code></pre> <p>where &lt;N&gt; and &lt;M&gt; are the lowest numbers such that there is no conflict.</p> Encrypt and Decrypt Text and Text Files (BETA) Jython GUI (Python) 2011-11-23T05:37:04-08:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577957-encrypt-and-decrypt-text-and-text-files-beta-jytho/ <p style="color: grey"> Python recipe 577957 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/cryptography/">cryptography</a>, <a href="/recipes/tags/cryptology/">cryptology</a>, <a href="/recipes/tags/decryption/">decryption</a>, <a href="/recipes/tags/encryption/">encryption</a>, <a href="/recipes/tags/encryption_decryption/">encryption_decryption</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/hiding/">hiding</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is a Jython GUI of my previous recipe: Encrypt and Decrypt Text and Text Files</p> Python reader for spss sav files (Python) 2011-07-25T18:59:49-07:00Albert-Jan Roskamhttp://code.activestate.com/recipes/users/4177640/http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/ <p style="color: grey"> Python recipe 577650 by <a href="/recipes/users/4177640/">Albert-Jan Roskam</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/pasw/">pasw</a>, <a href="/recipes/tags/sav/">sav</a>, <a href="/recipes/tags/spss/">spss</a>, <a href="/recipes/tags/statistics/">statistics</a>). Revision 3. </p> <p><strong>Python Program to read Spss system files (.sav)</strong></p> <ul> <li><em>* version 2 (FASTER!) *</em></li> <li>Requires spssio32.dll, which can be freely downloaded from: <a href="https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%2520for%2520IBM%25C2%25AE%2520SPSS%25C2%25AE%2520Statistics?lang=en" rel="nofollow">https://www.ibm.com/developerworks/mydeveloperworks/wikis/home/wiki/We70df3195ec8_4f95_9773_42e448fa9029/page/Downloads%20for%20IBM%C2%AE%20SPSS%C2%AE%20Statistics?lang=en</a></li> <li>You can also find this dll in the installation directory of SPSS (although SPSS is _not_ needed!)</li> <li><p>The .dll should be saved in the same location as this program.</p></li> <li><p>Parameters: <em>savFileName</em>: the file name of the spss data file; <em>returnHeader</em>: Boolean that indicates whether the first record should be a list of variable names (default: True); <em>recodeSysmisTo</em>: indicates to which value missing values should be recoded (default: ""); <em>selectVars</em>: indicates which variables in the file should be selected.The variables should be specified as a list or a tuple of valid variable names. If None is specified, all variables in the file are used (default: None); <em>verbose</em>: Boolean that indicates whether information about the spss data file (e.g., number of cases, variable names, file size) should be printed on the screen (default: True). <br /> <em>rawMode</em>: Boolean that indicates whether values should get SPSS-style formatting,and whether date variables (if present) should be converted to ISO-dates. If True, the program does not format any values, which increases processing speed. (default: = False) <em>interfaceEncoding</em> Indicates the mode in which text communicated to or from the I/O Module will be. Valid values are 'UTF-8' or 'CODEPAGE' (default = 'CODEPAGE')</p></li> <li><p>Typical use: savFileName = "d:/someFile.sav" with SavReader(savFileName) as sav: header = sav.next() for line in sav: process(line)</p></li> <li><p>Note: --<em>New version</em>: If you downloaded this previously, use the current version as it is <strong>MUCH faster!!</strong> --this code currently only works on Windows (32 bits). I might make it work on Linux Ubuntu 10 at some point. --date fields in spss are represented as the number of seconds since the Gregorian calendar. The program converts these, wherever possible, to ISO-dates (yyyy-mm-dd).</p></li> </ul> <p>Any feedback is welcome! I'm still learning!</p> A Komodo macro for duplicating the current file in Komodo 6 (JavaScript) 2011-02-03T23:47:41-08:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577562-a-komodo-macro-for-duplicating-the-current-file-in/ <p style="color: grey"> JavaScript recipe 577562 by <a href="/recipes/users/4166930/">Eric Promislow</a> (<a href="/recipes/tags/copy/">copy</a>, <a href="/recipes/tags/files/">files</a>). </p> <p>The project/file API changed significantly moving from Komodo 5 to 6. Specifically, the project manager and file manager have been split into two separate modules.</p> <p>Here's some code to duplicate the current file, using Komodo 6. Note that it uses an internal function - I've made a note that the function has been effectively published, and needs to preserve its current interface.</p> Recoursive File Moving (Python) 2010-09-28T19:01:24-07:00shawnhttp://code.activestate.com/recipes/users/4175099/http://code.activestate.com/recipes/577411-recoursive-file-moving/ <p style="color: grey"> Python recipe 577411 by <a href="/recipes/users/4175099/">shawn</a> (<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 3. </p> <p>This script is placed in a location then run. All files underneath the root will be propagated to the top level, while deleting any old directory's.</p> <p>Problems: If you have large files in nested folders you will be copying them multiple times.</p> <p>p.s. shoulda used walk() but i'm newb</p>