renaming file prefixes and filetypes
In python 2.7, how would I go about renaming any .bat
files in my current directory to a file called 1.txt
?
[test.txt, abc.exe, dkckx.bat, 123.vbs]
, how could I rena开发者_运维百科me the dkckx.bat
to 1.txt
?
thx in advance.Check the python documentation for the rename
function.
And may be glob
will be helpful too.
os.path
has everything you'd need for filename manipulation. glob
can search for files:
import os, glob
i = 1
for f in glob.glob('*'):
if os.path.splitext(os.path.split(f)[-1])[-1].lower() == '.bat':
os.rename(f, '{0}.txt'.format(i)
i += 1
No guarantees, but I think this above script should work.
精彩评论