PDA

View Full Version : Python Help


JGaines
07-29-2009, 08:33 PM
Currently I am creating a rename program/script in python that I will make into a pyw. Here is a basic over view of how my script works:
Choose your directory
all files in that directory are put on a scroll list
select files in list to rename
type in new name
click button to rename

Here is the problem I am having:
When the files are put on the scroll list, python places them in alphabetical order, rather than the order they are in the folder. Also, having my selectmode=EXTENDED, when I select in a specific order, python doesn't hold true to your selection order. Instead, it reverts to the indexes of the listed items.

So if my desktop was my directory, and I had 4 text files (one.txt, two.txt, three.txt, and four.txt), My scroll list shows this:

four.txt
one.txt
three.txt
two.txt

Now I want to rename them in the selection order of one, two, three, four. However, since python reverts to the indexes, the selection order reverts back to the way the files are above.

Is there way for python to rename this way based on selection?
*curselection() returns the index of the selected object as a string

Here is the code I am using:

def renamer(self):
#Get selection from list:
items=self.jpg_listbox.curselection()

print("Here are the selected Items:")
print(items)

#Query Padding int value
pad= self.jpg_padField.get()

#Query Counter int Value
cnt= self.jpg_countField.get()

for item in items:
selText=self.jpg_listbox.get(item)

print("Item Index: %s" %item)

#Get directory:
curDir=os.getcwd()
fullPath=curDir+"\\"
print fullPath

#Split file name and extension:
fileExtGet=selText.split(".")
fileExt= fileExtGet[-1]

#Query new file name:
tempName= self.jpg_newFile.get()

#Query Increment check box
inc= self.jpg_incChk.get()

#If inc or no inc:
if (inc==1):
num=self.counter(cnt, pad)
newName="%s_%s"% (tempName, num)

#Join newName and fileExt together:
finalName=(".".join([newName, fileExt]))

print("Old Name: %s\nNew Name: %s" %(selText, finalName))

#Rename it:
os.rename(fullPath+selText, fullPath+finalName)
cnt=int(cnt)+1

else:
newName=tempName

#Join newName and fileExt together:
finalName=(".".join([newName, fileExt]))

print("Old Name: %s\nNew Name: %s" %(selText, finalName))

#Rename it:
os.rename(fullPath+selText, fullPath+finalName)

#Clear listbox:
self.jpg_listbox.delete(0, END)

#Load listbox with data:
path=os.getcwd()
files=os.listdir(path)
for file in files
self.jpg_listbox.insert(END, file)

Please help if you can.
PS this text box will not allow me to keep the proper indentation in my script.

CGTalk Moderation
07-29-2009, 08:33 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.