1

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?

1 Answer 1

1

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):

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.