日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python创建模块文件夹_python文件、文件夹、压缩包处理模块-shutil模块

發布時間:2024/10/12 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python创建模块文件夹_python文件、文件夹、压缩包处理模块-shutil模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

shutil模塊 高級的文件、文件夾、壓縮包 處理模塊

本節內容基本在linux下python交互環境實現

復制移動文件、文件夾

將文件內容拷貝到另一個文件中,可以部分內容

格式如下:

```

shutil.copyfileobj(fsrc, fdst[, length])

```

源代碼如下:

```

def copyfileobj(fsrc, fdst, length=16*1024):

"""copy data from file-like object fsrc to file-like object fdst"""

while 1:

buf = fsrc.read(length)

if not buf:

break

fdst.write(buf)

```

拷貝文件

shutil.copyfile(src, dst)

shutil.copyfile("shell.py","/usr/local/src/shell.py")

例子:

import shutil

f1 = open("file1",encoding="utf-8")

f2 = open("file2","w")

shutil.copyfileobj(f1,f2)

#等于下面的一句

shutil.copyfile("file2","file3")

僅拷貝權限、內容、組、用戶均不變

shutil.copymode(src, dst)

如:

>>> os.system("ls -lh /usr/local/src/shell.py")

-rwxrwxrwx 1 ftp ftp 5.0K 11月 22 15:15 /usr/local/src/shell.py

0

>>> shutil.copymode("shell.py","/usr/local/src/shell.py")

>>> os.system("ls -lh /usr/local/src/shell.py")

-rwxr--r-- 1 ftp ftp 5.0K 11月 22 15:15 /usr/local/src/shell.py

拷貝狀態的信息,包括:mode bits, atime, mtime, flags

shutil.copystat(src, dst)

拷貝文件和權限

shutil.copy(src, dst)

源代碼如下:

def copy(src, dst):

"""Copy data and mode bits ("cp src dst").

The destination may be a directory.

"""

if os.path.isdir(dst):

dst = os.path.join(dst, os.path.basename(src))

copyfile(src, dst)

copymode(src, dst)

拷貝文件和狀態信息

shutil.copy2(src, dst)

源代碼如下:

def copy2(src, dst):

"""Copy data and all stat info ("cp -p src dst").

The destination may be a directory.

"""

if os.path.isdir(dst):

dst = os.path.join(dst, os.path.basename(src))

copyfile(src, dst)

copystat(src, dst)

遞歸的去拷貝文件

shutil.ignore_patterns(*patterns)

shutil.copytree(src, dst, symlinks=False, ignore=None)

例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))

遞歸的去刪除文件

shutil.rmtree(path[, ignore_errors[, onerror]])

如:

>>> os.system("ls -lh abc/aa/bb/cc")

總用量 0

-rw-r--r-- 1 root root 0 11月 22 15:41 test.sh

0

>>> shutil.rmtree("abc")

>>> os.system("ls -lh abc/aa/bb/cc")

ls: 無法訪問abc/aa/bb/cc: 沒有那個文件或目錄

512

遞歸的去移動文件

shutil.move(src, dst)

打包文件、文件夾,壓縮包處理

創建壓縮包并返回文件路徑,例如:zip、tar

語法:shutil.make_archive(base_name, format,...)

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

如:www =>保存至當前路徑

如:/Users/aaa/www =>保存至/Users/aaa/

format : 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”

root_dir: 要壓縮的文件夾路徑(默認當前目錄)

owner : 用戶,默認當前用戶

group : 組,默認當前組

logger : 用于記錄日志,通常是logging.Logger對象

將 /root/11 下的文件打包放置當前程序目錄

>>> import shutil

>>> os.system("ls -lh ")

總用量 8.0K

drwxr-xr-x 3 root root 15 11月 22 15:52 11

-rwxr--r-- 1 root root 5.0K 7月 24 08:49 shell.py

0

>>> shutil.make_archive("www","gztar",root_dir="/root/11")

'/root/www.tar.gz'

>>> os.system("ls -lh ")

總用量 12K

drwxr-xr-x 3 root root 15 11月 22 15:52 11

-rwxr--r-- 1 root root 5.0K 7月 24 08:49 shell.py

-rw-r--r-- 1 root root 184 11月 22 15:54 www.tar.gz

0

將 /root/11 下的文件打包放置 /srv目錄

import shutil

os.system("ls -lh /srv")

總用量 0

0

shutil.make_archive("/srv/www","gztar",root_dir="/root/11")

'/srv/www.tar.gz'

os.system("ls -lh /srv")

總用量 4.0K

-rw-r--r-- 1 root root 183 11月 22 15:59 www.tar.gz

0

shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:

zipfile 壓縮解壓

import zipfile

#壓縮

import zipfile

z = zipfile.ZipFile("shell.zip","w")

z.write("shell.py")

z.close()

os.system("ls -lh ")

#解壓

z = zipfile.ZipFile("shell.zip","r")

z.extractall() #可設置解壓地址

z.close()

tarfile 壓縮解壓

#壓縮

import tarfile

tar = tarfile.open("tar.tar","w")

tar.add("/root/shell.zip",arcname="shell.zip")

tar.add("/root/www.tar.gz",arcname="www.tar.gz")

tar.close()

#解壓

tar = tarfile.open("tar.tar","r")

tar.extractall() #可設置解壓地址

tar.close()

總結

以上是生活随笔為你收集整理的python创建模块文件夹_python文件、文件夹、压缩包处理模块-shutil模块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。