Thank you for the Python script! But I am having problems with executing the script...
>>> os.listdir()
['ABCD345XB.txt', 'input.txt', 'ABCD123QW.txt', 'script.py']
>>> python script.py input.txt
SyntaxError: invalid syntax
>>>
Hello BioStar Community,
I would like to rename multiple files in a folder using R script (or scripts written in Python or Perl).
The files in the folder look like:
And I have a text file which contains Old Names and corresponding New Names:
I am a novice in the field of bioinformatics, could anyone help with the file renaming issue?
Here's a solution in Python. It will work with different operating systems (Windows, Linux, MacOS, etc.). Make sure the script is in the same directory as the files you want to rename.
Python code (script.py)
import os
import sys
with open(sys.argv[1]) as fh:
for line in fh:
sl = line.strip().split(",")
old_name = sl[0]
new_name = sl[1]
os.rename(old_name, new_name)
How to use
python script.py input.txt
Input.txt
ABCD123QW.txt,AL001.txt
ABCD345XB.txt,AL002.txt
Demo
Look at the screenshot of my terminal on Mac. First I display all files that are in the directory (ABCD123QW.txt, ABCD345XB.txt, input.txt, script.py). Then, I run the script and list files again. You can see that two text files were renamed to AL001.txt and AL002.txt.

Thank you for the Python script! But I am having problems with executing the script...
>>> os.listdir()
['ABCD345XB.txt', 'input.txt', 'ABCD123QW.txt', 'script.py']
>>> python script.py input.txt
SyntaxError: invalid syntax
>>>
Python scripts need to be executed from the command prompt (under Windows) or terminal (under Linux/MacOs). The prompt >>> indicates that you are now in an interactive Python interpeter session, also called the “Python shell”. This is different from the normal terminal command prompt.
Thank you for the note!
I switched to the terminal (Mac), and ran the script, but still there was an error message:
>>> pprint(dirlist)
['ABCD345XB.txt', 'input.txt', 'ABCD123QW.txt', 'script.py']
>>> python script.py input.txt
File "<stdin>", line 1
python script.py input.txt
^
IndentationError: unexpected indent
>>>
I updated my answer by adding a screenshot of my terminal on Mac.
Thank you very much, the python code script.py) worked well!
By the way, why does the python code work in Mac terminal [--- -bash --- 80 x 24], but not in python [--- python --- 80 x 24]?
I have a bash script to do the same thing but it requires comma-separated file as input.
#!/bin/bash
INPUT="$1"
IFS=,
[ ! -f "$INPUT" ] && { echo "$INPUT file not found"; exit 99; }
while read old_name new_name
do
rename "$old_name" "$new_name" # mv or rename
done < "$INPUT"
How to use:
bash script.sh input.txt
Input.txt structure:
Do not include any header.
ABCD123QW.txt,AL001.txt
ABCD345XB.txt,AL002.txt
Arup, Thank you for the bash script!
If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted. You can accept multiple answers as correct.

I have executed the bash script, and it worked! But unfortunately, only the name of the first file was changed to AL001, but not the other files...
Any solutions for this?
Can you share error you are getting or first few lines of the csv file you are using, the script working perfectly fine in my computer with multiple file name as input.
Log in to answer this question.
The 1st time, only one file was renamed, but no error message. The 2nd time, an error message popped up, as shown in the screenshot, which is expectable
I don't know why it didn't work in your computer here I attached a GIF Animation.
The Screenshot:
Please use
ADD COMMENT/ADD REPLYwhen responding to existing posts to keep threads logically organized.All of your comments regarding
bashscript should have gone under @Arup's answer.