Anekdotin
Anekdotin

Reputation: 1591

Changing the foldername and file name under directory

I am trying to change the foldername, and the file names. They all have a $, and I want a #. Here is what I got

def cleanFiles(self):
    #directory = '/Users/eeamesX/work/data/Sept_1_upload/priority_2/transcriptsAudoSplits/09012015_331_male3_r1_seg1/IT_007/hell$o '
    directoryChosen = self.directoryChoice()
    print directoryChosen + "     you made it to files selected"
    self.listWidget.addItem(directoryChosen)
    #for file_names in os.listdir(directoryChosen):
        #self.listWidget.addItem(file_names)

    for n in os.listdir(directoryChosen):
        print n + "   made it here"
        if os.path.isfile(directoryChosen):
            print directoryChosen + "almost there"
            newname =  n.replace('$', '#')
            print newname + "    this is newname"
            if newname != n:
                os.rename(n,newname)
    print '\n--------------------------------\n'
    for n in os.listdir(directoryChosen):
        print n
        self.lblNamechange.show()

I have pinpointed my problem through the printing. It is in the line

if os.path.isfile(directoryChosen):

It is not reading the files in the directory to change the filenames. Any help? Also, Will this change the foldername?

Upvotes: 1

Views: 27

Answers (1)

idjaw
idjaw

Reputation: 26578

If what you are passing is in fact a directory, then this:

if os.path.isfile(directoryChosen):

will never be True, because isfile checks whether what you are passing is in fact a file.

Running help(os.path.isfile) in your interpreter will show you:

isfile(path)
    Test whether a path is a regular file

What you want to use is actually isdir:

if os.path.isdir(directoryChosen):

Upvotes: 1

Related Questions