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

歡迎訪問 生活随笔!

生活随笔

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

python

将所有文件从目录复制到Python中的另一个目录

發(fā)布時間:2025/3/11 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将所有文件从目录复制到Python中的另一个目录 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

shutil (shell utilities) module, provides option to copy the files recursively from src to dst.

shutil(shell實用程序)模塊 ,提供了將文件從src遞歸復制到dst的選項 。

The syntax to copy all files is:

復制所有文件的語法為:

shutil.copytree(src, dst, symlink=False, ignore=None, copy_function=copy2, ignore_dangling_symlins=False)

Here,

這里,

  • src - source directory from where the files shall be copied.

    src-從中復制文件的源目錄。

  • dst - destination to where the files shall be copied.

    dst-將文件復制到的目的地。

  • If symlinks are True,

    如果符號鏈接為True,

    • Move the file with new name
    • Copy and rename file using "os" module

移動并重命名文件 (Move and Rename file)

shutil.move(src, dst, copy_function=copy2)

Listing command:

清單命令:

-bash-4.2$ lspython_samples test test.txt test.txt.copy test.txt.copy2

Code:

碼:

# importing modules import os import shutilsrc_dir = os.getcwd() # gets the current working dir dest_file = src_dir + "/python_samples/test_renamed_file.txt" shutil.move('test.txt',dest_dir)print(os.listdir()) # the file 'test.txt' is moved from # src to dest with a new nameos.chdir(dest_dir) print(os.listdir()) # list of files in dest

Output

輸出量

'/home/sradhakr/Desktop/my_work/python_samples/ test_renamed_file.txt’ ['python_samples', 'test', 'test.txt.copy', 'test.txt.copy2'] ['.git', '.gitignore', 'README.md', 'src', ' test_renamed_file.txt']

使用os和shutil模塊進行復制和重命名 (Copy and rename using os and shutil module)

In this approach we use the shutil.copy() function to copy the file and os.rename() to rename the file.

在這種方法中,我們使用shutil.copy()函數(shù)復制文件,并使用os.rename()重命名文件。

# Importing the modules import os import shutilsrc_dir = os.getcwd() #get the current working dir print(src_dir)# create a dir where we want to copy and rename dest_dir = os.mkdir('subfolder') os.listdir()dest_dir = src_dir+"/subfolder" src_file = os.path.join(src_dir, 'test.txt.copy2') shutil.copy(src_file,dest_dir) #copy the file to destination dirdst_file = os.path.join(dest_dir,'test.txt.copy2') new_dst_file_name = os.path.join(dest_dir, 'test.txt.copy3')os.rename(dst_file, new_dst_file_name)#rename os.chdir(dest_dir)print(os.listdir())

Output

輸出量

/home/user/Desktop/my_work ['python_samples', 'subfolder', 'test', 'test.txt.copy2', 'test.txt.copy_1'] '/home/sradhakr/Desktop/my_work/subfolder/test.txt.copy2' ['test.txt.copy3']

Summary: shutil (shell utilities module ) is a more pythonic way to perform the file or directory copy , move or rename operations.

簡介: shutil (shell實用程序模塊)是執(zhí)行文件或目錄復制,移動或重命名操作的更Python方式。

Reference: https://docs.python.org/3/faq/windows.html

參考: https : //docs.python.org/3/faq/windows.html

翻譯自: https://www.includehelp.com/python/copy-all-files-from-a-directory-to-another.aspx

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的将所有文件从目录复制到Python中的另一个目录的全部內容,希望文章能夠幫你解決所遇到的問題。

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