LoginSignup
0
1

More than 3 years have passed since last update.

Pythonで、OCTAのudfファイル名を操作する

Last updated at Posted at 2020-04-03

Pythonで、re、osモジュールを使ってファイル名を一括で処理する。

簡単なプログラムですが、OCTAを使用していて、UDFファイルのみを取り出して、ファイル名を一括で処理したい時があるので、覚えで作りました。

ファイル名に一括で分類名を付ける。

re: 正規表現モジュール
re.compile()を使うと、その中の文字列を正規表現にコンパイルしてオブジェクトを作成する。
reg_exit = re.compile(keyword) は、compileで、正規表現にコンパイルするキーワードを指定することで、正規表現になる。このオブジェクトを、reg_exitに入れる。

re.search(): 文字列すべてが検索対象。
os: ファイルやディレクトリの操作ができる。
os.rename(a,b): aのファイル名をbに変更する

for name in file_list:で、ファイル名を一つずつ取り出してnameに入れる。
if reg_exit.search(name):で、取り出したファイル名の中に、正規表現にコンパイルしたキーワードがあるかどうかを判別する。
udf_file.append(name) は、キーワードのあったファイルをリストに加える。
os.rename(file, category+file) は、ファイル名(file)にキーワード(category)を加える

OCTAでのファイル名に一括で分類名をつける
#モジュールをimportする
import os
import re
#ディレクトリー内のファイルのリストを作る.

file_list = os.listdir()
udf_file = []

reg_exit = re.compile(r'(.udf)$')

for name in file_list:
    if reg_exit.search(name):
        udf_file.append(name) 


#分類名を指定(category)して、ファイル名に追加(os.rename)する
category = 'カテゴリー10_'
for file in udf_file:
    os.rename(file, category+file)
0
1
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
0
1