p神之python目录遍历,爬虫

Python目录遍历

参考链接:https://www.leavesongs.com/PYTHON/pythonfile.html

1.作用

查找黑客上传的webshell

2.开头添加# -*- coding=UTF-8 -*-,可以#加中文不报错

3.多行注释

选中代码,按住ctrl+/多行注释

4.代码

# -*- coding=UTF-8 -*-
import os
def replace_str(filepath,sourcestr,objectstr):
    file = open(filepath,"r")
    str = file.read()
    str = str.replace(sourcestr,objectstr)
    file.close()
    file = open(filepath,"w")
    file.write(str)
    file.close()
def getfile(path):
    # leix = os.listdir(path)
    # print(type(leix))
    # print(leix)
    for file in os.listdir(path):
        file = os.path.join(path,file)
#        print(file)
        if os.path.isdir(file):
            getfile(file)
        else:
            replace_str(file,"abcd","")
rootpath = "D:/phpStudy/WWW/pshen/bianli" #要用/,以列表输出目录和文件
getfile(rootpath)

5.另外一种

# -*- coding=UTF-8 -*-
import os
def replace_str(filepath,sourcestr,objectstr):
    file = open(filepath,"r")
    str = file.read()
    str = str.replace(sourcestr,objectstr)
    file.close()
    file = open(filepath,"w")
    file.write(str)
    file.close()

def getfile():
    generator = os.walk('D:/phpStudy/WWW/pshen/bianli')
    for (nowdir,_,file_list) in generator:
        for file in file_list:
            file = os.path.join(nowdir,file)
            print(file)
            replace_str(file,"abcd","")
getfile()
# generator = os.walk('D:/phpStudy/WWW/pshen/bianli')
# print(type(generator))
# print(generator)
# for i in generator:
#     print(i)

Python与爬虫

扫描二维码关注公众号,回复: 8383729 查看本文章

1.python直接使用requests库

2.python调用浏览器  app仿冒项目

3.python调用app  python玩转抖音项目

猜你喜欢

转载自www.cnblogs.com/qzdlp/p/12128397.html