How can I rename multiple files in a certain directory using Python?

0 votes
I'm trying to rename some files in a directory using Python.

I'm tried to use the os.path.split but it's not working properly. I have also considered using string manipulations, but have not been successful with that either. Can someone help??
Nov 23, 2018 in Python by charlie_brown
• 7,720 points
337 views

1 answer to this question.

0 votes

Use os.rename(src, dst) to rename or move a file or a directory.

$ ls
cheese_cheese_type.bar  cheese_cheese_type.foo
$ python
>>> import os
>>> for filename in os.listdir("."):
...  if filename.startswith("cheese_"):
...    os.rename(filename, filename[7:])
... 
>>> 
$ ls
cheese_type.bar  cheese_type.foo
answered Nov 23, 2018 by ariaholic
• 7,340 points

Related Questions In Python

+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,810 points
249 views
0 votes
1 answer
0 votes
1 answer

How to list only text files in a directory using python?

Well, you are using a complex way. ...READ MORE

answered Feb 3 in Python by Omkar
• 68,480 points
364 views
0 votes
1 answer

How can I lookup hostname using the IP address with a timeout in Python?

Good question. I actually was stuck with ...READ MORE

answered Feb 6 in Python by Nymeria
• 3,520 points
103 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 425 views
+4 votes
6 answers
0 votes
1 answer

How can I rename files on the fly using Python?

You could simply use a wrapper object ...READ MORE

answered Sep 7, 2018 in Python by ariaholic
• 7,340 points
55 views
0 votes
1 answer

How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

answered Oct 9, 2018 in Python by ariaholic
• 7,340 points
475 views