Created
August 18, 2020 11:32
-
-
Save olavurmortensen/11f7e91e3a823e2b6c6f5cc13ac4e0f0 to your computer and use it in GitHub Desktop.
Don't code in Bash, learn to use system calls in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys, os, shutil, subprocess, logging | |
# Set logging level; log everything. | |
logging.basicConfig(level=logging.INFO) | |
# Define some inputs. | |
input1 = sys.argv[1] | |
input2 = sys.argv[2] | |
# Try creating a new folder. | |
some_path = 'some_path_goes_here' | |
try: | |
os.mkdir(some_path) | |
except FileExistsError: | |
logging.warning('Working directory "%s" already exists.' % some_path) | |
# Navigate to the new folder. | |
os.chdir(some_path) | |
# We will try to make a new file. | |
# Check if this file already exists. | |
fn = 'testfile.txt' | |
if os.path.isfile(fn): | |
logging.warning('File "%s" already exists.' % fn) | |
# Run a command, producing a new file. | |
cmd = 'echo %s %s > %s' %(input1, input2, fn) | |
subprocess.call(['/bin/sh', '-c', cmd]) | |
# Copy the file. | |
shutil.copy(fn, 'testfile_copy.txt') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment