LoginSignup
1
0

More than 3 years have passed since last update.

複数のファイル名の頭を指定した文字数分だけ一括削除する

Last updated at Posted at 2021-01-25

すんごい大量のファイルの、名前を、頭から指定した文字数だけ削除したい

たいしたことない特徴

・大量のファイルが入っている親のフォルダを.py内で指定
・ターミナルで実行
・何ファイルあっても大丈夫

注意!!必ず守ってください

変更したいファイルでやる前に、どうでもいいファイルでテストしてみるか、ファイルを複製しておいてくださいね。消しちゃいけない文字まで消した時にもし後戻りできないと悲惨なのと、責任は負えません。

ファイルの場所

'/Users/hogehoge/test_folder/'
この「folder」の下に以下のような名前の大量のファイルがあると仮定します

消したい文字fileA.txt
消したい文字fileB.txt
消したい文字fileC.txt
消したい文字fileD.txt
(以下大量)

期待する結果
fileA.txt
fileB.txt
fileC.txt
fileD.txt
(以下大量)

変えるところ

以下のコードの
path1と、new_nameの中身です

コード

test.py
import glob
import os

path1 = '/Users/hogehoge/test_folder/'
path2 = path1 + '*'

flist = glob.glob(path2)

i = 1
# ファイル名を一括で変更する
for file in flist:
    print('ファイル名:',file)
    onlyfile = os.path.split(file)[1]
    #以下の[0:]の数字を消したい文字数に変更してください
    #「消したい文字fileA.txt」の場合→[6:]
    new_name = onlyfile[0:]
    os.rename(file, path1 + new_name)
    i+=1

1
0
0

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
1
0