Python遍历文件夹并 改所有文件的后缀名

Python025

Python遍历文件夹并 改所有文件的后缀名,第1张

import os

import shutil

root = 'F:\\python2'

for rt, dirs, files in os.walk(root):

    for f in files:

        fn = os.path.join(rt, f)

        nfn = fn.replace('.txt', '.py').

        if src in fn and fn != nfn:

            shutil.move(fn, nfn)

遍历所有文件

from os import walk

f = []

for (dirpath, dirnames, filenames) in walk(mypath):

    f.extend(filenames)

    break