Reputation: 21
I'm trying to write a small program that allows the user to click a button to select files, type in a stock number, and then click a button to rename those files.
This is what I've come up with so far:
import os
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilenames
root = Tk()
root.title("Photo Renamer")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
directory = ''
arrayOfFiles = []
stockNum = 0
def getStockNumber():
stockNum = stockNumber.get()
def selectFiles():
selectedFiles = askopenfilenames()
# Switch our files from a tuple to an array.
# arrayOfFiles = []
for i in selectedFiles:
arrayOfFiles.append(i)
return arrayOfFiles
def rename(array): # array will be arrayOfFiles
# Separate file names from the whole file path.
fileNames = []
for i in range(len(array)):
fileNames.append(os.path.basename(i))
count = 1
directory = os.path.dirname(array[1])
for file in directory:
oldFileName = '%s/%s' % (directory, file)
newFileName = '%s/gma%d_%d.jpg' % (directory, stockNum, count)
os.rename(oldFileName, newFileName)
count += 1
# "Stock Number" label
ttk.Label(mainframe, text="Stock Number: ").grid(column=1, row=1, sticky=W)
# Entry box
stockNumber = ttk.Entry(mainframe, width=7)
stockNumber.grid(column=2, row=1, sticky=(W, E))
# "Select Files" button
ttk.Button(mainframe, text="Select Files", command=selectFiles).grid(column=1, row=2, sticky=(W, E))
# "Number of Files" label
ttk.Label(mainframe, text="Number of Files: ").grid(column=1, row=3, sticky=W)
# "Rename" button
ttk.Button(mainframe, text="Rename", command= lambda: rename(arrayOfFiles)).grid(column=3, row=3, sticky=(W, E)) # command=rename
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
stockNumber.focus()
root.mainloop()
I can select the files, and type in the stock number, but when I click the rename button, it gives me this error in the console:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:/Users/.../Desktop/Python/gui.py", line 56, in <lambda>
ttk.Button(mainframe, text="Rename", command= lambda: rename(arrayOfFiles)).grid(column=3, row=3, sticky=(W, E)) # command=rename
File "C:/Users/.../Desktop/Python/gui.py", line 32, in rename
fileNames.append(os.path.basename(i))
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 232, in basename
return split(p)[1]
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 204, in split
d, p = splitdrive(p)
File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\ntpath.py", line 139, in splitdrive
if len(p) >= 2:
TypeError: object of type 'int' has no len()
Upvotes: 1
Views: 267
Reputation:
list.append() requires a string - you're giving an integer. Change
fileNames.append(os.path.basename(int))
to
fileNames.append(os.path.basename(str))
The error was thrown within the Python code (os
module, see the docs over here (Python 2)) when it tried to get the len(int)
, which is impossible (since int
doesn't have the method __len__
)
Upvotes: 1
Reputation: 7735
for i in range(len(array)):
fileNames.append(os.path.basename(i))
In your above code, i
is an integer and os.path.basename(path) expects a pathname like string.
When iterating over a list, you can direcly iterate through items.
for file in array:
fileNames.append(os.path.basename(file))
or if you want to use indexes, you can do
for i in range(len(array)):
fileNames.append(os.path.basename(array[i]))
^^^^^ notice accessing an item using index
Upvotes: 1
Reputation: 1569
Try this:
for i in range(array):
Instead of
for i in range(len(array)):
Upvotes: 0