Python – How to compare directories project

In this project I’ll show you how to compare directories, save the result, and process the result: all combined in one attractive GUI.

The first thing we’ll do: we have to find the right tools/methods in Python:
so I’ve googled on “python compare directories” and found this one on the official documentation:

https://docs.python.org/3/library/filecmp.html

Now let’s start! First we’ll create the project:

Create “CompareDirectories” Project

Share project on Github (optional, recommended)

Initial commit

Now we have an initial, almost empty “compareDirectories” project on Github. It will serve us as a backup as well.

We will open the project directly on Github now and we’ll add a readme to it:

Now we’re done and we can update our project in Pycharm (with pull).

Now it’s time to really start!
We will start with a “first try” copied from
https://docs.python.org/3/library/filecmp.html

from filecmp import dircmp
def print_diff_files(dcmp):
    for name in dcmp.diff_files:
        print("diff_file %s found in %s and %s" % (name, dcmp.left,
              dcmp.right))
    for sub_dcmp in dcmp.subdirs.values():
        print_diff_files(sub_dcmp)

dcmp = dircmp('dir1', 'dir2') 
print_diff_files(dcmp) 

I’ve changed the code like this:


def print_diff_files(dcmp):
common_files = sorted(dcmp.common_files)
for name in common_files:
print("common_file %s found in %s and %s" % (name, dcmp.left,
dcmp.right))
left_only_files = sorted(dcmp.left_only)
for name in left_only_files:
print("left_only_file %s found in %s" % (name, dcmp.left))
right_only_files = sorted(dcmp.right_only)
for name in right_only_files:
print("right_only_file %s found in %s " % (name, dcmp.left))
for sub_dcmp in dcmp.subdirs.values():
print_diff_files(sub_dcmp)

What is really amazing to me is the sheer amount of information you’ll get from this compare method: try to experiment with it in debug mode and you’ll see what I mean.

Start with the GUI first

We’ll build a very simple GUI to choose both directories. Afterward we’ll add an action to it.
As a base project we will use this one:
https://github.com/edleijnse/pythonGui

You’ll find the gui part in TaggerBizControlCentre.py

Now I’ve made a first button “compare from”:

self.btn_compare_from_dir = tk.Button(self.fr_buttons, text="COMPARE FROM directory",
command=compareFromClicked, highlightbackground="cyan", bg="cyan")

While defining the button you can add a command to it, in this case: “compareFromClicked”.
In the command you can define what happens after you clicked the button.

def compareFromClicked():
print("compare from clicked")
mydir = open_directory(self.filearray[0], "directory to compare from")
btn_compare_from_dir_txt_var.set(mydir)
self.filearray[0]=mydir

The programmed action is like this: the program will ask you where to find the directory and store the value in 2 variabels: the first “btn_compare_from_dir_txt_var” is displayed as a label field right from the button. the second “self.filearray” is used to save the chosen directory.

In the same style I’ve added a “compare_to” button.
And I’ve added 2 methods for saving and reading the data in a file called

compareDirectoriesconfig.txt

So, now we have some basics to start.

Python – reading Outlook mails

Github

Project https://github.com/edleijnse/mailReader

In this project you’ll find a sample how to read items from Outlook, select some of them and save attachments to a directory. The filenames of the attachments are changed: there will be a receivement date added as a prefix to the filename

import win32com.client
import os
from datetime import datetime, timedelta

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


