首页 > 解决方案 > 将 Python 中的文件复制到现有目录(允许覆盖)

问题描述

大家晚上好

我目前正在尝试学习 Python。我今天在工作中想到了一个非常酷的想法,让我和我的支持团队的生活更轻松,我认为这将是学习 Python 的一个很好的入门项目。我在一家软件公司工作,我们当前的版本有大量补丁需要在初始安装后安装。我们目前正在手动放置这些补丁,所以我正在尝试制作我自己的安装程序,它会出去获取补丁文件并覆盖当前文件。

我最初正在使用shutil.move()并且在修复代码的过程中,我没有意识到这是从我的 DIR 中获取文件并将它们“移动”到新的目录(duh)。我不希望这种情况发生,所以我将其更改为shutil.copy(),现在我收到错误和权限错误,甚至以管理员身份运行 PyCharm。

我希望你能看看我的代码并帮助我。先感谢您!

# Importing the modules

import os
import shutil

src_dir = os.getcwd()

# print("'Copying Files from' + 'tt_source_dir' + 'to' 'tt_target_dir'")
# print(os.listdir())
# not working want it to work

# Moving the TT2018 Folder

tt_source_dir = src_dir + "/TT2018"
tt_target_dir = 'C:\\test'

file_names = os.listdir(tt_source_dir)

for file_names in file_names:
    shutil.copy(os.path.join(tt_source_dir, file_names), tt_target_dir)

# Moving the WebPortal Folder

wp_source_dir = src_dir + "/WebPortal"
wp_target_dir = 'C:\\test\Web Portals'

file_names = os.listdir(wp_source_dir)

for file_names in file_names:
    shutil.copy(os.path.join(wp_source_dir, file_names), wp_target_dir)

# Moving the SupClient Folder

sc_source_dir = src_dir + "/SupClientTimeZonePatch"
sc_target_dir = 'C:\\test\Client'

file_names = os.listdir(sc_source_dir)

for file_names in file_names:
    shutil.copy(os.path.join(sc_source_dir, file_names), sc_target_dir)

# Moving the DBSyncAdmin Folder

dba_source_dir = src_dir + "/DBSyncAdmin"
dba_target_dir = 'C:\\TimeTrakCONNECT\DBSyncServerAdmin'

file_names = os.listdir(dba_source_dir)

for file_names in file_names:
    shutil.copy(os.path.join(dba_source_dir, file_names), dba_target_dir)

这些是我收到的以下错误:

"C:\Users\Chuck\venv\TimeTrak Patches Installer\Scripts\python.exe" "C:/Users/Chuck/PycharmProjects/TimeTrak Patches Installer/main.py"
Traceback (most recent call last):
  File "C:/Users/Chuck/PycharmProjects/TimeTrak Patches Installer/main.py", line 30, in <module>
    shutil.copy(os.path.join(wp_source_dir, file_names), wp_target_dir)
  File "C:\Users\Chuck\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 415, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\Chuck\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 261, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Chuck\\PycharmProjects\\TimeTrak Patches Installer/WebPortal\\bin'

Process finished with exit code 1

标签: pythondirectorycopy

解决方案


我能够弄清楚这一点,我想我会发布答案以帮助其他遇到此类问题的人。

我遇到的问题是由于我的 WebPortal 文件夹包含目录 + 文件。我需要使用 os.walk。我遇到的另一个问题是,如果我再次运行该程序,它不会覆盖文件,因此我使用 os.remove 删除了我试图替换的文件,同时保持该目录中的所有其他文件完好无损。

我不是 100% 确定这是最终的解决方案,但就我目前的坐姿而言,这对我来说没有错误。

WebPortals 文件夹包含目录和文件,而 TT2018 文件夹仅包含文件。我可以让那个副本更简单。

# Importing the modules

import os
import shutil

src_dir = os.getcwd()

# print("'Copying Files from' + 'source_dir' + 'to' 'target_dir'")
# print(os.listdir())
# not working want it to work

# Moving the TT2018 Folder

tt_source_dir = src_dir + "\TT2018"
tt_target_dir = 'C:\\test'

file_names = os.listdir(tt_source_dir)

for file_name in file_names:
    shutil.copy2(os.path.join(tt_source_dir, file_name), tt_target_dir)



# Moving the WebPortal Folder

wp_source_dir = src_dir + "\WebPortal"
wp_target_dir = 'C:\\test\\WebPortal\\'

for source_dir, dirs, file_names in os.walk(wp_source_dir):
    target_dir = source_dir.replace(wp_source_dir, wp_target_dir, 1)

    for file_name in file_names:
        source_file = os.path.join(source_dir, file_name)
        target_file = os.path.join(target_dir, file_name)
        if os.path.exists(target_file):
            # in case of the source and target are the same file
            if os.path.samefile(source_file, target_file):
                continue
            os.remove(target_file)
        shutil.copy2(source_file, target_dir)

推荐阅读