python 脚本收藏

去掉文件中的空格

import os
import sys

def delline(infile,outfile):
    infopen = open(infile,"r",encoding="utf-8")
    outfopen = open(outfile,"w",encoding="utf-8")

    lines = infopen.readlines()
    for line in lines:
        if line.split():
            outfopen.writelines(line)
        else:
            outfopen.writelines("")

    infopen.close()
    outfopen.close()

delline("index.html","temp.heml")

遍历文件中的目录

1 rootdir = 'F:\data'
2 list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
3 for i in range(0,len(list)):
4        path = os.path.join(rootdir,list[i])
5        if os.path.isfile(path):
6               #你想对文件的操作
#使用os.walk方法遍历:

import os
path="D:\\Temp_del\\a"
for i in os.walk(path):
print(i)

猜你喜欢

转载自www.cnblogs.com/csnd/p/10798023.html