python3----shutil模块copy、os模块、os.walk()、os.path读取文件夹与文件copy、判断是否为文件夹或文件

python3—-shutil模块copy、os模块、os.walk()、os.path读取文件夹与文件copy、判断是否为文件夹或文件

一、OS模块对应的知识

参考:https://blog.csdn.net/moshlwx/article/details/52694397

通过import os模块中提供的功能,实现python对操作系统中文件和目录的操作。习题相关的主要函数有:

    os.path.abspath(path),显示path在操作系统中的绝对路径
    os.path.join(path, x),将path和x拼接成当前操作系统兼容的路径形式
    os.listdir(path),输出当前目录下的文件及子目录为list类型
    os.path.isdir(path) and os.path.isfile(path)判断路径为目录还是文件,注意参数为path,之前在这里出现了很多错误,直接输文件名的话,没有该文件就直接显示False


脚本演示情况:
root@kali:~/bwapp# python3.6
Python 3.6.3 (default, Oct  3 2017, 21:16:13) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.path.abspath("/root"))
/root
>>> print(os.path.abspath("/root/bwapp"))
/root/bwapp
>>> 
>>> print(os.path.join("/root/bwapp","cp_init.py"))
/root/bwapp/cp_init.py
>>> print(os.listdir("/root/bwapp"))
['cp_init.py', 'cp_file_init.py', 'bWAPP.py', 'session_mangement', 'injection', 'sensitive_data_exposure', 'using_know_vulnerable_components', 'pocdb.py', 'xss', '__pycache__', 'missing_functional_level_access_control', 'security_misconfiguration', 'csrf', 'cookies.txt', 'broken_auth', 'unalidated_redirects_forwards', 'insecure_direct_object_references']
>>> 
>>> os.path.isdir("/root/bwapp")
True
>>> os.path.isdir("/root/bwapp/cp_init.py")
False
>>> 
>>> os.path.isfile("/root/bwapp/cp_init.py")
True
>>> os.path.isfile("/root/bwapp")
False
>>> 

二、使用上述知识点实现

实现代码功能:把某个特定的文件(__init__.py)copy到/root/bwapp目录下所有文件夹中(最简单方式建议使用os.walk())

root@kali:~/bwapp# cat cp_file_init.py 
#/usr/bin/python3.6
#--*-- coding:utf-8 --*--

import os
import shutil

def find_cur(path):
    #print("current_path is %s" % os.path.abspath(path))
    #遍历PATH当前目录下的文件夹,将文件夹的绝对路径添加到列表listdir中
    for x in os.listdir(path):#遍历path目录下所有文件夹与文件名
        if os.path.isdir(path+"/"+x):#只遍历path目录下的文件夹
            listdir.append(os.path.join(path,x))#对遍历到的文件夹增加到列表中

def deeper_dir():#对path目录下子目录进行遍历
    for x in listdir:
        #print(x)
        find_cur(x)#循环对path目录下子目录遍历
        #print(x)

def copy_file_to_everyfloder():#copy特定文件到path目录及其所有子目录中
        src = "/root/__init__.py"
        for x in listdir:
            #print (x)
            #在shutil模块中copy文件时源与目的都必须是文件名结尾
            dst = x+"/__init__.py"#拼接目的的文件名
            print(dst)
            shutil.copyfile(src,dst)#实现copy文件

if __name__ == "__main__":
    listdir = []
    find_cur("/root/bwapp/")
    deeper_dir()
    print(listdir)
    copy_file_to_everyfloder()

root@kali:~/bwapp# 

三、使用os.walk()函数实现
1.1、实现保存所有被遍历的文件与文件夹的绝对路径抓取

root@kali:~/bwapp# cat cp_init.py 
#!/usr/bin/python3.6
# --*-- coding:utf-8 --*--
#参考https://www.cnblogs.com/jiaxin359/p/7324077.html

import os

listdir = []#保存被遍历的文件夹的列表
listfile = []#保存被遍历的文件的列表
def detect_walk(dir_path):
    for root,dirs,files in os.walk(dir_path):
        for filename in files:
            #print ("\033[1;35m file:%s \033[0m!\n" % filename)#输出被遍历到的文件
            listfile.append(os.path.join(root,filename))#被遍历的到文件加上绝对路径后,增加到列表中
        for dirname in dirs:
            #print ("dir:%s\n" % dirname)#输出被遍历到的文件夹
            listdir.append(os.path.join(root,dirname))#被遍历的到文件夹加上绝对路径后,增加到列表中

