I'm new to python and want to improve it. Now I want to write a python script to organize my fastq file names into a txt file, like this:
My files are like this:
d1_S10_L001_R1_001.fastq
d1_S10_L001_R2_001.fastq
d2_S11_L001_R1_001.fastq
d2_S11_L001_R2_001.fastq
What I want is to write a txt file like this:
d1 d1_S10_L001_R1_001.fastq d1_S10_L001_R2_001.fastq
d2 d2_S11_L001_R1_001.fastq d2_S11_L001_R2_001.fastq
This file contains: the strings before the first "_" followed by the fastq pairs. They are separated by "\t".
I know this should be a very simple python task, but all I can do right now is:
import os
files = os.listdir(os.getcwd() + "/fastq")
with open("microbiome.files", "w") as myfile:
for file in files:
filename = file.split("_")[0]
myfile.write(filename + "\t" + file + '\n')
This is obviously not doing the right job. It gives me:
d1 d1_S10_L001_R1_001.fastq
d1 d1_S10_L001_R2_001.fastq
d2 d2_S11_L001_R1_001.fastq
d2 d2_S11_L001_R2_001.fastq
How to correct this?
Thank you so much!