老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

shutil模塊中的文件、文件夾、壓縮包處理模塊是怎樣的-創新互聯

shutil模塊中的文件、文件夾、壓縮包處理模塊是怎樣的,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

站在用戶的角度思考問題,與客戶深入溝通,找到即墨網站設計與即墨網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:做網站、網站設計、企業官網、英文網站、手機端網站、網站推廣、國際域名空間、網頁空間、企業郵箱。業務覆蓋即墨地區。
  • shutil.copyfileobj(fsrc, fdst[, length])將文件內容拷貝到另一個文件中,length是每次復制的大小

guessage.py中內容
"""
猜年齡游戲:
允許用戶最多猜三次,猜了三次后,詢問是都繼續玩,如果輸入Y,可以繼續猜三次,否則退出
"""
age = 23
count = 0
while count < 3:
    try:
        guess_age = int(input("input the age of you think:"))
    except ValueError:
        print("you should input one number!")
        count = count + 1
        continue

    if guess_age > 23:
        print("the age you input is too big!")
    elif guess_age < 23:
        print("the age you input is too small!")
    else:
        print("excellent!you are right!")
        break
    count = count + 1
    while count == 3:
        your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
        if your_choice.lower() == "y":
            count = 0
        elif your_choice.lower() =="n":
            break
        else:
            print("your input is illegal!input again!")
"正確的程序運行代碼"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfileobj(open("guessage.py","r",encoding="utf-8"),open("guessage_new.py","w",encoding="utf-8",10))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py

Process finished with exit code 0
"不加編碼時,有報錯信息"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfileobj(open("guessage.py","r"),open("guessage_new.py","w"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 5, in 
    shutil.copyfileobj(open("guessage.py","r"),open("guessage_new.py","w"))
  File "D:\software2\Python3\install\lib\shutil.py", line 79, in copyfileobj
    buf = fsrc.read(length)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 29: illegal multibyte sequence

Process finished with exit code 1
guessage_new.py
"""
猜年齡游戲:
允許用戶最多猜三次,猜了三次后,詢問是都繼續玩,如果輸入Y,可以繼續猜三次,否則退出
"""
age = 23
count = 0
while count < 3:
    try:
        guess_age = int(input("input the age of you think:"))
    except ValueError:
        print("you should input one number!")
        count = count + 1
        continue

    if guess_age > 23:
        print("the age you input is too big!")
    elif guess_age < 23:
        print("the age you input is too small!")
    else:
        print("excellent!you are right!")
        break
    count = count + 1
    while count == 3:
        your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
        if your_choice.lower() == "y":
            count = 0
        elif your_choice.lower() =="n":
            break
        else:
            print("your input is illegal!input again!")
  • shutil.copyfile(src, dst)拷貝文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfile("guessage.py","guessage_new.py")#目標文件無需存在
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=7881299347900196, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1557104769, st_mtime=1557104769, st_ctime=1557104769)

Process finished with exit code 0
  • shutil.copymode(src, dst)僅拷貝權限。內容、組、用戶均不變

"程序代碼"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copymode("guessage.py","guessage_new.py")#目標文件必須存在
#目標文件不存在時
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 5, in 
    shutil.copymode("guessage.py","guessage_new.py")
  File "D:\software2\Python3\install\lib\shutil.py", line 144, in copymode
    chmod_func(dst, stat.S_IMODE(st.st_mode))
FileNotFoundError: [WinError 2] 系統找不到指定的文件。: 'guessage_new.py'

Process finished with exit code 1
#新建一個文件guessage_new.py,內容為空,再運行程序
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copymode("guessage.py","guessage_new.py")
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=2533274790397796, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=61, st_atime=1557104202, st_mtime=1557104202, st_ctime=1557104202)

Process finished with exit code 0
  • shutil.copystat(src, dst)僅拷貝狀態的信息,包括:mode bits, atime, mtime, flags

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copystat("guessage.py","guessage_new.py") #目標文件必須存在
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=2814749767108452, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=61, st_atime=1555571381, st_mtime=1555571381, st_ctime=1557104576)

Process finished with exit code 0
  • shutil.copy(src, dst)拷貝文件和權限

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copy("guessage.py","guessage_new.py")#目標文件可以不存在
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=6473924464346978, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1557104716, st_mtime=1557104716, st_ctime=1557104716)

Process finished with exit code 0
  • shutil.copy2(src, dst)拷貝文件和狀態信息

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copy2("guessage.py","guessage_new.py")
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=7318349394478946, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1557104918)

Process finished with exit code 0
  • shutil.ignore_patterns(*patterns)

  • shutil.copytree(src, dst, symlinks=False, ignore=None)遞歸的去拷貝文件夾

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

