大家晚上好,
我目前正在尝试学习Python。我今天在工作中想到了一个非常酷的想法,让我和我的支持团队的生活变得更容易,我认为这将是学习Python的一个很好的入门项目。我在一家软件公司工作,我们当前的版本有大量的补丁需要在初始安装后安装。我们目前正在手动安装这些补丁,所以我正在尝试制作我自己的安装程序,它将直接获取补丁文件并覆盖现有的文件。
我最初使用的是shutil.move()
,在修复代码的过程中,我意识到这是从我的目录中取出文件,并将它们“移动”到新的目录(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
发布于 2020-10-28 07:46:53
我能够弄清楚这一点,我想我会把答案贴出来,帮助其他有类似问题的人。
我遇到的问题是由于我的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)
https://stackoverflow.com/questions/64491975
复制相似问题