If I read that line by line, I think
This is removing all the .xlsx extensions of the file on disk
os.rename(filename, filename[:-5]) # Problem 1
Then adds the name without extension to the list
renamed.append(filename[:-5])
and then tries to join something a) on the whole array and b) on a file and its extension rather than two paths
os.path.join(renamed, '.xls') # Problem 2 and 3
You'd rather
newname = filename[:-5] # remove extension
newname = newname + ".xls" # add new extension
os.rename(filename, newname) # rename correctly
renamed.append( ... ) # Whatever name you want in the list
Also note that if filename.endswith(".Xlsx"):
might be False
for all files that end in lower case .xlsx
.
Instead of [:-5]
, you could use the help of the operating system as well:
import glob
import os
allFiles = glob.glob("c:/test/*.xlsx")
renamed = []
for filename in allFiles:
path, filename = os.path.split(filename)
basename, extension = os.path.splitext(filename)
if extension == ".xlsx":
destination = os.path.join(path, basename+".xls")
os.rename(filename, destination)
And just FYI: if renaming is the sole purpose of the program, try ren *.xlsx *.xls
at the Windows command prompt.