0

I am trying to rename multiple files by importing the OS Module (Mac OS). To do this I have first split the files' titles from their extension and then the files (pdfs in this case) had an additional format specification (a4). Here's an example : "darwinism_a4.pdf". I am trying to remove the "a4" part. So, I have tried split the name at the underscore using this line : f_title, f_format = f_name.split(""). At this point I have received this error message :

Traceback (most recent call last): File "/Users/me/Documents/Programming/Cody_schafer/training.py", line 10, in f_title, f_format = f_name.split("_") ValueError: not enough values to unpack (expected 2, got 1)

I have finished the code anyways to show what I want to achieve and will include it here.

import os


for f in os.listdir():

    f_name, f_ext = os.path.splitext(f)

    print(f_name.split("_"))

    f_title, f_format = f_name.split("_")

    print(f_title, f_format)

    new_name = '{}-{}'.format(f_title, f_ext)

    os.rename(f, new_name)

I am thinking maybe it has something to do with all the files not having the same format? There's a file titled '.DS_Store' (for desktop services). It's put there by default.

  • 1
    It looks like you are trying to split a name that doesn't include the `"_"` character, so split is only returning one value. Are there any other files in that directory? – Craig Jan 16 '21 at 01:36
  • @Craig is there a way I can restrict the renaming only to certain files of a specific format? Also please see the edited version (last paragraph after code sample), can this be the problem? – Mahmoud Hosny Jan 16 '21 at 01:40
  • 1
    @MahmoudHosny you can. But you should do your research on your own. It's been asked before. – Mad Physicist Jan 16 '21 at 01:40
  • 1
    One of many options: [Get a filtered list of files in a directory](https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory) – Craig Jan 16 '21 at 01:43

1 Answers1

1

The problem is that some of the files or directories returned by os.listdir() do not contain an underscore. Your code needs to account for this possibility. For instance, you could do this:

import os

for f in os.listdir():
    f_name, f_ext = os.path.splitext(f)
    print(f_name.split("_"))
    if "_" in f_name:
        f_title, f_format = f_name.split("_", maxsplit=1)
        print(f_title, f_format)
        new_name = '{}.{}'.format(f_title, f_ext)
        os.rename(f, new_name)

Note also that I added maxsplit=1 to the split method, as it is also possible that files may have more than one underscore in their name.

Also, you could check which filename does not have the underscore by adding a print(f) line under the os.listdir() line. It might be a directory name, as os.listdir() lists both files and directories.

Finally, I changed your format string {}-{} to {}.{}, as file extensions follow a period, not a hyphen. (However, this matters more on Windows than on MacOS, as Windows checks the filename, but MacOS checks inside the file to find the file type.)

Jack Taylor
  • 5,588
  • 19
  • 35