最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python基礎(chǔ)學(xué)習(xí)之組織文件

 更新時(shí)間:2021年05月24日 09:28:41   作者:shenmingik  
今天帶大家復(fù)習(xí)python基礎(chǔ)知識,此文章將要介紹如何組織文件,既拷貝,移動等,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下

一、Shutil 模塊

shutil其實(shí)也就是shell模塊。其中包含一些函數(shù),可以讓我們在python程序中復(fù)制、移動、改名和刪除文件。

1.1 復(fù)制文件和文件夾

  • shutil.copy(source,destination):將路徑source處的文件復(fù)制到路徑destination處的文件夾。如果destination是一個(gè)文件名,那么它將作為被復(fù)制的新名字
  • shutil.copytree(source,destination):將路徑source處的文件夾,包括它的所有文件和子文件夾,復(fù)制到路徑destination處的文件夾。
import shutil
import os

current_path = os.getcwd()
# 拷貝test.txt 文件到temp1文件夾下
shutil.copy(current_path+"/test.txt",current_path+"/Python/temp1")
# 將temp1文件夾內(nèi)容拷貝到temp2文件夾下,此時(shí)會創(chuàng)建temp2文件夾
shutil.copytree(current_path+"/Python/temp1",current_path+"/Python/temp2")

結(jié)果:

在這里插入圖片描述

1.2 移動文件和文件夾

  • shutil.move(source,destination):將source處的文件夾移動到路徑destination,并返回新位置的絕對路徑的字符串


如果destination指向一個(gè)文件夾,source文件將移動到destination中,并保持原來的文件名;
如果destination指向一個(gè)文件,這個(gè)文件就會被覆蓋,注意!
如果destination指向一個(gè)不存在的文件夾,這個(gè)時(shí)候source里面的內(nèi)容會移動到destination,source改名為destination

import shutil
import os

current_path = os.getcwd()
# 將temp2文件夾內(nèi)的文件拷貝到temp中,并將temp2改名為temp
shutil.move(current_path+"/Python/temp2",current_path+"/temp")

結(jié)果:

在這里插入圖片描述

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")

結(jié)果:

在這里插入圖片描述

二、遍歷文件

  • os.walk(path):通過傳入一個(gè)路徑

os.walk()在循環(huán)的每次迭代中,返回三個(gè)值:

1.當(dāng)前文件夾名稱的字符串

2.當(dāng)前文件夾中子文件夾的字符串的列表

3.當(dāng)前文件夾中文件的字符串的列表

import shutil
import os

current_path = os.getcwd()
for folder_name,sub_folders,file_names in os.walk(current_path):
    print(folder_name+":")
    # 加載當(dāng)前文件路徑下的所有子文件
    for sub_folder in sub_folders:
        print("\t"+folder_name+": "+sub_folder+"(dir)")
    for file_name in file_names:
        print("\t"+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模塊中的函數(shù),python程序可以創(chuàng)建和打開ZIP文件。

3.1 創(chuàng)建和添加ZIP文件

想要?jiǎng)?chuàng)建ZIP文件,必須用ZipFile方法創(chuàng)建一個(gè)ZipFile對象。ZipFile對象在概念上和File對象相似。

之后,以寫模式打開這個(gè)對象。調(diào)用write()方法傳入一個(gè)路徑,python就會壓縮該路徑所指的文件,將它加到ZIP文件中。write的第一個(gè)參數(shù)是一個(gè)字符串,代表要添加的文件名。第二個(gè)參數(shù)是”壓縮類型“參數(shù),告訴計(jì)算機(jī)使用怎樣的算法來壓縮文件。一般來說,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)

結(jié)果:

在這里插入圖片描述

3.2 讀取ZIP文件

當(dāng)用ZipFile函數(shù)打開一個(gè)zip文件的時(shí)候,會返回一個(gè)ZipFile對象。之后調(diào)用這個(gè)對象的namelist()方法就可以獲得zip里面的壓縮文件列表。