def extract_attachments(mailbox, restrictMessage, outputDir):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Mailbox: {mailbox}')  # Press Ctrl+F8 to toggle the breakpoint.
    outlook = win32com.client.Dispatch('outlook.application')
    mapi = outlook.GetNamespace("MAPI")

    for root in mapi.Folders:
        try:
          if (mailbox in root.FolderPath):
             print ("FolderPath: " + root.FolderPath)
             for folder in root.Folders:
                 print("folder: " + folder.FolderPath)
                 messages = folder.Items
                 restrictedMessages = messages.Restrict(restrictMessage)
                 for message in list(restrictedMessages):
                    received = str(message.ReceivedTime)
                    print (message.SenderEmailAddress)
                    print (received[0:10])
                    receivedString = received[0:10] + "_"
                    for attachment in message.Attachments:
                       attachment.SaveASFile(os.path.join(outputDir, receivedString + attachment.FileName))
                       print(f"attachment {receivedString +  attachment.FileName}  saved")

        except:
            print("exception")

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    outputDir = r"f:\swissedu_attachments"
    extract_attachments('ed@leijnse.info', "[SenderEmailAddress] = 'helena.dimi@windowslive.com'", outputDir)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

Python – read Word documents

Project: https://github.com/edleijnse/wordDocumentReader

In this Python script you’ll find a sample how to traverse a directory with subdirectories, select Word document, extract the texts and make a new document file. At the end 2 summaries will be added: one with all the words and frequencies and another with the frequencies and words.

Sample “frequency / word”

37 we
38 about
38 from
38 this
39 B
39 C
39 D
39 YOUR
39 make
39 very
40 been
40 do
41 use
43 can
43 so
49 He
49 an
50 money

# This is a sample Python script.
from docx2python import docx2python
from collections import Counter

import os
import pandas as pd

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

def extract_words(filename, outputFile):
    print("---------------------------------------------------------------------------")
    outputFile.write("\n------------------------------------------------------------------------------------------------\n")
    print(filename)
    outputFile.write(filename+"\n")
    print("---------------------------------------------------------------------------")
    outputFile.write("------------------------------------------------------------------------------------------------\n")
    doc_result = docx2python(filename).text
    print(doc_result)
    outputFile.writelines(doc_result)


    # extract words in a table
    res = doc_result.split()
    return res
def print_tallied(sorted_tallied, outputFile, title):
    #print(sorted_tallied)
    outputFile.write(
        "\n----------------------------------------------------------------------------------------------\n")
    outputFile.write(title)
    outputFile.write(
        "\n----------------------------------------------------------------------------------------------\n")
    ii = 0
    for item in sorted_tallied:
        if (item[1]>0):
           if (item[0].isalpha()):
              print(str(item[0]) + ":" + str(item[1]))
              outputFile.write(str(item[0]) + ":" + str(item[1]) + "\n")
              ii = ii + 1
    print("total: " + str(ii))

    outputFile.write("total: " + str(ii))

def sort_panda(tallied, outputFile):
    outputFile.write(
        "\n----------------------------------------------------------------------------------------------\n")
    outputFile.write("summary words by frequency")
    outputFile.write(
        "\n----------------------------------------------------------------------------------------------\n")

    ii = 0
    wordTab = []
    countTab = []

    for item in tallied:
        if (item[1] > 0):
            if (item[0].isalpha()):
                wordTab.append(item[0])
                countTab.append(item[1])
                print(str(item[0]) + ":" + str(item[1]))
                ii = ii + 1
    data = {'Count': countTab, 'Word': wordTab}
    df = pd.DataFrame(data, columns=['Count', 'Word'])
    df.sort_values(by=['Count','Word'], inplace=True)
    for oneValue in df.values:
        print(str(oneValue[0]) + " " + oneValue[1])
        outputFile.write(str(oneValue[0]) + " " + oneValue[1] + "\n")

def word_document_reader(inputDir, outputFile):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {inputDir}')  # Press Ctrl+F8 to toggle the breakpoint.
    import os

    total_res = []
    for path, currentDirectory, files in os.walk(inputDir):
        for file in files:
            filename = os.path.join(path, file)
            if ("doc" in filename):
                total_res = total_res + extract_words(filename, outputFile)
    tallied = Counter(total_res)
    sorted_tallied = sorted(tallied.items())
    print_tallied(sorted_tallied, outputFile, "summary of words")
    sort_panda(tallied.items(),outputFile)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    inputDir = r"f:\swissedu_attachments"
    outputFile = open(r"f:\swissedu.txt", "w", encoding="utf-8")


    word_document_reader(inputDir, outputFile)

    outputFile.close()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