if __name__ == "__main__":
    detect_walk("/root/bwapp")
    print("--------输出被遍历到的所有文件夹,并且带绝对路径--------")
    print(listdir)#输出被遍历到的所有文件夹,并且带绝对路径
    print("\n")
    print("++++++++输出被遍历到的所有文件,并且带绝对路径++++++++++")
    print(listfile)#输出被遍历到的所有文件,并且带绝对路径

root@kali:~/bwapp# 

1.2、脚本运行情况

root@kali:~/bwapp# python3.6 cp_init.py 
--------输出被遍历到的所有文件夹,并且带绝对路径--------
['/root/bwapp/session_mangement', '/root/bwapp/injection', '/root/bwapp/sensitive_data_exposure', '/root/bwapp/using_know_vulnerable_components', '/root/bwapp/xss', '/root/bwapp/__pycache__', '/root/bwapp/missing_functional_level_access_control', '/root/bwapp/security_misconfiguration', '/root/bwapp/csrf', '/root/bwapp/broken_auth', '/root/bwapp/unalidated_redirects_forwards', '/root/bwapp/insecure_direct_object_references', '/root/bwapp/injection/PHP_CODE_injection', '/root/bwapp/injection/XML_XPATH_injection', '/root/bwapp/injection/IFRAME_injection', '/root/bwapp/injection/__pycache__', '/root/bwapp/injection/HTML_injection', '/root/bwapp/injection/SQL_injection', '/root/bwapp/injection/SERVER_SIDE_INCLUDES_injection', '/root/bwapp/injection/OS_COMMAND_injection', '/root/bwapp/injection/SQL_injection/__pycache__', '/root/bwapp/xss/ws', '/root/bwapp/xss/sss', '/root/bwapp/xss/ws/sed', '/root/bwapp/xss/ws/sed/de', '/root/bwapp/xss/ws/sed/de/we']


++++++++输出被遍历到的所有文件,并且带绝对路径++++++++++
['/root/bwapp/cp_init.py', '/root/bwapp/cp_file_init.py', '/root/bwapp/bWAPP.py', '/root/bwapp/pocdb.py', '/root/bwapp/.cp_init.py.swp', '/root/bwapp/cookies.txt', '/root/bwapp/injection/injectionmain.py', '/root/bwapp/injection/__init__.py', '/root/bwapp/injection/__pycache__/__init__.cpython-36.pyc', '/root/bwapp/injection/__pycache__/injectionmain.cpython-36.pyc', '/root/bwapp/injection/SQL_injection/SQL_injection_get_search.py', '/root/bwapp/injection/SQL_injection/__init__.py', '/root/bwapp/injection/SQL_injection/__pycache__/__init__.cpython-36.pyc', '/root/bwapp/injection/SQL_injection/__pycache__/SQL_injection_get_search.cpython-36.pyc', '/root/bwapp/__pycache__/pocdb.cpython-36.pyc']
root@kali:~/bwapp# 

2.1使用os.walk()与shutil模块对文件进行copy

参考:https://www.cnblogs.com/wang-yc/p/5625046.html
https://www.cnblogs.com/jiaxin359/p/7324077.html
https://blog.csdn.net/keenweiwei/article/details/9039001

实现代码功能:把某个特定的文件(__init__.py)copy到/root/bwapp目录下所有文件夹中

环境配置:未实现前的/root/bwapp、文件夹情况

root@kali:~/bwapp# tree /root/bwapp
/root/bwapp
├── broken_auth
├── bWAPP.py
├── cookies.txt
├── cp_file_init.py
├── cp_init.py
├── csrf
├── injection
│   ├── HTML_injection
│   ├── IFRAME_injection
│   ├── __init__.py
│   ├── injectionmain.py
│   ├── OS_COMMAND_injection
│   ├── PHP_CODE_injection
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── injectionmain.cpython-36.pyc
│   ├── SERVER_SIDE_INCLUDES_injection
│   ├── SQL_injection
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   └── SQL_injection_get_search.cpython-36.pyc
│   │   └── SQL_injection_get_search.py
│   └── XML_XPATH_injection
├── insecure_direct_object_references
├── missing_functional_level_access_control
├── pocdb.py
├── __pycache__
│   └── pocdb.cpython-36.pyc
├── security_misconfiguration
├── sensitive_data_exposure
├── session_mangement
├── unalidated_redirects_forwards
├── using_know_vulnerable_components
└── xss

21 directories, 14 files
root@kali:~/bwapp# 
实现的Python脚本内容
root@kali:~/bwapp# cat cp_init.py 
#!/usr/bin/python3.6
# --*-- coding:utf-8 --*--

