-1

I am trying to rename a set of files in a folder from .Xlsx to .xls. This is what I have done thus far:

allFiles = glob.glob("/*.Xlsx") # Folder that has all the files
renamed = []
for filename in allFiles:
    if filename.endswith(".Xlsx"):
        os.rename(filename, filename[:-5])
        renamed.append(filename[:-5]) # removed the .Xlsx extension
        os.path.join(renamed, '.xls') # this fails

I am trying to see how can I have .xls added to the above list renamed

kalzso
  • 502
  • 2
  • 6
  • 27
scott martin
  • 1,253
  • 1
  • 14
  • 36
  • Besides the renaming: you are aware that making these renames won't change the unferlying file types or structure, right? I.e. old ms. excel versions will still be unable to open then - it is not just a name change. – jsbueno Apr 24 '19 at 15:26
  • Possible duplicate of [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python) – handras Apr 24 '19 at 15:27
  • folks, while you are at it, one might try posting an answer using pathlib and its methods, instead of "glob", "os.path.join" and filename[:-5] to strip the suffix. – jsbueno Apr 24 '19 at 15:28
  • For multile files: https://stackoverflow.com/questions/17748228/rename-multiple-files-in-python – handras Apr 24 '19 at 15:29

4 Answers4

4

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.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 1
    thanks for the reply. I tried to do the above but I get an error `FileNotFoundError: [Errno 2] No such file or directory: file1.Xlsx -> file1/.xls`. But I can see the `File1.Xlsx` in the folder despite executing the rename method above – scott martin Apr 24 '19 at 15:51
  • yes I am aware that this will only work if the file extension is '.Xlsx' – scott martin Apr 24 '19 at 15:55
  • @scottmartin: good catch. `os.path.join` will work for paths only and add slashes (backslashes) as needed. It's not useful for concatenating strings – Thomas Weller Apr 24 '19 at 16:45
1
  1. With your glob call, if filename.endswith(".Xlsx"): should always be true.

  2. You mix up your order:

    os.rename(filename, filename[:-5])  # this renames foo.Xlsx to foo, everything after it is too late.
    renamed.append(filename[:-5]) # This adds the file w/o the extension, but also w/o the new extension.
    os.path.join(renamed, '.xls') # This is a statement which would produce a result if called correctly (i. e. with a string instead of a list), but the result is discarded.
    

    Instead, do

    basename = filename[:-5]
    newname = os.path.join(basename, '.xls')
    os.rename(filename, newname)
    renamed.append(basename) # or newname? Choose what you need.
    
glglgl
  • 89,107
  • 13
  • 149
  • 217
0

If I understood correctly, you're currently dividing the process in the following steps:

  • Remove the xlsx extension
  • Add the file to a list (without extension)
  • Add the new extension to the file (this fails because os.path.join won't take a list as input)

To keep it simpler, I would only rename to the new extension, and if you need the renamed list, populate it. Like so:

allFiles = glob.glob("/*.Xlsx") <- Folder that has all the files
renamed = []
for filename in allFiles:
    if filename.endswith(".Xlsx"):
        new_name = filename[:-5]+'.xls'
        os.rename(filename, new_name)
        renamed.append(new_name)
francisco sollima
  • 7,952
  • 4
  • 22
  • 38
-1

os.path.join does not rename the file. You should directly rename it with the os.rename method by doing:

os.rename(filename, filename[:-5]+'.xls')
shriakhilc
  • 2,922
  • 2
  • 12
  • 17