Zhu-Xueming

os.walk()遍历所有的文件并列出各级,os.listdir()只列出当前文件下的条目,一个列表

1.os.walk() 大材小用。

import os
path = \'j:\\juypter\\爬虫\\百度图片\\\'
for dirnames, subnames, files in os.walk(path):#父目录,子目录,文件名称
    for file in files:#只取files里的文件名,不含目录
        new_name = \'美女\' + file
        os.rename(path + file, path + new_name)#重命名,写入相同的文件。

2,listdir()

import os
path = \'j:\\juypter\\爬虫\\风景图\\\'
for file in os.listdir(path):    #os.listdir(\'.\')遍历文件夹内的每个文件名,并返回一个包含文件名的list
    new_name = \'风景图\' + file[6:]
    os.rename(path + file, path + new_name)

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-02-23
  • 2022-01-16
相关资源
相似解决方案