当前位置:
首页
文章
后端
详情

python如何组织文件?python文件操作全介绍!

很多小伙伴在文件的使用上并没有养成一个良好的归类习惯,以至于像小编一样文档常常找不到,而真要花费时间去整理的时候往往工作量巨大直接劝退。但是呢,这样的重复操作为什么不交给python来做呢?接下来的这篇文章,小编就来介绍一下python怎么组织文件和文件夹操作,以及python遍历文件夹和压缩文件的方法

    一、Shutil 模块

    shutil其实也就是shell模块。其中包含一些函数,可以让我们在python程序中复制、移动、改名和删除文件

    1.1 复制文件和文件夹

    • shutil.copy(source,destination):将路径source处的文件复制到路径destination处的文件夹。如果destination是一个文件名,那么它将作为被复制的新名字
    • shutil.copytree(source,destination):将路径source处的文件夹,包括它的所有文件和子文件夹,复制到路径destination处的文件夹。
    import shutil
    import os
    
    current_path = os.getcwd()
    # 拷贝test.txt 文件到temp1文件夹下
    shutil.copy(current_path+"/test.txt",current_path+"/Python/temp1")
    # 将temp1文件夹内容拷贝到temp2文件夹下,此时会创建temp2文件夹
    shutil.copytree(current_path+"/Python/temp1",current_path+"/Python/temp2")

    结果:

    文件移动

    1.2 移动文件和文件夹

    • shutil.move(source,destination):将source处的文件夹移动到路径destination,并返回新位置的绝对路径的字符串


    如果destination指向一个文件夹,source文件将移动到destination中,并保持原来的文件名;
    如果destination指向一个文件,这个文件就会被覆盖,注意!
    如果destination指向一个不存在的文件夹,这个时候source里面的内容会移动到destination,source改名为destination

    import shutil
    import os
    
    current_path = os.getcwd()
    # 将temp2文件夹内的文件拷贝到temp中,并将temp2改名为temp
    shutil.move(current_path+"/Python/temp2",current_path+"/temp")

    结果:

    文件夹操作

    1.3 删除文件和文件夹

    • os.unlink(path):将删除path处的文件
    • os.rmdir(path):将删除path处的文件夹。该文件夹必须为空,其中没有任何文件和文件夹
    • shutil.retree(path):将删除path处的文件夹,它包含的所有文件和文件夹都会被删除
    import shutil
    import os
    
    current_path = os.getcwd()
    shutil.rmtree(current_path+"/temp")

    结果:

    删除文件

    二、遍历文件

    • os.walk(path):通过传入一个路径

    os.walk()在循环的每次迭代中,返回三个值:

    1.当前文件夹名称的字符串

    2.当前文件夹中子文件夹的字符串的列表

    3.当前文件夹中文件的字符串的列表

    import shutil
    import os
    
    current_path = os.getcwd()
    for folder_name,sub_folders,file_names in os.walk(current_path):
        print(folder_name+":")
        # 加载当前文件路径下的所有子文件
        for sub_folder in sub_folders:
            print("	"+folder_name+": "+sub_folder+"(dir)")
        for file_name in file_names:
            print("	"+folder_name+": "+file_name)

    输出(部分):

    ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
    /home/ubuntu/python_file:
            /home/ubuntu/python_file: .vscode(dir)
            /home/ubuntu/python_file: Python(dir)
            /home/ubuntu/python_file: test.cpp
            /home/ubuntu/python_file: test.txt
            /home/ubuntu/python_file: tempCodeRunnerFile.py
    /home/ubuntu/python_file/.vscode:
            /home/ubuntu/python_file/.vscode: db(dir)
            /home/ubuntu/python_file/.vscode: .git(dir)
            /home/ubuntu/python_file/.vscode: log(dir)
            /home/ubuntu/python_file/.vscode: settings.json
    /home/ubuntu/python_file/.vscode/db:
            /home/ubuntu/python_file/.vscode/db: cpptips.db-wal
            /home/ubuntu/python_file/.vscode/db: cpptips.db-shm
            /home/ubuntu/python_file/.vscode/db: cpptips.db
    /home/ubuntu/python_file/.vscode/.git:
            /home/ubuntu/python_file/.vscode/.git: 6eb7a60f73d1a1d9bdf44f2e86d7f4cc_test.cpp
    /home/ubuntu/python_file/.vscode/log:
            /home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-19.log
            /home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-16.log
            /home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-17.log
            /home/ubuntu/python_file/.vscode/log: cpptips.client.log
            /home/ubuntu/python_file/.vscode/log: cpptips.server.log
    /home/ubuntu/python_file/Python:
            /home/ubuntu/python_file/Python: temp1(dir)
            /home/ubuntu/python_file/Python: .git(dir)
            /home/ubuntu/python_file/Python: README.md
            /home/ubuntu/python_file/Python: hello_world.py
            /home/ubuntu/python_file/Python: orignize_files.py
            /home/ubuntu/python_file/Python: regex.py
            /home/ubuntu/python_file/Python: file_mange.py
    .........
    .........
    .........

    三、压缩文件

    利用zipfile模块中的函数,python程序可以创建和打开ZIP文件。

    3.1 创建和添加ZIP文件

    想要创建ZIP文件,必须用ZipFile方法创建一个ZipFile对象。ZipFile对象在概念上和File对象相似。

    之后,以写模式打开这个对象。调用write()方法传入一个路径,python就会压缩该路径所指的文件,将它加到ZIP文件中。write的第一个参数是一个字符串,代表要添加的文件名。第二个参数是”压缩类型“参数,告诉计算机使用怎样的算法来压缩文件。一般来说,ZIP_DEFLATED就可以了。

    import zipfile
    import os
    
    current_path = os.getcwd()
    new_zip = zipfile.ZipFile("newZip","w")
    new_zip.write("test.txt",compress_type=zipfile.ZIP_DEFLATED)
    new_zip.write("test.cpp",compress_type=zipfile.ZIP_DEFLATED)

    结果:

    3.2 读取ZIP文件

    当用ZipFile函数打开一个zip文件的时候,会返回一个ZipFile对象。之后调用这个对象的namelist()方法就可以获得zip里面的压缩文件列表。

    同时这个对象还有一个getinfo()方法,通过传入一个压缩文件名,就可以获得这个文件的一些信息。

    import zipfile
    import os
    
    current_path = os.getcwd()
    new_zip = zipfile.ZipFile("newZip","r")
    files = new_zip.namelist()
    
    for file in files:
        info = new_zip.getinfo(file)
        print("filename: "+file)
        print("	size: "+str(info.file_size))
        print("	compress_size: "+str(info.compress_size))

    输出:

    ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
    filename: test.txt
            size: 26
            compress_size: 26
    filename: test.cpp
            size: 30
            compress_size: 28

    3.3 解压缩ZIP文件

    ZipFile对象的extractall方法从ZIP文件中解压缩所有的文件和文件夹,放到当前工作目录下:

    import zipfile
    import os
    
    current_path = os.getcwd()
    example_zip = zipfile.ZipFile("newZip","r")
    # 解压
    example_zip.extractall()
    example_zip.close()

    结果:


    到此这篇关于python怎么组织文件的文章就介绍到这了,更多python相关的学习内容请搜索W3Cschool以前的文章或继续浏览下面的相关文章。

    免责申明:本站发布的内容(图片、视频和文字)以转载和分享为主,文章观点不代表本站立场,如涉及侵权请联系站长邮箱:xbc-online@qq.com进行反馈,一经查实,将立刻删除涉嫌侵权内容。

    同类热门文章

    深入了解C++中的new操作符:使用具体实例学习

    C++中的new操作符是动态分配内存的主要手段之一。在程序运行时,我们可能需要动态地创建和销毁对象,而new就是为此提供了便利。但是,使用new也常常会引发一些问题,如内存泄漏、空指针等等。因此,本文将通过具体的示例,深入介绍C++中的new操作符,帮助读者更好地掌握其使用。

    
