0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonで数字のファイル名を1つずつずらして名前変更

Posted at

1.txt ⇒ 2.txt
2.txt ⇒ 3.txt
3.txt ⇒ 4.txt

もっと良いやり方あったら教えてくださいm(_ _)m

import os
path = "./dirname"
folderfile = os.listdir(path)
file = [f for f in folderfile if os.path.isfile(os.path.join(path, f))]
newlist = sorted(file, reverse=True)
for f in newlist:
    basename_without_ext = int(os.path.splitext(os.path.basename(f))[0])
    before_name = os.path.join(path,f)
    after_name  = os.path.join(path, str(basename_without_ext + 1) + os.path.splitext(os.path.basename(f))[1])
    os.rename(before_name,after_name)
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?