Python’s rich library – a tutorial

Python’s rich library – a tutorial

Python, System Administration
The Python rich library is a package for having clearer, styled, and colored output in the terminal. rich works across multiple operating systems - including Windows, Linux, and macOS. In this post, we'll give an introduction to what it can do for you. You can get started with rich by installing it with pip. [code] pip install rich [/code] Once you have it installed, open up the command line and type in python. In order to get the additional functionality from rich, you'll need to do one more step, which you can see below. Running this snippet will allow you to have styled / formatted code interactively. You'll only need to do this once. [code lang="python"] from rich import pretty pretty.install() [/code] Here's a couple examples of automatic coloring for…
Read More
How to schedule a Python script on a Mac

How to schedule a Python script on a Mac

Python, System Administration
In a previous article, we talked about how to run Python from the Windows Task Scheduler. This post will cover how to schedule Python tasks on a Mac operating system as well as give an overview of the schedule package. Using crontab on Mac Python tasks can be scheduled on Mac using crontab. To do that, first, open up the Terminal. Then, we need to modify the crontab file. We can do that by typing crontab -e. This will open crontab in the default editor, which is typically vim. You can change the editor by adding the editor name in front of our command - for example, to modify the crontab file using nano, we can run nano crontab -e (followed by enter). Next, we need to add in a…
Read More
How to schedule R scripts

How to schedule R scripts

R, System Administration
Running R with taskscheduleR and cronR In a previous post, we talked about how to run R from the Windows Task Scheduler. This article will talk about two additional approaches to schedule R scripts, including using the taskscheduleR package on Windows and the cronR package for Linux. For scheduling Python code, check out this post. Schedule R scripts with taskscheduleR Let's install taskscheduleR using the install.packages command. [code lang="R"] install.packages("taskscheduleR") [/code] Next, we just need to load the package to get started. [code lang="R"] library(taskscheduleR) [/code] Creating a sample R script to run automatically Before we do any scheduling, we need to first create a script. We'll save the code below in a file called "create_file.txt". This script will randomly generate a collection of integers and write them out to…
Read More
How to run R from the Task Scheduler

How to run R from the Task Scheduler

R, System Administration
In a prior post, we covered how to run Python from the Task Scheduler on Windows. This article is similar, but it'll show how to run R from the Task Scheduler, instead. Similar to before, let's first cover how to R from the command line, as knowing this is useful for running it from the Task Scheduler. Running R from the Command Line To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line. If Rscript is in your PATH, then typing Rscript into the command line, and pressing enter, will not result in an error. Otherwise, you might get a message saying "'Rscript'…
Read More
R: How to create, delete, move, and more with files

R: How to create, delete, move, and more with files

File Manipulation, R, System Administration
Though Python is usually thought of over R for doing system administration tasks, R is actually quite useful in this regard. In this post we're going to talk about using R to create, delete, move, and obtain information on files. How to get and change the current working directory Before working with files, it's usually a good idea to first know what directory you're working in. The working directory is the folder that any files you create or refer to without explicitly spelling out the full path fall within. In R, you can figure this out with the getwd function. To change this directory, you can use the aptly named setwd function. [code lang="R"] # get current working directory getwd() # set working directory setwd("C:/Users") [/code] Creating Files and Directories…
Read More
Running Python from the Task Scheduler

Running Python from the Task Scheduler

Python, System Administration
Background Running Python from the Windows Task Scheduler is a really useful capability. It allows you to run Python in production on a Windows system, and can save countless hours of work. For instance, running code like extracting data from a database on an automated, regular basis is a common need at many companies. How to run Python from the command line Before we go into how to schedule a Python script to run, you need to understand how to run Python from the command line. To open the command prompt (command line), press the windows key and type cmd into the search box. Next, suppose your python script is called cool_python_script.py, and is saved under C:\Users. You can run this script from the command prompt by typing the below…
Read More
File Manipulation with Python

File Manipulation with Python

File Manipulation, Python, System Administration
Getting started Python is great for automating file creation, deletion, and other types of file manipulations.  Two of the primary packages used to perform these types of tasks are os and shutil.  We'll be covering a few useful highlights from each of these. [code lang="python"] import os import shutil [/code] How to get and change your current working directory You can get your current working directory using os.getcwd: [code lang="python"] os.getcwd() [/code] Any actions you take without specifying a directory will be assumed to be associated with your current working directory i.e. if you create or search for a file without specifying a directory, Python will assume you're in the value of os.getcwd(). To change your working directory, use os.chdir: [code lang="python"] os.chdir("C:/path/to/new/directory") [/code] How to merge a directory name…
Read More