當前位置:首頁 » 編程語言 » python的shutilmove

python的shutilmove

發布時間: 2022-06-12 09:22:44

python shutil.問題 為什麼無法傳值

shutil.file( src, dst) 從源src復制到dst中去。當然前提是目標地址是具備可寫許可權。拋出的異常信息為IOException. 如果當前的dst已存在的話就會被覆蓋掉
shutil.move( src, dst) 移動文件或重命名
shutil.mode( src, dst) 只是會復制其許可權其他的東西是不會被復制的
shutil.stat( src, dst) 復制許可權、最後訪問時間、最後修改時間
shutil.( src, dst) 復制一個文件到一個文件或一個目錄!

Ⅱ python shutil.move shutil.哪個快

文件的話shutil.move快,目錄的話shutil.
由於shutil.move移動目錄時也是進行操作,然後再刪除

Ⅲ python裡面shutil是什麼

shutil 是高級的文件,文件夾壓縮包處理模塊。

1.shutil.fileobj(fsrc, fdst[, length])

將文件內容拷貝到另一個文件中

import shutil
shutil.fileobj(open('old.xml','r'), open('new.xml', 'w'))

2.shutil.file(src, dst)

拷貝文件

shutil.file('f1.log', 'f2.log')

3.shutil.mode(src, dst)

僅拷貝許可權。內容、組、用戶均不變

shutil.mode('f1.log', 'f2.log')

4.shutil.stat(src, dst)

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

shutil.stat('f1.log', 'f2.log')

5.shutil.(src, dst)

拷貝文件和許可權

shutil.('f1.log', 'f2.log')

6.shutil.2(src, dst)

拷貝文件和狀態信息

shutil.2('f1.log', 'f2.log')

7.shutil.ignore_patterns(*patterns)

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

遞歸的去拷貝文件夾

shutil.tree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.tree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

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

遞歸的去刪除文件

shutil.rmtree('folder1')

9.shutil.move(src, dst)

遞歸的去移動文件,它類似mv命令,其實就是重命名。

shutil.move('folder1', 'folder3')

10.shutil.make_archive(base_name, format,...)

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

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

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

  • format: 壓縮包種類,「zip」, 「tar」, 「bztar」,「gztar」

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

  • owner: 用戶,默認當前用戶

  • group: 組,默認當前組

  • logger: 用於記錄日誌,通常是logging.Logger對象


Ⅳ python怎樣批量修改文件名

os.rename(a,b)是把a重命名為b(名字帶路徑)
os.listdir(path)以列表形式獲取路徑下文件(包括文件夾),os.listdir(「.」)是當前文件夾
然後。。。想怎麼重命名就怎麼寫程序。。。遍歷,(檢查,)修改。。。
如果待重命名的文件和py程序在一個文件夾會很方便,重命名不需要路徑,像os.rename(「1.txt」,「2.txt」)
如果重命名的規則沒法表述,那隻能自己一個個改了。。。

Ⅳ python 怎麼將輸入目錄內的文件拷貝至另一個目錄的同名文件夾

這是最近寫的一個類似代碼,你拿去改改
import shutil
import os
import logging
import sys

logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

def cp_or_mv2(src_file, des_dir, is_):
print(src_file, des_dir)
if os.path.isfile(src_file):
logger.info(f'from file {src_file}')
if is_:
shutil.2(src_file, des_dir)
logger.info(f' to {des_dir}')
else:
des_file = os.path.join(des_dir, src_file)
shutil.move(src_file, des_file)
logger.info(f'move to {des_file}')
else:
logger.info(f'from dir {src_file}')
des_dir_level1 = os.path.join(des_dir, src_file)
shutil.tree(src_file, des_dir_level1, dirs_exist_ok=True)
logger.info(f'to {des_dir_level1}')
if not is_:
shutil.rmtree(src_file)
logger.info(f'deleted {src_file}')

def process_files_in_txt(txt_file, src_dir, des_dir, is_=True):
os.chdir(src_dir)
with open(txt_file, 'r', encoding='utf8', errors='ignore') as f:
for line in f.readlines():
src_file = line.strip()
# logger.info(src_file)
if os.path.exists(src_file):
cp_or_mv2(src_file, des_dir, is_)
else:
logger.warning(f'{src_file} missing!')