深入了解C++中的new操作符:使用具体实例学习

    怎么用Java反射获取包下所有类? 详细代码实例操作

    Java的反射机制就是在运行状态下,对于任何一个类,它能知道这个类的所有属性和方法;对于任何一个对象,都能调用这个对象的任意一个方法。本篇文章将通过具体的代码示例,展示如何通过Java反射来获取包下的所有类。

    
怎么用Java反射获取包下所有类? 详细代码实例操作

    了解Java中的volati关键字的作用 以及具体使用方法

    本篇文章将和大家分享一下Java当中的volatile关键字,下面将为各位小伙伴讲述volatile关键字的作用以及它的具体使用方法。

    
了解Java中的volati关键字的作用 以及具体使用方法

    Java Map 所有的值转为String类型

    可以使用 Java 8 中的 Map.replaceAll() 方法将所有的值转为 String 类型: 上面的代码会将 map 中所有的值都转为 String 类型。 HashMap 是 Java

    Java Map 所有的值转为String类型

    员工线上学习考试系统

    有点播,直播,在线支付,三级分销等功能,可以对学员学习情况的监督监控,有源码,可二次开发。支持外网和局域网私有化部署,经过测试源码完整可用!1、视频点播:视频播放,图文资料,课件下载,章节试学,限时免

    员工线上学习考试系统