Python 修补程序移动:文件备份和替换
尝试从包含错误修复的单个源目录复制文件时出错:Python 修补程序移动:文件备份和替换,python,python-3.x,Python,Python 3.x,尝试从包含错误修复的单个源目录复制文件时出错:/home/saurabh/testbed/patch\u dir/到多个目标目录:app\u dir\u 1和app\u dir\u 2 这两个目录都是彼此的精确副本 脚本执行以下操作:- 将文本文件中的行读取到列表中。每行包含一个组件的名称。在本例中:['file1.class',file2.html] 从特定目录开始,在每个索引处递归搜索值: /home/saurabh/testbed/dest_dir/ 通过将ddMonyyyy附加到这些文件
/home/saurabh/testbed/patch\u dir/
到多个目标目录:app\u dir\u 1
和app\u dir\u 2
这两个目录都是彼此的精确副本
脚本执行以下操作:-
/home/saurabh/testbed/dest_dir/
/home/saurabh/testbed/patch\u dir/
到先前进行备份的目录
/home/saurabh/testbed/dest\u dir/
|--应用程序目录1
|--file1.class
|--file2.html
|--file3.jsp
|--file4.xml
|--副处长
|--应用程序目录2
|--file1.class
|--file2.html
|--file3.jsp
|--file4.xml
|--副处长
|--其他目录
/home/saurabh/testbed/patch_dir/
|--file1.class
|--file2.html
下面是我的代码:
#!/usr/bin/env python
import os
import fnmatch
import datetime
import shutil
with open('filenames.txt') as f:
content = f.readlines()
content = [x.strip() for x in content]
print('File contents:')
print(content)
suffix = datetime.datetime.now().strftime("_%d%b%Y")
approot = '/home/saurabh/testbed/dest_dir/'
source_dir = '/home/saurabh/testbed/patch_dir/'
dir_list = []
print('\n' + 'Renaming files present at:')
for root, dirs, files in os.walk(approot):
for file_list in content:
for filename in fnmatch.filter(files, file_list):
print(os.path.join(root, filename))
dir_list.append(root)
current_file = os.path.join(root, filename)
backup_file = os.path.join(root, filename + suffix)
os.rename(current_file, backup_file)
print("\n" + "Backup of all files complete!")
print('Backup of ' + str(len(dir_list)) + ' files taken recursively')
print('Number of files mentioned in text file: ' + str(len(content)) + '\n')
# 2 instances in UAT
# 12 instances in PROD
if (2*len(content)) == len(dir_list):
print("Retrofitted components will be copied to their respective directories")
for dst_ind in range(0, len(dir_list)):
if filename in fnmatch.filter(files, file_list):
print(source_dir + content[dst_ind] + "\t" + dir_list[dst_ind])
#shutil.copy2(source_dir+content[dst_ind], dir_list[dst_ind])
File contents:
['file1.class', 'file2.html']
Renaming files present at:
/home/saurabh/testbed/dest_dir/app_dir_1/file1.class
/home/saurabh/testbed/dest_dir/app_dir_1/file2.html
/home/saurabh/testbed/dest_dir/app_dir_2/file1.class
/home/saurabh/testbed/dest_dir/app_dir_2/file2.html
Backup of all files complete!
Backup of 4 files taken recursively
Number of files mentioned in text file: 2
Retrofitted components will be copied to their respective directories
/home/saurabh/testbed/patch_dir/file1.class /home/saurabh/testbed/dest_dir/app_dir_1
/home/saurabh/testbed/patch_dir/file2.html /home/saurabh/testbed/dest_dir/app_dir_1
/home/saurabh/testbed/patch_dir/file1.class /home/saurabh/testbed/dest_dir/app_dir_2
/home/saurabh/testbed/patch_dir/file2.html /home/saurabh/testbed/dest_dir/app_dir_2
复制文件时出现以下错误(4)。
感谢您对修复代码的任何帮助。已经解决了
#!/usr/bin/env python
import os
import fnmatch
import datetime
import shutil
with open('filenames.txt') as f:
content = f.readlines()
content = [x.strip() for x in content]
print('File contents:')
print(content)
suffix = datetime.datetime.now().strftime("_%d%b%Y")
approot = '/Users/saurabhm/Desktop/Python/testbed/dest_dir/'
source_dir = '/Users/saurabhm/Desktop/Python/testbed/patch_dir/'
dir_list = []
print('\n' + 'Renaming files present at:')
for root, dirs, files in os.walk(approot):
for file_list in content:
for filename in fnmatch.filter(files, file_list):
print(os.path.join(root, filename))
dir_list.append(root)
current_file = os.path.join(root, filename)
backup_file = os.path.join(root, filename + suffix)
#print(current_file, backup_file)
os.rename(current_file, backup_file)
#print(source_dir + filename + "\t" + root)
shutil.copy2(source_dir + filename, root)
os.chmod(root + '/' + filename, 0o750)
print("\n" + "Backup of all files complete!")
print('Backup of ' + str(len(dir_list)) + ' files taken recursively')
print('Number of files mentioned in text file: ' + str(len(content)) + '\n')
一旦通过电子邮件收到修补组件,它们将被复制到相应服务器中的目录中。所有这些文件都合并到一个目录中(patch_dir)
现有文件(具有相同名称且存在于“dest_dir”中)的备份会在任何地方进行,然后将每个文件从“patch_dir”复制到“dest_dir”中的目录中,获取备份的位置。您遇到的错误意味着您试图访问
列表
中超出列表长度的元素。我不是说这会起作用,但是试着print(source_dir+content[dst_ind-1]+“\t”+dir_list[dst_ind-1])
它与print(source_dir+content[dst_ind-2]+“\t”+dir_list[dst_ind-2]一起工作。
。我创建了一个类似的应用程序目录3
,但使用[dst\u ind-3]
不起作用。我们的目标是在有12个实例的生产环境中做到这一点:——app\u diru\u 1
,app\u diru\u 2
,…,app\u diru 11
,以及app\u diru 12
您试图同时迭代内容
和diru列表
,但根据定义(在if
语句中),content
的成员数量只有dir\u list
的一半,因此当然您将耗尽content
的成员并生成该错误。是的,我理解这一点。我想不透。我之前有一个不同的想法,我的输出是app_diru_1
打印4次,然后是app_diru_2
打印4次(都在最后)
#!/usr/bin/env python
import os
import fnmatch
import datetime
import shutil
with open('filenames.txt') as f:
content = f.readlines()
content = [x.strip() for x in content]
print('File contents:')
print(content)
suffix = datetime.datetime.now().strftime("_%d%b%Y")
approot = '/Users/saurabhm/Desktop/Python/testbed/dest_dir/'
source_dir = '/Users/saurabhm/Desktop/Python/testbed/patch_dir/'
dir_list = []
print('\n' + 'Renaming files present at:')
for root, dirs, files in os.walk(approot):
for file_list in content:
for filename in fnmatch.filter(files, file_list):
print(os.path.join(root, filename))
dir_list.append(root)
current_file = os.path.join(root, filename)
backup_file = os.path.join(root, filename + suffix)
#print(current_file, backup_file)
os.rename(current_file, backup_file)
#print(source_dir + filename + "\t" + root)
shutil.copy2(source_dir + filename, root)
os.chmod(root + '/' + filename, 0o750)
print("\n" + "Backup of all files complete!")
print('Backup of ' + str(len(dir_list)) + ' files taken recursively')
print('Number of files mentioned in text file: ' + str(len(content)) + '\n')