我想重命名使用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和堆栈溢出很陌生,所以我很抱歉如果之前有人问过这个问题,或者我在错误的地方问过这个问题。