同時(shí)這個(gè)對象還有一個(gè)getinfo()方法,通過傳入一個(gè)壓縮文件名,就可以獲得這個(gè)文件的一些信息。

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("\tsize: "+str(info.file_size))
    print("\tcompress_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文件中解壓縮所有的文件和文件夾,放到當(dāng)前工作目錄下:

import zipfile
import os

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

結(jié)果:

在這里插入圖片描述

四、參考文獻(xiàn)

AI Sweigart.Python編程快速上手——讓繁瑣工作自動化.人民郵電出版社.2016.07

到此這篇關(guān)于python基礎(chǔ)學(xué)習(xí)之組織文件的文章就介紹到這了,更多相關(guān)python組織文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python?Pandas中布爾索引的用法詳解

    Python?Pandas中布爾索引的用法詳解

    布爾索引是一種使用?DataFrame?中數(shù)據(jù)的實(shí)際值的索引。本文將通過一些示例為大家詳細(xì)講講Python中布爾索引的用法,需要的可以參考一下
    2022-08-08
  • Python實(shí)現(xiàn)抓取網(wǎng)頁生成Excel文件的方法示例

    Python實(shí)現(xiàn)抓取網(wǎng)頁生成Excel文件的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)抓取網(wǎng)頁生成Excel文件的方法,涉及PyQuery模塊的使用及Excel文件相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • python 利用turtle庫繪制笑臉和哭臉的例子

    python 利用turtle庫繪制笑臉和哭臉的例子

    今天小編就為大家分享一篇python 利用turtle庫繪制笑臉和哭臉的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法

    Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法

    這篇文章主要介紹了Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法,適用于數(shù)據(jù)較多時(shí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Python正則表達(dá)式匹配和提取IP地址

    Python正則表達(dá)式匹配和提取IP地址

    這篇文章主要介紹了Python正則表達(dá)式匹配和提取IP地址的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • python神經(jīng)網(wǎng)絡(luò)使用tensorflow構(gòu)建長短時(shí)記憶LSTM

    python神經(jīng)網(wǎng)絡(luò)使用tensorflow構(gòu)建長短時(shí)記憶LSTM

    這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)tensorflow構(gòu)建長短時(shí)記憶網(wǎng)絡(luò)LSTM,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python psutil 模塊概述及使用示例

    python psutil 模塊概述及使用示例

    psutil是一個(gè)跨平臺的Python庫,用于系統(tǒng)監(jiān)控、性能分析和進(jìn)程管理,它提供了豐富的API,可用于獲取系統(tǒng)的CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等資源的使用情況,以及進(jìn)行進(jìn)程管理,psutil支持Linux、Windows、macOS等主流操作系統(tǒng)
    2024-11-11
  • python模塊引入問題和解決方案

    python模塊引入問題和解決方案

    本文主要介紹了python模塊引入問題和解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python實(shí)現(xiàn)抖音視頻批量下載

    python實(shí)現(xiàn)抖音視頻批量下載

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)抖音視頻批量下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python實(shí)現(xiàn)子類調(diào)用父類的初始化實(shí)例

    Python實(shí)現(xiàn)子類調(diào)用父類的初始化實(shí)例

    這篇文章主要介紹了Python實(shí)現(xiàn)子類調(diào)用父類的初始化實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03

最新評論

隆安县| 仪征市| 杭锦旗| 姜堰市| 定远县| 利辛县| 乌拉特中旗| 海安县| 东光县| 天峻县| 鸡泽县| 聂拉木县| 全椒县| 襄汾县| 吴旗县| 定安县| 腾冲县| 台中市| 汝城县| 天柱县| 肇州县| 哈巴河县| 巴东县| 栖霞市| 江华| 武定县| 巴南区| 合川市| 罗城| 峡江县| 中江县| 应用必备| 安乡县| 北宁市| 龙门县| 青海省| 垣曲县| 闽侯县| 湘乡市| 泊头市| 东乌珠穆沁旗|