pythonを使用してディレクトリ内のファイルの名前を変更しようとしています。ファイルには現在、プール番号、AR番号、S番号(例:Pool1_AR001_S13__fw_paired.fastq.gz)のラベルが付けられています。各ファイルには特定のプラントシーケンス名が含まれています。これらのファイルの名前を変更するには、 'Pool_AR_S'を削除してシーケンス名に置き換えます。 'Lbienne_dor5_GS1'は接尾辞(例:fw_paired.fastq.gz、rv_unpaired.fastq.gz)を残していますが、ファイルを辞書に読み込もうとしていますが、次に何をすべきかについては固執しています。私は、次の形式で必要な情報を含む.txtファイルを持っている:pythonを使用してディレクトリ内のファイル名の一部を置換する
Pool1_AR010_S17 - Lbienne_lla10_GS2
Pool1_AR011_S18 - Lbienne_lla10_GS3
Pool1_AR020_S19 - Lcampanulatum_borau4_T_GS1
私がこれまで持っているコードは次のとおりです。
from optparse import OptionParser
import csv
import os
parser = OptionParser()
parser.add_option("-w", "--wanted", dest="w")
parser.add_option("-t","--trimmed", dest="t")
parser.add_option("-d", "--directory", dest="working_dir", default="./")
(options, args) = parser.parse_args()
wanted_file = options.w
trimmomatic_output = options.t
#Read the wanted file and create a dictionary of index vs species identity
with open(wanted_file, 'rb') as species_sequence:
species_list = list(csv.DictReader(species_sequence, delimiter='-'))
print species_list
#Rename the Trimmomatic Output files according to the dictionary
for trimmed_sequence in os.listdir(trimmomatic_output):
os.rename(os.path.join(trimmomatic_output, trimmed_sequence),
os.path.join(trimmomatic_output, trimmed_sequence.replace(species_list[0], species_list[1]))
あなたはの半分を置き換えるために私を助けることができるしてください。私は非常にPythonとオーバーフローをスタックするので、この質問が以前に尋ねられた場合、または私は間違った場所でこれを尋ねた場合はごめんなさい申し訳ありません。