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

歡迎訪問 生活随笔!

生活随笔

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

python

python中shutil.copyfile的用法_Python shutil.copyfile()用法及代码示例

發布時間:2025/3/19 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中shutil.copyfile的用法_Python shutil.copyfile()用法及代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python中的Shutil模塊提供了許多對文件和文件集合進行高級操作的功能。它屬于Python的標準實用程序模塊。此模塊有助于自動執行文件和目錄的復制和刪除過程。

shutil.copyfile()Python中的方法用于將源文件的內容復制到目標文件。文件的元數據未復制。源和目標必須代表一個文件,并且目標必須是可寫的。如果目標已經存在,則將其替換為源文件,否則將創建一個新文件。如果源和目標表示相同的文件,則將引發SameFileError異常。

用法: shutil.copyfile(source, destination, *, follow_symlinks = True)

參數:

source:代表源文件路徑的字符串。

destination:代表目標文件路徑的字符串。

follow_symlinks(可選):此參數的默認值為True。如果為False且source表示符號鏈接,則將創建一個新的符號鏈接,而不是復制文件。

Note:參數列表中的“ *”表示以下所有參數(此處為“ follow_symlinks”)僅是關鍵字參數,可以使用其名稱(而不是位置參數)來提供。

返回類型:此方法返回一個表示新創建文件路徑的字符串。

代碼1:使用shutil.copyfile()方法將文件從源復制到目標

# Python program to explain shutil.copyfile() method

# importing os module

import os

# importing shutil module

import shutil

# path

path = '/home/User/Documents'

# List files and directories

# in '/home/User/Documents'

print("Before copying file:")

print(os.listdir(path))

# Source path

source = "/home/User/Documents/file.txt"

# Destination path

destination = "/home/User/Documents/file(copy).txt"

# Copy the content of

# source to destination

dest = shutil.copyfile(source, destination)

# List files and directories

# in "/home / User / Documents"

print("After copying file:")

print(os.listdir(path))

# Print path of newly

# created file

print("Destination path:", dest)

輸出:

Before copying file:

['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'copy.cpp']

After copying file:

['hrithik.png', 'test.py', 'sample.txt', 'file.text', 'file(copy).txt', 'copy.cpp']

Destination path: /home/User/Documents/file(copy).txt

代碼2:使用shutil.copyfile()方法時可能出現的錯誤

# Python program to explain shutil.copyfile() method

# importing shutil module

import shutil

# If the source and destination

# represents the same file

# 'SameFileError' exception

# will be raised

# If the destination is

# is directory then

# 'IsADirectoryError' exception

# will be raised

# If the destination is

# not writable

# 'PermissionError' exception

# will be raised

# Source path

source = "/home/User/Documents/file.txt"

# Destination path

destination = "/home/User/Documents/file.txt"

# Copy the content of

# source to destination

shutil.copyfile(source, destintion)

輸出:

Traceback (most recent call last):

File "copy.py", line 31, in

shutil.copyfile(source, destination)

File "/usr/lib/python3.6/shutil.py", line 104, in copyfile

raise SameFileError("{!r} and {!r} are the same file".format(src, dst))

shutil.SameFileError: '/home/User/Documents/file.txt' and '/home/User/Documents/file.txt'

are the same file

代碼3:使用Shutil.copyfile()方法處理錯誤

# Python program to explain shutil.copyfile() method

# importing shutil module

import shutil

# Source path

source = "/home/User/Documents/file.txt"

# Destination path

destination = "/home/User/Documents"

# Copy the content of

# source to destination

try:

shutil.copyfile(source, destination)

print("File copied successfully.")

# If source and destination are same

except shutil.SameFileError:

print("Source and destination represents the same file.")

# If destination is a directory.

except IsADirectoryError:

print("Destination is a directory.")

# If there is any permission issue

except PermissionError:

print("Permission denied.")

# For other errors

except:

print("Error occurred while copying file.")

輸出:

Destination is a directory.

總結

以上是生活随笔為你收集整理的python中shutil.copyfile的用法_Python shutil.copyfile()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

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