I want to erase the unnecessary name of the image file in a specific folder.

Asked 1 weeks ago, Updated 1 weeks ago, 3 views

In the folder C:\Users\Administrator\Downloads\T


There are these files. 6 digits in front of the file

000140.png I want to change it like this.

I'm trying to look it up, but I'm too new to understand.

How do I approach to delete without the extension after leaving only the first six digits?

import os

path = 'C:/Users/Administrator/Downloads/T'
for file in os.listdir(path):
    if filename.startswith("[0:5]"):


2022-09-20 08:45

1 Answers

import os

path = 'C:/Users/Administrator/Downloads/T'
for filename in os.listdir(path):
    name = filename.rsplit(".", 1)[0][0:6]
    ext = filename.rsplit(".", 1)[1]
    os.rename(filename, name+ext)

Why don't you try this?

2022-09-20 08:45

If you have any answers or tips

© 2022 pinfo. All rights reserved.