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

Asked 2 years ago, Updated 2 years ago, 24 views

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

000230_2022-07-14_05-02-44.png,
000140_2022-07-14_12-43-38.png,
000105_2022-07-14_12-54-49.png,

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

000230.png
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]"):
        os.rename(filename,"[0:5]"+filename[-5:])

python

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


© 2024 OneMinuteCode. All rights reserved.