Python文件处理

2022-06-11  本文已影响0人  薛东弗斯

1. 遍历目录、文件

import os

path=r"C:\Training\Python"

for foldName, subfolders, filenamesin os.walk(path):

for filenamein filenames:

print(foldName,filename)

2. 文件树状图

import os

def filetree(path, depth):

if depth ==0:

print("文件夹:" + path)

for filein os.listdir(path):

print("|    " * depth +"+--" + file)

directory = path +'/'+ file

if os.path.isdir(directory):

filetree(directory, depth +1)

filetree(r'C:\Training\Python', 0)

3. 批量更名

import os

path=r"H:\示例\第3章\批量更名"

for foldName, subfolders, filenamesin os.walk(path):

for filenamein filenames:

abspath=os.path.join(foldName,filename)

extension =os.path.splitext(abspath)[1]

new_name=filename.replace(extension,"2020"+extension)

new_name="2020"+new_name

os.rename(abspath,os.path.join(foldName,new_name))

4. 修改文件名

import os

path=r"H:\示例\第3章\case"

for foldName, subfolders, filenamesin os.walk(path):

for filenamein filenames:

abspath=os.path.join(foldName,filename)

new_name=abspath.replace("\\","-").replace(":","-").replace("--","-")

new_name="H:\\示例\\第3章\\case\\"+new_name

os.rename(abspath,os.path.join(foldName,new_name))

5. 删除小文件

import os

for filein os.listdir():

path=os.path.abspath(file)

filesize = os.path.getsize(file)

if (filesize <2000) & (os.path.splitext(path)[1]!=".txt"):

os.remove(file)

6. 删除空文件夹

import os,shutil

path=r"H:\示例\第3章\case"

for filein os.listdir(path):

directory = path +'\\'+ file

if os.path.isdir(directory):

shutil.rmtree(directory)

7. 删除重复文件

import os,shutil,hashlib

path=r"H:\示例\第3章\case"

list=[]

for filein os.listdir(path):

fileName = path +'\\'+ file

if os.path.isdir(fileName):

shutil.rmtree(fileName)

else:

m = hashlib.md5()

with open(fileName, "rb")as mfile:

m.update(mfile.read())

md5_value = m.hexdigest()

if md5_valuein list:

os.unlink(fileName)

else:

list.append(md5_value)

8. 库搜索

import os,inspect,importlib

def filetree(path, depth,f):

if depth ==0:

print("文件夹:" + path,file=f)

for filein os.listdir(path):

print("|    " * depth +"+--" + file,file=f)

directory = path +'\\'+ file

if file.endswith(".py"):

use_inspect(path, depth,f,file)

if os.path.isdir(directory):

filetree(directory, depth +1,f)

def use_inspect(path, depth,f,file):

fileN=file.replace(".py","")

module_name=path.replace(rootdir,"").replace("\\",".")+"."+fileN

print("|    " *  (depth+1)  +"+--模块" + file,file=f)

try:

my_module=importlib.import_module(module_name)

for name, objin inspect.getmembers(my_module):

if inspect.isclass(obj):

pt0="|    " * (depth+1) +"+--" +str(obj)

print(pt0,file=f)

for k,vin obj.__dict__.items():

if not(str(k).startswith("_")):

pt1="|    " * (depth+2) +"+--" +str(k)+":"+str(v)

print(pt1,file=f)

if inspect.isfunction(obj):

pt0="|    " * (depth+1) +"+--" +str(obj)

print(pt0,file=f)

except:

print("导入"+module_name+"失败",file=f)

rootdir ='C:\\ProgramData\\Anaconda3\\Lib\\site-packages\\'

package ="openpyxl"

with open('test.txt','w')as f:

filetree(rootdir+package,0,f)

上一篇下一篇

猜你喜欢

热点阅读