将所有文件从目录复制到Python中的另一个目录
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.copy2Code:
碼:
# 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 destOutput
輸出量
'/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ì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的将所有文件从目录复制到Python中的另一个目录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: golang rsa密钥_如何在Gola
 - 下一篇: 顶级Javaer,常用的 14 个类库