Popular Python recipes tagged "meta:requires=os"http://code.activestate.com/recipes/langs/python/tags/meta:requires=os/2017-07-15T00:46:59-07:00ActiveState Code RecipesUno (Text-Based) (Python)
2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/
<p style="color: grey">
Python
recipe 580811
by <a href="/recipes/users/4194238/">Brandon Martin</a>
(<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>).
</p>
<p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </p>
Show OS error codes and messages from the os.errno module (Python)
2017-03-01T17:18:23-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580759-show-os-error-codes-and-messages-from-the-oserrno-/
<p style="color: grey">
Python
recipe 580759
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/libraries/">libraries</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe is a simple Python introspection utility that displays the defined OS error codes and messages (that Python knows about) from the os.errno module. It works for both Python 2 and Python 3. For each kind of OS error defined in Python, it will display a serial number, the error code, and the corresponding error name, and English error message. E.g. the first few lines of its output are shown below:</p>
<p>$ py -2 os_errno_info.py</p>
<p>Showing error codes and names</p>
<p>from the os.errno module:</p>
<p>Python sys.version: 2.7.12</p>
<p>Number of error codes: 86</p>
<p>Idx Code Name Message</p>
<p>0 1 EPERM Operation not permitted</p>
<p>1 2 ENOENT No such file or directory</p>
<p>2 3 ESRCH No such process</p>
<p>3 4 EINTR Interrupted function call</p>
<p>4 5 EIO Input/output error</p>
<p>More information, full output and other details are available here:</p>
<p><a href="https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/show-error-numbers-and-codes-from.html</a></p>
ctypes CDLL with automatic errno checking (Python)
2017-01-03T10:31:26-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/580741-ctypes-cdll-with-automatic-errno-checking/
<p style="color: grey">
Python
recipe 580741
by <a href="/recipes/users/2033964/">Oren Tirosh</a>
.
</p>
<p>This class extends ctypes.CDLL with automatic checking of errno and automatically raises an exception when set by the function.</p>
A binary file split utility in Python (Python)
2016-04-07T18:19:35-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580637-a-binary-file-split-utility-in-python/
<p style="color: grey">
Python
recipe 580637
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/commandline/">commandline</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/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple binary file split utility in Python.</p>
<p>It takes two command line arguments: 1) the name of the input file to split , 2) the number of bytes per file into which to split the input.</p>
Unix-like split command in Python (simple version) (Python)
2016-03-14T22:35:05-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580620-unix-like-split-command-in-python-simple-version/
<p style="color: grey">
Python
recipe 580620
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/splitting/">splitting</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple version of the Unix split command in Python. The split command splits an input file into multiple smaller files, the size of each of which is specified by a command-line argument giving the number of lines per file. This is useful for multiple purposes, such as editing large files in pieces, backing up files to small capacity storage devices, sending files across the network, etc.</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>
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>
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>
FileSpec: Set it, forget it, reuse it (Python)
2016-03-08T05:49:16-08:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/580618-filespec-set-it-forget-it-reuse-it/
<p style="color: grey">
Python
recipe 580618
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/filename/">filename</a>, <a href="/recipes/tags/utilities/">utilities</a>).
</p>
<p>Python provides good utilities for transforming filenames, but they are tedious to use and clutter up the source code.</p>
<p>FileSpec offers one-stop shopping to convert a file path to every component you might want to know, reuse, or transform.</p>
Persistent Queue (Python)
2015-11-12T06:45:12-08:00zhangkaizhaohttp://code.activestate.com/recipes/users/4189594/http://code.activestate.com/recipes/579124-persistent-queue/
<p style="color: grey">
Python
recipe 579124
by <a href="/recipes/users/4189594/">zhangkaizhao</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>A class for persistent queues.</p>
vlc.py stream capture scheduler script (Python)
2015-08-19T05:00:25-07:00jwhite88http://code.activestate.com/recipes/users/4192711/http://code.activestate.com/recipes/579096-vlcpy-stream-capture-scheduler-script/
<p style="color: grey">
Python
recipe 579096
by <a href="/recipes/users/4192711/">jwhite88</a>
(<a href="/recipes/tags/recording/">recording</a>, <a href="/recipes/tags/stream/">stream</a>, <a href="/recipes/tags/vlc/">vlc</a>).
</p>
<p>Capture network streams using vlc.py on a schedule.</p>
Examples for random float between 0 and 1 (Python)
2015-06-25T17:55:33-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/579072-examples-for-random-float-between-0-and-1/
<p style="color: grey">
Python
recipe 579072
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/example/">example</a>, <a href="/recipes/tags/random/">random</a>).
</p>
<p>I was researching how floats are stored in a computer and was trying to think up a way to generate random values between 0 and 1. Python already provides an implementation allowing this already, and two of the functions below are directly inspired by that code, but the third is a slightly different way of doing the same thing. A similar version of this code has been used to implement similar functionality in C# at one time. Others might also find it useful if they want to create equivalent code in a separate language while having access to random bytes but not random floats. It should be noted that the various implementations get slower as you go down the list.</p>
Simple audio with ctypes and SDL also for Tkinter (Python)
2015-08-17T18:54:17-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579070-simple-audio-with-ctypes-and-sdl-also-for-tkinter/
<p style="color: grey">
Python
recipe 579070
by <a href="/recipes/users/4192188/">Jiri Justra</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/sdl/">sdl</a>, <a href="/recipes/tags/sdl_mixer/">sdl_mixer</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
Revision 3.
</p>
<p>I've needed just to play audio in my Tkinter application, but it seems there is no easy way to do this, so I have made this simple code. It is small ctypes wrapper around SDL_mixer.</p>
<p>It should work for Win and *nix also .. I've tested it on ubuntu-14.04.3-desktop-i386</p>
Tail multiple pidgin IRC logfiles (Python)
2015-06-06T12:13:00-07:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/579066-tail-multiple-pidgin-irc-logfiles/
<p style="color: grey">
Python
recipe 579066
by <a href="/recipes/users/2667360/">Anton Vredegoor</a>
(<a href="/recipes/tags/colorize/">colorize</a>, <a href="/recipes/tags/irc/">irc</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/logfiles/">logfiles</a>, <a href="/recipes/tags/merging/">merging</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pidgin/">pidgin</a>, <a href="/recipes/tags/tail/">tail</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>Tail multiple pidgin IRC logfiles. </p>
<p>Pidgin should be connected to IRC with the channels one
wants to tail joined, and it should save logs as text.</p>
<p>The script needs two arguments:</p>
<pre class="prettyprint"><code>the directory containing the directories with channel logs
a list of channel names, quoted and separated by spaces
</code></pre>
<p>Example command:</p>
<p>python pidgin-irctail.py
-d <a href="mailto:~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net">~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net</a>
-c "#chan1 #chan2 #chan3"</p>
<p>Some text elements are higlighted, and channel names are
inserted into the log lines after the time info.</p>
<p>If more than one channel is entered, the output of the logs
is merged. </p>
A UNIX-like "which" command for Python (Python)
2015-03-20T19:23:45-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579035-a-unix-like-which-command-for-python/
<p style="color: grey">
Python
recipe 579035
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/commands/">commands</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/which/">which</a>).
</p>
<p>UNIX users are familiar with the which command. Given an argument called name, it checks the system PATH environment variable, to see whether that name exists (as a file) in any of the directories specified in the PATH. (The directories in the PATH are colon-separated on UNIX and semicolon-separated on Windows.)</p>
<p>This recipe shows how to write a minimal which command in Python.
It has been tested on Windows.</p>
Convert doc and docx files to pdf (Python)
2014-03-31T18:39:16-07:00Fabian Mayerhttp://code.activestate.com/recipes/users/4189629/http://code.activestate.com/recipes/578858-convert-doc-and-docx-files-to-pdf/
<p style="color: grey">
Python
recipe 578858
by <a href="/recipes/users/4189629/">Fabian Mayer</a>
(<a href="/recipes/tags/doc/">doc</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/win32com/">win32com</a>).
Revision 2.
</p>
<p>The Script converts all doc and docx files in a specified folder to pdf files. It checks whether the provided absolute path does actually exist and whether the specified folder contains any doc and docx files. It does not travers the directory recursively. The script is not portable and runs only a Windows machine. Based on the experience I made, I recommend closing MS Word before running the script.</p>
Robust Testing of Tkinter Menu Items with Mocking. (Python)
2017-06-13T18:44:58-07:00Stephen Rigdenhttp://code.activestate.com/recipes/users/4191139/http://code.activestate.com/recipes/578964-robust-testing-of-tkinter-menu-items-with-mocking/
<p style="color: grey">
Python
recipe 578964
by <a href="/recipes/users/4191139/">Stephen Rigden</a>
(<a href="/recipes/tags/invoke/">invoke</a>, <a href="/recipes/tags/mainloop/">mainloop</a>, <a href="/recipes/tags/mock/">mock</a>, <a href="/recipes/tags/mocking/">mocking</a>, <a href="/recipes/tags/tk/">tk</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/unittest/">unittest</a>, <a href="/recipes/tags/unittesting/">unittesting</a>, <a href="/recipes/tags/unittests/">unittests</a>).
Revision 7.
</p>
<p>This recipe addresses the problems encountered when building robust tests for Tk menus.
The software under test is a simple window with two menu items that each invoke a one button dialog box. All user visible text is imported from an external config.ini file.
This scenario can lead to fragile test code because of the way TK's invoke(index) command has been implemented. (Tcl8.6.3/Tk8.6.3.)</p>
Python: Determine Name and Directory of the Top Level Running Script (Python)
2015-02-03T01:37:09-08:00Harald Liederhttp://code.activestate.com/recipes/users/4191581/http://code.activestate.com/recipes/579018-python-determine-name-and-directory-of-the-top-lev/
<p style="color: grey">
Python
recipe 579018
by <a href="/recipes/users/4191581/">Harald Lieder</a>
.
</p>
<p>Yet another way to provide this information.
I wanted to get the right answers in ANY situation: Python 2 or 3, compiled or interpreted (interactive or batch).
The following code has been tested under Windows, Python 2.7, Python 3.4, IDLE, IPython (sh and Qt), PyInstaller, cx_Freeze, py2exe.</p>
A Basic USe flag EDitor for Gentoo Linux supporting on-the-fly editing (Python)
2015-02-28T07:04:31-08:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/579028-a-basic-use-flag-editor-for-gentoo-linux-supportin/
<p style="color: grey">
Python
recipe 579028
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/popen/">popen</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/user_input/">user_input</a>).
</p>
<p>This allows for on-the-fly editing. Simply drop abused.py into your path, and ensure that -a is not set in EMERGE_DEFAULT_OPTS in /etc/portage/make.conf. Then whenver you are installing new packages, use abused in place of emerge (eg: abused multitail) you will be presented with a list of use flags that are used in this action, and a prompt for editing any of them, simply hit enter with no changes to fire off the build.</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>