How to change folder names in python?

2020-05-20 08:14发布

I have multiple folders each with the name of a person, with the first name(s) first and the surname last. I want to change the folder names so that the surname is first followed by a comma and then the first name(s) follow.

As an example, in the folder Test, i have:

C:/Test/John Smith
C:/Test/Fred Jones
C:/Test/Ben Jack Martin

and i want to make this:

C:/Test/Smith, John
C:/Test/Jones, Fred
C:/Test/Martin, Ben Jack

I tried some things with os.rename but i couldn't seem to make it work with the varying name length, and i wasn't sure how to insert the comma into the surname.

Also, some of the folder names are already in the correct form, so i need to skip these folders during the renaming. I think you can do this by just adding an if, so that if the folder name contains a comma it will continue.

Otherwise, the surname will always be the last word in the folder name.

Thanks for any help you can provide.

标签: python rename
3条回答
乱世女痞
2楼-- · 2020-05-20 08:53

You can write it out fairly straight-forward, using os.listdir and the os.path functions:

import os
basedir = 'C:/Test'
for fn in os.listdir(basedir):
  if not os.path.isdir(os.path.join(basedir, fn)):
    continue # Not a directory
  if ',' in fn:
    continue # Already in the correct form
  if ' ' not in fn:
    continue # Invalid format
  firstname,_,surname = fn.rpartition(' ')
  os.rename(os.path.join(basedir, fn),
            os.path.join(basedir, surname + ', ' + firstname))
查看更多
手持菜刀,她持情操
3楼-- · 2020-05-20 08:55
os.rename("Joe Blow", "Blow, Joe")

Seems to work fine for me. Which part are you having trouble with?

查看更多
戒情不戒烟
4楼-- · 2020-05-20 09:11

I like phihag's suggestion of rpartition(), I think the following are mostly equivalent:

>>> 'first second third fourth'.rpartition(' ')
('first second third', ' ', 'fourth')
>>> 'first second third fourth'.rsplit(None, 1)
['first second third', 'fourth']

I prefer rsplit() because I don't want to care about the separator, but I can also see that it is a bit more verbose.

Setup

>>> base = 'C:\\Test'
>>> os.makedirs(os.path.join(base, 'John Smith'))
>>> os.makedirs(os.path.join(base, 'Fred Jones'))
>>> os.makedirs(os.path.join(base, 'Ben Jack Martin'))
>>> os.listdir(base)
['Ben Jack Martin', 'Fred Jones', 'John Smith']

Solution

>>> for old_name in os.listdir(base):
    # [::-1] is slice notation for "reverse"
    new_name = ', '.join(old_name.rsplit(None, 1)[::-1])
    os.rename(os.path.join(base, old_name),
          os.path.join(base, new_name))


>>> os.listdir(base)
['Jones, Fred', 'Martin, Ben Jack', 'Smith, John']
查看更多
登录 后发表回答