将 Python 中的文件复制到现有目录(允许覆盖)

Copying files in Python to an existing directory (allow overwriting)

大家晚上好,

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

我最初使用 shutil.move(),在修复代码的过程中,我没有意识到这是从我的 DIR 中获取文件并将它们“移动”到新目录(duh)。 我不希望发生这种情况,所以我将其从 shutil.copy() 更改为 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

我能够解决这个问题,我想我会 post 帮助遇到类似问题的其他人的答案。

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

我不是 100% 确定这是 end-all 修复,但就我目前的情况而言,这对我来说没有任何错误。

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)