Currently the file names in a directory are 'test1_1_4302929399.txt', 'test1_2_903929029.txt', 'test1_3_3949492929.txt' , how do I delete the last 10 digits in the file names for this directory so that the final is 'test1_1.txt', 'test1_2.txt', etc. in python? I have tried:
"""
import os
with open(filename, 'rb+') as filehandle:
filehandle.seek(-1, os.SEEK_END)
filehandle.truncate()
"""
But this doesn't work for all files as they are named differently. Many thanks!
Try this. Use os.rename
import os
path1=....
for i in os.listdir(path1):
os.rename(os.path.join(path1, i),os.path.join(path1, i[:8])+'.txt')