import shutil
import os
#参考https://www.cnblogs.com/jiaxin359/p/7324077.html

listdir = []#保存被遍历的文件夹的列表
listfile = []#保存被遍历的文件的列表
def detect_walk(dir_path):
    for root,dirs,files in os.walk(dir_path):
        for filename in files:
            #print ("\033[1;35m file:%s \033[0m!\n" % filename)#输出被遍历到的文件
            listfile.append(os.path.join(root,filename))#被遍历的到文件加上绝对路径后,增加到列表中
        for dirname in dirs:
            #print ("dir:%s\n" % dirname)#输出被遍历到的文件夹
            if dirname != "__pycache__":#排除__pycache__的缓存文件夹
                listdir.append(os.path.join(root,dirname))#被遍历的到文件夹加上绝对路径后,增加到列表中

def copy_file_to_everyfloder():
    src = "/root/__init__.py"#此文件必须不能在被copy的目录中
    for x in listdir:
        #print (x)
        #在shutil模块中copy文件时源与目的都必须是文件名结尾
        dst = x+"/__init__.py"#拼接目的的文件名
        print(dst)
        shutil.copyfile(src,dst)#实现copy文件

if __name__ == "__main__":
    detect_walk("/root/bwapp")
    #print("--------输出被遍历到的所有文件夹,并且带绝对路径--------")
    #print(listdir)#输出被遍历到的所有文件夹,并且带绝对路径
    #print("\n")
    #print("++++++++输出被遍历到的所有文件,并且带绝对路径++++++++++")
    #print(listfile)#输出被遍历到的所有文件,并且带绝对路径
    copy_file_to_everyfloder()

root@kali:~/bwapp#
脚本运行情况
root@kali:~/bwapp# python3.6 cp_init.py 
/root/bwapp/session_mangement/__init__.py
/root/bwapp/injection/__init__.py
/root/bwapp/sensitive_data_exposure/__init__.py
/root/bwapp/using_know_vulnerable_components/__init__.py
/root/bwapp/xss/__init__.py
/root/bwapp/missing_functional_level_access_control/__init__.py
/root/bwapp/security_misconfiguration/__init__.py
/root/bwapp/csrf/__init__.py
/root/bwapp/broken_auth/__init__.py
/root/bwapp/unalidated_redirects_forwards/__init__.py
/root/bwapp/insecure_direct_object_references/__init__.py
/root/bwapp/injection/PHP_CODE_injection/__init__.py
/root/bwapp/injection/XML_XPATH_injection/__init__.py
/root/bwapp/injection/IFRAME_injection/__init__.py
/root/bwapp/injection/HTML_injection/__init__.py
/root/bwapp/injection/SQL_injection/__init__.py
/root/bwapp/injection/SERVER_SIDE_INCLUDES_injection/__init__.py
/root/bwapp/injection/OS_COMMAND_injection/__init__.py
root@kali:~/bwapp# 
脚本运行成功后的文件夹/root/bwapp/的目录结构

root@kali:~/bwapp# tree /root/bwapp
/root/bwapp
├── broken_auth
│   └── __init__.py
├── bWAPP.py
├── cookies.txt
├── cp_file_init.py
├── cp_init.py
├── csrf
│   └── __init__.py
├── injection
│   ├── HTML_injection
│   │   └── __init__.py
│   ├── IFRAME_injection
│   │   └── __init__.py
│   ├── __init__.py
│   ├── injectionmain.py
│   ├── OS_COMMAND_injection
│   │   └── __init__.py
│   ├── PHP_CODE_injection
│   │   └── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── injectionmain.cpython-36.pyc
│   ├── SERVER_SIDE_INCLUDES_injection
│   │   └── __init__.py
│   ├── SQL_injection
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   └── SQL_injection_get_search.cpython-36.pyc
│   │   └── SQL_injection_get_search.py
│   └── XML_XPATH_injection
│       └── __init__.py
├── insecure_direct_object_references
│   └── __init__.py
├── missing_functional_level_access_control
│   └── __init__.py
├── pocdb.py
├── __pycache__
│   └── pocdb.cpython-36.pyc
├── security_misconfiguration
│   └── __init__.py
├── sensitive_data_exposure
│   └── __init__.py
├── session_mangement
│   └── __init__.py
├── unalidated_redirects_forwards
│   └── __init__.py
├── using_know_vulnerable_components
│   └── __init__.py
└── xss
    └── __init__.py

21 directories, 30 files
root@kali:~/bwapp# 

你可能感兴趣的:(python3)