vendredi 8 mai 2015

python cannot get folders to save inside of one folder

I've run into some trouble that I believe must be an easy fix. I am currently pulling png files from one path, and saving them in a folder in another path. This is all done correctly. What I try to do after is to group those folders into one Results folder. My current code does not throw any exceptions, the results folder is created, but the other folders are not moving into the Results folder.

def create_folder(self,folder_name):
    '''Create folders if does not exist'''
    path,fileName = os.path.split(self.snmpPaths[0])
    target = os.path.join(path, folder_name)
    if not os.path.exists(target):
        os.makedirs(target)
    return target

snippet from other method

    if flag ==1:
        for png in pngs:
            if 'MPM' in png:
                target = self.create_folder("MPM")
                folderList.add(target)
            elif 'DAT' in png:
                target = self.create_folder("DAT")
                folderList.add(target)
            elif 'HNR' in png:
                target = self.create_folder("HNR")
                folderList.add(target)
            elif 'VER' in png:
                target = self.create_folder("VER")
                folderList.add(target)
            elif 'XT2R' in png:
                target = self.create_folder("XT2R")
                folderList.add(target)
            else:
                target = self.create_folder("Other")
                folderList.add(target)
            try:
                print "png =", png
                print "target = ", target

                os.rename(png, os.path.join(target, png))
                print "png after =", png
            except BaseException:
                print "Could not rename file for target"

    for folder in folderList:
        # path,folder = os.path.split(folder)
        # folder = "./%s" %(folder)
        try:
            print "folder = ", folder
            print "results = ", results
            os.rename(folder, os.path.join(results, folder))
            print "folder after rename = ", folder
        except BaseException:
            print "Could not rename file for results"

Here is a sample of my print statements. I can provide more if needed. Any help will be appreciated. I tried to copy the same format as the png files (./FILE) as seen in my commented code, but I would get a BaseException. An explanation would be great as to why my code is not performing the way I want it to. I'm guessing it has to do with my folder variable.

png = ./VER_SUBMODE.png

target = D:\SNMP\SPINACH\VER

png after = ./VER_SUBMODE.png

png = ./VER_TX_STATUS.png

target = D:\SNMP\SPINACH\VER

png after = ./VER_TX_STATUS.png

folder = D:\SNMP\SPINACH\VER

results = D:\SNMP\SPINACH\Results_2015-05-08_09-21-01

folder after rename = D:\SNMP\SPINACH\VER

Aucun commentaire:

Enregistrer un commentaire