在用python完成文件中图片重命名时报错:Windows error[2]解决方法

Windows error[2]解决方法

在用python完成文件中图片重命名时,遇到了windows error[2]的错误,主要是路径问题。代码中加上os.chdir(file)那句代码就能解决这个问题,把路径改变到你所需要重命名图片的路径下。
错误如下:
在这里插入图片描述

#-*- 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)
    ```

猜你喜欢

转载自blog.csdn.net/BigData_Mining/article/details/112203082
今日推荐