I'm trying to get a dirList.txt file in 2 different directories to (eventually) compare them. I've got a basic start going

# A program to create and compare directory lists of 
# a music folder and its backup

import os, cPickle, string, stat, fnmatch

# Define variables for the 2 lists

mu1List = os.listdir('/Users/BradyO/Desktop/Mu1')
mu2List = os.listdir('/Users/BradyO/Desktop/Mu2')

class Comparelist:
    
    def getMu1():

        # Makes sure it's in the right directory

        if os.getcwd() != 'Users/BradyO/Desktop/Mu1':
            os.chdir('/Users/BradyO/Desktop/Mu1') 

        # Opens a file called "dirList.txt" and writes an intro to the log 
        # The txt file is in the CWD since it changes to Mu1 at the start-up
        """
        ---It's also nice since it will compare the two dirList.txts
        after it creates them.---
        """
    
        fileHandle = open( 'dirList.txt', 'w' )
        fileHandle.write( 'This is the log file of Mu1.\nIt will be compared with Mu2\n\n-------Contents-------\n' )
        fileHandle.close()
    
        # Append the already defined list: mu1List
    
        fileHandle = open( 'dirList.txt', 'a' )
        fileHandle.write('mu1List')
        fileHandle.close()
        
    getMu1()
    
    def getMu2():
    
        os.chdir('/Users/BradyO/Desktop/Mu2')
        
        fileHandle2 = open( 'dirList.txt', 'w' )
        fileHandle2.write( 'This is the log file of Mu2.\nIt will be compared with Mu1\n\n-------Contents-------\n' )
        fileHandle2.close()
    
        # Append the already defined list: mu2List
    
        fileHandle2 = open( 'dirList.txt', 'a' )
        fileHandle2.write('mu2List')
        fileHandle2.close()
        
    getMu2()
            
Comparelist()

The dirList.txts print out the name of the list instead of what is actually inside. I had it working before, but I added the def getMu2() and put the mu1List and mu2Lists in the globals. Can you point me in the right direction? I've tryed a bunch of combinations. Also, I realize this is inefficient, and I'm just working on getting the nuts-n-bolts. Thanks

Got it. Just needed to pickle the list and then write it

fileHandle = open( 'dirList.txt', 'w' )
fileHandle.write( 'This is the log file... etc etc' )
cPickle.dump(mu1List, fileHandle)
fileHandle.close()
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.