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
• 1,689 views
•
link
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 .
• 0 views
•
link
Log in to answer this question.
Could you perhaps indicate what your file names look like as they are now? Are they actually named something like
ENCFF006UXO.bamor is it more along the lines ofENCFF006UXO_somethingsomething.bam?Also, your column 1 and column 2 there have a many-to-one relationship. For instance:
Does this mean that those two files (
ENCFF006UXO_something.bamandENCFF468AKT_something.bam) should get concatenated (merged together) into one file namedETS1-human_something.bam?