This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use python to rename files in a folder with known prefix, prefix are in excel?

I want to rename 970 files in a folder. The name is like this "ENCFF207HQD.bam" and so on. And the prefix I want to use is in an excel. The existing prefix is in the second column, and the prefix I want to use is in the third column, How to use information in the excel to rename all the files correctly each by each. Thanks!

Excel like this:

9   ENCFF006UXO ETS1-human
10  ENCFF468AKT ETS1-human
11  ENCFF000YFF ATF1-human
12  ENCFF000YFI ATF1-human
13  ENCFF990JRT GATA2-human
14  ENCFF778HCA GATA2-human
15  ENCFF257UWF EZH2-human
16  ENCFF083IDB EZH2-human
17  ENCFF788FFM HDAC8-human
next-gen python r

Could you perhaps indicate what your file names look like as they are now? Are they actually named something like ENCFF006UXO.bam or is it more along the lines of ENCFF006UXO_somethingsomething.bam?

Also, your column 1 and column 2 there have a many-to-one relationship. For instance:

ENCFF006UXO ETS1-human
ENCFF468AKT ETS1-human

Does this mean that those two files (ENCFF006UXO_something.bam and ENCFF468AKT_something.bam) should get concatenated (merged together) into one file named ETS1-human_something.bam?

1 answer

First, export your Excel file to comma-separated value (.csv) format. This will allow you to use Python's CSV module to read your prefix file.

You can do the actual file renaming using os.rename .

Log in to answer this question.