美文网首页
Python文件处理

Python文件处理

作者: 薛东弗斯 | 来源:发表于2022-06-11 06:48 被阅读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)

相关文章

  • Python 文件处理

    Python 读文件处理 1. readline() with 处理开闭文件&文件异常处理 readline()内...

  • 从Excel到Python (视频)

    Python Pandas处理Excel文件

  • Python 文件处理

    文件的理解 文件是数据的抽象和集合 文本文件 VS. 二进制文件 f.txt 文件保存: "人生苦短, 我用Pyt...

  • Python 文件处理

    文件处理 常用打开文件方式 1.读取文件内容, 模式为 'r' 表示读, 这也是打开的默认方式。 2.如果要创建或...

  • python 文件处理

  • python——文件处理

    1.文件处理 如果报错 说明编码不对。 按正常逻辑来讲,文件是以什么方式存的,就应该用什么方式去读取,比如以gb2...

  • Python 文件处理

    python 文件打开方式 文件打开方式: open(name[,mode[,buf]])name: 文件路径mo...

  • Python 文件处理

    创建文件 f = file('myfile.txt','w') f.write('hello,baby!')f....

  • python 文件处理

    文件处理有三个步骤:1、定义文件路径: 2、打开并保存文件内容。 r:以只读方式打开文件。文件的指针将会放在文件的...

  • Python文件处理

    文件读取 整个读取 两个注意点: 使用关键字with可以自动关闭文件流,如果使用file_object = ope...

网友评论

      本文标题:Python文件处理

      本文链接:https://www.haomeiwen.com/subject/wcbbmrtx.html