![](/img/trans.png)
[英]IsADirectoryError: [Errno 21] Is a directory: '/' while copying files from one folder to another existing folder Python
[英]Copying files from one directory to another python
您好,有人知道如何將文件從一個目錄復制到另一個目錄嗎? 我已經使用了“ shutil.copy2”,它可以工作並將副本復制到指定的輸出。
但是,我的目標是能夠將文件從一個目錄復制到另一個目錄,從而允許用戶通過名稱指定要復制的文件。 不必每次都輸入目錄路徑。
思考過程:由於我指定了文件目錄。 使用raw_input用戶可以通過某種方式指定要從指定目錄復制的文件。 發表我的代碼以供參考。 #請不要BS評論我是編碼新手,只是想學習。
#----------------------------------------------------------------------------------------------------------------#
# These params will be used for specifying which template you want to copy and where to output
#----------------------------------------------------------------------------------------------------------------#
'''Load file from x directory into current working directory '''
#PullTemplate: Specify which template you want to copy, by directory path
TemplateRepo = ("/home/hadoop/BackupFolders/Munge_Stage_Templates/Templates")
user_input = raw_input("which file do you want to pull:")
#OutputTemplate: Let's you specify where you want to output the copied template.
#Originally set to your current working directory (u".")
OutputTemplate = (u".")
#----------------------------------------------------------------------------------------------------------------#
# STATIC CODE: Do not alter "Just Run!"
#----------------------------------------------------------------------------------------------------------------#
shutil.copy2(TemplateRepo, OutputTemplate)
因此,您需要使它們只是輸入文件名還是在詢問如何獲取相對路徑?
編輯
好的,這樣對於初學者來說,現在和將來使您的生活變得更輕松,函數將使您的生活更加輕松。 其次,查看docs https://docs.python.org/3/library/functions.html#open優質資源,您要做的大多數事情都會告訴您如何使用。 您必須使用絕對路徑保存文件,但是可以使用相對路徑打開文件。
shutil.copy2()
的第一個選項應該是文件而不是目錄,因此您需要從輸入中添加TemplateRepo和文件名:
TemplateRepo = ("/home/hadoop/BackupFolders/Munge_Stage_Templates/Templates")
user_input = raw_input("which file do you want to pull:")
OutputTemplate = (u".")
InputFile = TemplateRepo + '/' + user_input
shutil.copy2(InputFile, OutputTemplate)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.