if __name__ == '__main__':
process_files_in_txt(r"D:\D\需要拷貝.txt", # 哪些文件(夾)
r"D:\D\Desktop", # 從哪個文件夾
r"D:\D\新建文件夾", # 到哪個文件夾
is_=False) # True復制,False剪切

Ⅵ python shuutil.move對異常的處理

可以先判斷再做處理。用os.path.isfile函數

Ⅶ python shutil模塊函數file和的區別

shutil.file( src, dst) 從源src復制到dst中去。當然前提是目標地址是具備可寫許可權。拋出的異常信息為IOException. 如果當前的dst已存在的話就會被覆蓋掉
shutil.move( src, dst) 移動文件或重命名
shutil.mode( src, dst) 只是會復制其許可權其他的東西是不會被復制的
shutil.stat( src, dst) 復制許可權、最後訪問時間、最後修改時間
shutil.( src, dst) 復制一個文件到一個文件或一個目錄
shutil.2( src, dst) 在上的基礎上再復制文件最後訪問時間與修改時間也復制過來了,類似於cp –p的東西
shutil.2( src, dst) 如果兩個位置的文件系統是一樣的話相當於是rename操作,只是改名;如果是不在相同的文件系統的話就是做move操作
shutil.tree( olddir, newdir, True/Flase)
把olddir拷貝一份newdir,如果第3個參數是True,則復制目錄時將保持文件夾下的符號連接,如果第3個參數是False,則將在復制的目錄下生成物理副本來替代符號連接
shutil.rmtree( src ) 遞歸刪除一個目錄以及目錄內的所有內容

Ⅷ python的shutil.move是不是很慢

是會慢很多
shutil.move的代碼,如果src是dir,dst存在也是dir,
則先復制再刪除src,所以會很慢,操作系統中是直接移動所以快

def move(src, dst):
"""Recursively move a file or directory to another location.

If the destination is on our current filesystem, then simply use
rename. Otherwise, src to the dst and then remove src.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.

"""

try:
os.rename(src, dst)
except OSError:
if os.path.isdir(src):
if destinsrc(src, dst):
raise Error, "Cannot move a directory '%s' into itself '%s'."
% (src, dst)
tree(src, dst, symlinks=True)
rmtree(src)
else:
2(src,dst)
os.unlink(src)

Ⅸ Python 中怎樣刪除zip壓縮文件夾中某個文件


PyZipFile
方法很簡陋。
是把
zip壓縮包裡面的文件,除了要刪除的文件以外,逐個讀取出來,保存到新文件中。最後用新文件覆蓋舊文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
coding=utf-8
import
zipfile
import
your_delet_file="你要刪除的文件名"
old_zipfile='archive.zip'
#新文件
new_zipfile='archve_new.zip'
#新文件
zin
=
zipfile.ZipFile
(old_zipfile,
'r')
#讀取對象
zout
=
zipfile.ZipFile
(new_zipfile,
'w')
#被寫入對象
for
item
in
zin.infolist():
buffer
=
zin.read(item.filename)
if
(item.filename!=
your_delet_file):
#剔除要刪除的文件
zout.writestr(item,
buffer)
#把文件寫入到新對象中
zout.close()
zin.close()
#用新文件覆蓋舊文件
shutil.move(new_zipfile,old_zipfile)

熱點內容
自己的電腦怎麼搭建手游伺服器端 發布:2025-02-07 17:21:44 瀏覽:45
怎樣修改蘋果密碼怎麼辦 發布:2025-02-07 17:15:44 瀏覽:714
電腦一般怎麼連接伺服器 發布:2025-02-07 17:12:55 瀏覽:489
ftp用ie打開文件 發布:2025-02-07 17:07:42 瀏覽:269
android列表顯示 發布:2025-02-07 17:01:19 瀏覽:64
芒果tv緩存的視頻在哪個文件里 發布:2025-02-07 16:45:05 瀏覽:815
php郵件群發 發布:2025-02-07 16:45:05 瀏覽:613
mysql資料庫基本語句 發布:2025-02-07 16:41:48 瀏覽:251
醫院門禁密碼多少 發布:2025-02-07 16:41:43 瀏覽:528
伺服器遭美國ip攻擊簽名 發布:2025-02-07 16:22:48 瀏覽:547