TI-84 Python development sample

Preparation (optional)


Get sample code from GITHUB here

Open Pycharm (or another editor) and create a project from the GITHUB link
Tip: close all projects first

There are 2 files for the mastermind project: MASTR002.PY and test\MASTR002.py
The one in test\ is used for unit tests

Install the emulator software, see howto here

Deploying a .py file on the TI-emulator

Start the TI-Smartview emulator

Get the contents

Now you can copy the file MASTR002.PY to the emulator with drag and drop:

You’ll get the following dialog box:

Now the MASTR002.PY is listed and ready to run

Now you can run MASTR002 on the TI-emulator

To run the program, click on Y=

Now you can play the game as long as you want!

After ending the game you can click 2nd quit

and then GRAPH (red below)

TI-84 Python programming – Introduction – simple Euro Calculator

TI-84 Python edition – hybrid programming

The Texas Instruments TI-84 Plus CE-T calculator has a special edition called “Python Edition”
It’s easy to develop Python programs for this system. 
I’m developing like this:
I develop the programs like normal python programs on a PC with an editor (in my case PyCharm and Notepad++).
To be able to test the program on a PC (multi-platform, hybrid) I’ve made the following design.

1. conditional support for imports from TI libraries, like this:
​try:
    from ti_system import *
except Exception as e:
     print (“no module ti_system”)

2. read and write data wrapped in functions with a hybrid implementation
For reading data TI uses recall_list
For writing data TI uses store_list
On a “normal” system without the ti_system library this will not work.
So, I made wrapper functions with exception handling, e.g.

def saveeuro(ieuro):
    xlist=[]
    xlist.append(ieuro)
  try:
      store_list(“1”,xlist)
  except NameError as e:
      saveEuroToFile(ieuro)
  return

See github for the sourcecode (or see EUROCHF.py below):
github.com/edleijnse/ti84python

EUROCHF.py
sample program to convert currencies (in sample from CHF TO EURO)
The EURO currency is stored and can be changed anytime.
On the TI-84 you can read (or set) the currency with STAT and L1

try:
  from ti_system import *
except Exception as e:
    print ("no module ti_system")
def euro2chf(initeuro):
  print("")
  try:
     neweuro = float(input("new euro value? (" + '{:.2f}'.format(initeuro) + "): "))
  except Exception as e:
     neweuro=initeuro

  euro= neweuro

  print("euro: " + '{:.2f}'.format(euro))
  print("")
  eurocount=float(input("amount euros "))
  print("amount euros: ", eurocount)
  chf=euro*eurocount
  print ("costs in CHF: " + '{:.2f}'.format(chf))
  return neweuro

def getEuroFromFile():
    try:
       file1 = open("eurostore.txt","r")
       storedeuro = file1.read()
       storedeuro = int(storedeuro)
       file1.close()
    except Exception as e:
       storedeuro = .95
    return storedeuro
def geteuro():
    try:
        xlist=recall_list("1")
        storedeuro = float(xlist[0])
    except NameError as e:
        storedeuro=getEuroFromFile()
    except Exception as e:
        storedeuro = .83
    saveeuro(storedeuro)
    return storedeuro
def saveEuroToFile(iEuro):
    file1 = open("eurostore.txt","w")
    file1.write(str(iEuro))
    file1.close()
def saveeuro(ieuro):
    xlist=[]
    xlist.append(ieuro)
    try:
       store_list("1",xlist)
    except NameError as e:
       saveEuroToFile(ieuro)
    return

initeuro=geteuro()
while True:
  try:
    stopnow = input("stop program (1=y)? ")
    if stopnow == "1":
       break
    initeuro = euro2chf(initeuro)
    saveeuro(initeuro)
  except Exception as e:
    print(e)

LINKS TI-PYTHON articles

https://www.tomshardware.com/news/circuitpython-powers-ti-84-calculator