# 目標目錄不能存在,注意對folder2目錄父級目錄要有可寫權限,ignore的意思是排除
shutil.copytree('ee', 'new_ee', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
print(os.listdir("ee"))
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['eee']
['eee']

Process finished with exit code 0
  • shutil.rmtree(path[, ignore_errors[, onerror]])遞歸的去刪除文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

shutil.rmtree('new_ee')
print(os.listdir("ee"))
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
['eee']
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 10, in 
    print(os.listdir("new_ee"))
FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: 'new_ee'

Process finished with exit code 1
  • shutil.move(src, dst)遞歸的去移動文件,它類似mv命令,其實就是重命名。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

shutil.move('ee','new_ee')
print(os.listdir("ee"))#move之后,源文件就不存在了
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 9, in 
    print(os.listdir("ee"))
FileNotFoundError: [WinError 3] 系統找不到指定的路徑。: 'ee'

Process finished with exit code 1

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.move('new_ee','mkdir') #可以移到另一個文件夾中
print(os.listdir("mkdir"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['new_ee']

Process finished with exit code 0
  • shutil.make_archive(base_name, format,...)創建壓縮包并返回文件路徑,例如:zip、tar

base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時,則保存至當前目錄,否則保存至指定路徑,
如 data_bak =>保存至當前路徑
如:/tmp/data_bak =>保存至/tmp/

format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要壓縮的文件夾路徑(默認當前目錄)
owner: 用戶,默認當前用戶
group: 組,默認當前組
logger: 用于記錄日志,通常是logging.Logger對象

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
#將 test2 下的文件打包放置當前程序目錄
import shutil
import os
ret = shutil.make_archive("test2_bak", 'gztar', root_dir='test2')#目標的壓縮包可以是已經存在的
print(os.listdir())
#將 test2下的文件打包放置 mkdir目錄
import shutil
ret = shutil.make_archive("mkdir/test2_bak", 'gztar', root_dir='test2')
print(os.listdir("mkdir"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['.idea', 'guessage.py', 'guessage_new.py', 'mkdir', 'requirements.txt', 'test.py', 'test2', 'test2_bak.tar.gz', '__pycache__', '寫文件.txt', '寫文件.txt.tmp', '格式化.py', '猜年齡的游戲.jpg', '讀文件.txt']
['new_ee', 'test2_bak.tar.gz']

Process finished with exit code 0
shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:
zipfile壓縮&解壓縮
import zipfile

# 壓縮
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解壓
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()

tarfile壓縮&解壓縮
import tarfile

# 壓縮,egon.tar這個壓縮包可以是不存在的,并且可對文件夾遞歸放到壓縮包中
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()

# 解壓
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()

關于shutil模塊中的文件、文件夾、壓縮包處理模塊是怎樣的問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創新互聯行業資訊頻道了解更多相關知識。

另外有需要云服務器可以了解下創新互聯cdcxhl.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。


新聞名稱:shutil模塊中的文件、文件夾、壓縮包處理模塊是怎樣的-創新互聯
網站路徑:http://www.xueling.net.cn/article/dppipd.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 国产国语一级毛片在线放 | 他揉捏她两乳不停呻吟人妻 | 成人久久综合 | 国产亚洲精品综合一区91555 | 久久久久久久免费观看 | 日韩欧美中文字幕视频 | gay男生露j打飞j视频网站 | a级毛片高清免费播放 | 久久九九国产精品 | 麻豆免费观看网站 | 欧美三个奶波霸 | 欧美日韩视频在线观看一区 | 石原莉奈一区二区在线播放 | 日日影视 | 欧洲大片精品免费永久看nba | Chinese国产HD精品实拍 | 免费人成网WW555KKK在线 | av无码午夜福利一区二区三区 | 中文字幕制服丝袜一区二区三区 | 超碰国产人人做人人爽久 | 特级毛片爽WWW免费版 | 香港毛片基地 | 远方的山楂树免费观看视频48集 | 日韩欧美成人免费观看 | 国产成人精品三级 | 免费的网站www | 国产一区在线导航 | 午夜精品一区二区三区在线视频 | 新婚少妇初尝禁果 | 久精品国产欧美 | 国产精品原创巨作av | 91av视频免费在线?看 | 免费黄色影片 | 成人春色在线观看免费网站 | 亚洲高清免费看 | 国产尤物在线观看yw | 亚洲日本乱码在线观看 | 亚洲一区二区五区 | 亚洲国产av无码精品果冻传媒 | 欧美色窝79yyyycom | 中国国语毛片免费观看视频 |