import os
for root, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".pyc"):
os.remove(os.path.join(root, file))
删除文件夹pychche
import os
import shutil
for root, dirs, files in os.walk(rootdir):
for dir in dirs:
if dir == "pycache":
shutil.rmtree(os.path.join(root, dir))
重命名文件pyc,去除指定字符串
import os
for root, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".pyc"):
os.rename(os.path.join(root, file),os.path.join(root, file.replace(".cpython-39","",1)))
当检测到当前目录下的子文件夹(pycache)内有指定文件时,将该文件移动到当前目录。
import shutil
import os
for root, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".py"):
des_dir = os.path.join(root,'pycache')
for child_root,child_dirs,child_files in os.walk(des_dir):
for child_file in child_files:
if(child_file.replace(".pyc","",1) == file.replace(".py","",1)):
shutil.move(os.path.join(child_root, child_file),os.path.join(root, child_file))
删除文件pyc
import os for root, dirs, files in os.walk(rootdir): for file in files: if file.endswith(".pyc"): os.remove(os.path.join(root, file))
删除文件夹pychche
import os import shutil for root, dirs, files in os.walk(rootdir): for dir in dirs: if dir == "pycache": shutil.rmtree(os.path.join(root, dir))
重命名文件pyc,去除指定字符串
import os for root, dirs, files in os.walk(rootdir): for file in files: if file.endswith(".pyc"): os.rename(os.path.join(root, file),os.path.join(root, file.replace(".cpython-39","",1)))
当检测到当前目录下的子文件夹(pycache)内有指定文件时,将该文件移动到当前目录。
import shutil import os for root, dirs, files in os.walk(rootdir): for file in files: if file.endswith(".py"): des_dir = os.path.join(root,'pycache') for child_root,child_dirs,child_files in os.walk(des_dir): for child_file in child_files: if(child_file.replace(".pyc","",1) == file.replace(".py","",1)): shutil.move(os.path.join(child_root, child_file),os.path.join(root, child_file))