create folder and save file inside each new folder
1
0
Entering edit mode
2.4 years ago

Hi all, I apologize in advance. I feel this is simple but I'm stuck for a few hours.

Giving a list of gene names(list_of_genes) and a tab delimited file such as id"\t"gene_names (args.i bellow),

how do I create a folder with gene name and save selected id within each new folder. So I guess its basically creating a folder g and then save the two instances of df5 data frame within this new g.

I'm at:

list_of_genes =['lactato desi', 'NADH lac']
for g in list_of_genes:
        m=['id', 'all']
        ids_tabbed=pd.read_csv(args.i,  sep="\t", names=m)
        df5 = ids_tabbed[ids_tabbed['all'].str.contains(g, na=False)]
        os.mkdir('{}'.format(str(g.replace(' ', '_'))))
        df5.to_csv(f"{g.replace(' ', '_')}.csv", index=False, header=None, sep="\t")
        df5[['id']].to_csv(f"ID.{g.replace(' ', '_')}.id.csv", index=False, header=None)

Im able to create the folders: lactato_desi NADH_lac and the files. But I don't know how to save each bunch files to its specific folder =(

Real thanks for your help!

Biopython • 923 views
ADD COMMENT
4
Entering edit mode
2.4 years ago
LChart 4.6k

Provide the local path to the file:

dir_name = g.replace(' ', '_')
file_name = g.replace(' ', '_') + '.csv'
file_path = '{}/{}'.format(dir_name, file_name)
os.mkdir(dir_name)
df5.to_csv(file_path)
ADD COMMENT
0
Entering edit mode

All working. Thanks a lot =)

ADD REPLY
2
Entering edit mode

A small educational note: if an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they all work. If an answer was not really helpful or did not work, provide detailed feedback so others know not to use that answer.

upvote_bookmark_accept

ADD REPLY
0
Entering edit mode

Noted. Thank Ram

ADD REPLY

Login before adding your answer.

Traffic: 1887 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6