日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

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

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

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

The syntax to copy all files is:

復(fù)制所有文件的語(yǔ)法為:

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-從中復(fù)制文件的源目錄。

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

    dst-將文件復(fù)制到的目的地。

  • If symlinks are True,

    如果符號(hào)鏈接為True,

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

移動(dòng)并重命名文件 (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模塊進(jìn)行復(fù)制和重命名 (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ù)復(fù)制文件,并使用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.

簡(jiǎn)介: shutil (shell實(shí)用程序模塊)是執(zhí)行文件或目錄復(fù)制,移動(dòng)或重命名操作的更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)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。