An error is reported when renaming the image in the file with python: Windows error[2] Solution

Windows error[2] solution

When I used python to rename the picture in the file, I encountered the windows error[2] error, which was mainly caused by the path problem. Adding os.chdir(file) to the code can solve this problem. Change the path to the path where you need to rename the image.
The error is as follows:
Insert picture description here

#-*- codeing = utf-8 -*-
#@Time:2021/1/4 20:12
#@Author: Z2Q
#@File : change_name.py
#@Software : PyCharm
import os ,os.path ,time

def rename(file):
    ''' file: 文件路径  '''

    **os.chdir(file)**
    items = os.listdir(file)
    i = 5294
    for name in items:
        a = os.path.splitext(name)[0]
        new_name = name.replace(a,str(i))
        print(new_name)
        i = i+1
        os.rename(name,new_name)
        print("success")


if __name__ == '__main__':
    path = 'G:\\software\\PyCharm 2018.3.3\\jewuis\\datasource\\rename\\k'
    rename(path)
    ```

Guess you like

Origin blog.csdn.net/BigData_Mining/article/details/112203082