以下の関数を使用して、ファイルをディレクトリにコピーし、関数が呼び出されたディレクトリに再作成します。ipythonでコードを部分的に実行しているときは、正常に機能しています。ただし、関数として実行すると、次のエラーが発生します。
---> 17 shutil.copy2(filein[0], os.path.join(dir,'template.in'))
TypeError: 'type' object is not subscriptable
これが機能です
import os
import shutil
from find import find
def recreatefiles(filedir):
currdir = os.getcwd() # get current directory
dirname = 'maindir'
dir = os.path.join(currdir,dirname)
if not os.path.exists(dir):
os.makedirs(dir)
#Copy .in files and create a template
filein = find('*.in',filedir) # find is a function created
shutil.copy2(filein[0], os.path.join(dir,'template.in'))
エラーについて何かアイデアはありますか?ありがとう
編集:これが検索のコードです
import os, fnmatch
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
if not name.startswith('.'):
result.append(os.path.join(root, name))
return result
EDIT2:ipythonからのfileinの出力
[1]: filein
[2]: ['/home/Projects/test.in']
基本的に、ファイルは1つだけです。shutil.copy2のfilein[0]を使用して、角かっこを削除しました