I'm trying to print diff.txt that contains the differences between the 2 log files I have. I want it to print the diff.txt in the current directory, but I get nothing.

def difference(dirList1, dirList2):

    difFile = list(set(dirList1).difference(set(dirList2)))
    writeDif = open( 'diff.txt', 'w' )
    writeDif.write( 'Yadda yadda, intro intro' )
    pprint( difFile, writeDif )
    writeDif.close()
    
difference()

dirList1 and 2 are already set via a path definition of:

path1 = "E:\Music\dirList1.txt"
dirList1 = os.path.basename(path1)

Any suggestions?

Recommended Answers

All 3 Replies

Print dirList1 and dirList2 at the beginning of the function, or at least the length of each. The first step is to make sure that both contain what you think they contain.

You're right. It prints out the file name, not what is in the .txt
I'm not really sure how to get the information out of the txt file into a list that I can use to determine the difference between them. Still searching. Any help appreciated

Generally, to find the difference of two texts you can use Python module difflib ...

# use Python module difflib to find the line by
# line differences between two texts
# modified to work with Python25 and Python30
"""
line markers ...
'- ' line unique to sequence 1 
'+ ' line unique to sequence 2 
' '  line common to both sequences 
'? ' line not present in either input sequence
     used to point out character differences in lines
     + word has extra char,  
     ^ spelling, 
     - word has missing char 
"""

import difflib

text1 = """The World's Shortest Books:
Human Rights Advances in China
"My Plan to Find the Real Killers" by OJ Simpson
"Strom Thurmond:  Intelligent Quotes"
America's Most Popular Lawyers
Career Opportunities for History Majors
Different Ways to Spell "Bob"
Dr. Kevorkian's Collection of Motivational Speeches
Spotted Owl Recipes by the EPA
The Engineer's Guide to Fashion
Ralph Nader's List of Pleasures
"""

text2 = """The World's Shortest Books:
Human Rights Advances in China
"My Plan to Find the Real Killers" by OJ Simpson
"Strom Thurmond:  Intelligent Quotes"
America's Most Poppular Lawyers
Career Opportunities for History Majors
Different Ways to Sell "Bob"
Dr. Kevorkian's Collection of Motivational Speeches
Spotted Owl Recipes by the EPA
The Engineer's Guide to Passion
Ralph Nader's List of pleasures
"""

# create a list of lines in text1
text1_lines = text1.splitlines(True)

# dito for text2
text2_lines = text2.splitlines(True)

diff_instance = difflib.Differ()
diff_list = list(diff_instance.compare(text1_lines, text2_lines))

print("Lines in diff_list showing the markers:")
for line in diff_list:
    line = line.rstrip()
    print(line)
print

# lines common to text1 and text2
common = ""
for line in diff_list:
  if line[0] == ' ':
    common += line[2:]

# lines unique to text1
unique1 = ""
for line in diff_list:
  if line[0] == '-':
    unique1 += line[2:]

# lines unique to text2
unique2 = ""
for line in diff_list:
  if line[0] == '+':
    unique2 += line[2:]

print('-'*50)

print("Lines common to text1 and text2:")
print(common)
print('-'*50)
print("Lines unique to text1:")
print(unique1)
print('-'*50)
print("Lines unique to text2:")
print(unique2)

"""
my result -->
Lines in diff_list showing the markers:
  The World's Shortest Books:
  Human Rights Advances in China
  "My Plan to Find the Real Killers" by OJ Simpson
  "Strom Thurmond:  Intelligent Quotes"
- America's Most Popular Lawyers
+ America's Most Poppular Lawyers
?                   +
  Career Opportunities for History Majors
- Different Ways to Spell "Bob"
?                    -
+ Different Ways to Sell "Bob"
  Dr. Kevorkian's Collection of Motivational Speeches
  Spotted Owl Recipes by the EPA
- The Engineer's Guide to Fashion
?                         ^  ^
+ The Engineer's Guide to Passion
?                         ^  ^
- Ralph Nader's List of Pleasures
?                       ^
+ Ralph Nader's List of pleasures
?                       ^

--------------------------------------------------
Lines common to text1 and text2:
The World's Shortest Books:
Human Rights Advances in China
"My Plan to Find the Real Killers" by OJ Simpson
"Strom Thurmond:  Intelligent Quotes"
Career Opportunities for History Majors
Dr. Kevorkian's Collection of Motivational Speeches
Spotted Owl Recipes by the EPA

--------------------------------------------------
Lines unique to text1:
America's Most Popular Lawyers
Different Ways to Spell "Bob"
The Engineer's Guide to Fashion
Ralph Nader's List of Pleasures

--------------------------------------------------
Lines unique to text2:
America's Most Poppular Lawyers
Different Ways to Sell "Bob"
The Engineer's Guide to Passion
Ralph Nader's List of pleasures
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.