Skip to content

Instantly share code, notes, and snippets.

@Haolicopter
Haolicopter / regexSearch.py
Last active May 6, 2017 20:50
A program that opens all .txt files in a folder and searches for any line that matches a user-supplied regular expression. The results should be printed to the screen.
#!/usr/bin/env python3
# A program that opens all .txt files in a folder
# and searches for any line that matches a user-supplied regular expression.
# The results should be printed to the screen.
import glob, re
destFolder = 'randomFolder'
userRegex = input('Enter your regular expression:\n')
@Haolicopter
Haolicopter / selectiveCopy.py
Last active March 9, 2023 18:09
Write a program that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder.
#!/usr/bin/env python3
# Write a program that walks through a folder tree
# and searches for files with a certain file extension (such as .pdf or .jpg).
# Copy these files from whatever location they are in to a new folder.
import os, shutil
def selectiveCopy(folder, extensions, destFolder):
folder = os.path.abspath(folder)
@Haolicopter
Haolicopter / huntGaints.py
Created May 6, 2017 21:11
Write a program that walks through a folder tree and searches for exceptionally large files or folders. Print these files with their absolute path to the screen.
#!/usr/bin/env python3
# Write a program that walks through a folder tree and searches for exceptionally large files or folders.
# Print these files with their absolute path to the screen.
import os
# Default size for gaint is 100MB
def huntGaints(folder, size = 1024000):
folder = os.path.abspath(folder)
@Haolicopter
Haolicopter / fillInGapsWithNewFiles.py
Created May 7, 2017 02:35
Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so no, in a single folder and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt). Have the program that can insert gaps into numbered files so that a new file can be added.
#!/usr/bin/env python3
# Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so no, in a single folder
# and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt).
# Have the program that can insert gaps into numbered files so that a new file can be added.
import os
def fillInGapsWithNewFiles(folder, prefix):
folder = os.path.abspath(folder)
@Haolicopter
Haolicopter / fillInGaps.py
Created May 7, 2017 03:29
Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so no, in a single folder and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt). Have the program rename all the later files to close this gap.
#!/usr/bin/env python3
# Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so no, in a single folder
# and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt).
# Have the program rename all the later files to close this gap.
import os, shutil
def fillInGaps(folder, prefix):
folder = os.path.abspath(folder)
@Haolicopter
Haolicopter / inPlaceShuffle.py
Created May 11, 2017 15:34
A small program that does in place shuffle
#!/usr/bin/env python3
# The shuffle must be "uniform," meaning each item in the original list must have the same probability of ending up in each spot in the final list.
# Assume that you have a function get_random(floor, ceiling) for getting a random integer that is >= floor and <= ceiling.
import random
myList = [12, 7, 9, 10, 4]
for i in range(len(myList)-1, 0, -1):
@Haolicopter
Haolicopter / multiplicationTable.py
Created May 15, 2017 03:58
This program takes a number N from the command line and creates an N by N multiplication table in an Excel spreadsheet.
#!/usr/bin/env python3
# This program takes a number N from the command line
# and creates an N by N multiplication table in an Excel spreadsheet.
import sys, openpyxl
from openpyxl.styles import Font
from openpyxl.utils import get_column_letter, column_index_from_string
# Default n is 6
@Haolicopter
Haolicopter / blankRowInserter.py
Created May 16, 2017 04:29
This program takes two integers and a filename string as command line arguments. Let's call the first integer N and the second integer M. Starting at row N, the program should insert M blank rows into the spreadsheet.
#!/usr/bin/env python3
# This program takes two integers and a filename string as command line arguments.
# Let's call the first integer N and the second integer M.
# Starting at row N, the program should insert M blank rows into the spreadsheet.
import sys, openpyxl
from openpyxl.utils import get_column_letter
if len(sys.argv) > 1:
@Haolicopter
Haolicopter / spreadsheetCellInverter.py
Created May 16, 2017 04:35
This program inverts the row and column of the cells in the spreadsheet.
#!/usr/bin/env python3
# This program inverts the row and column of the cells in the spreadsheet.
import sys, openpyxl
from openpyxl.utils import get_column_letter
fileName = 'somefile.xlsx'
wb = openpyxl.load_workbook(fileName)
sheet = wb.active
@Haolicopter
Haolicopter / textFilesToSpreadsheet.py
Created May 16, 2017 04:59
This program reads in the contents of several text files and inserts those contents into a spreadsheet, with one line of text per row. The lines of the first text file will be in the cells of column A. The lines of the second text file will be in the cells of column B, and so on.
#!/usr/bin/env python3
# This program reads in the contents of several text files and inserts those contents into a spreadsheet, with one line of text per row.
# The lines of the first text file will be in the cells of column A.
# The lines of the second text file will be in the cells of column B, and so on.
import openpyxl
from openpyxl.utils import get_column_letter
# Create a new spreadsheet