【发布时间】:2017-11-02 20:37:43
【问题描述】:
我正在浏览计算机列表并在某个目录和子目录中搜索文件扩展名为 .exe.config 的任何内容
我想获取那些以 .exe.config 结尾的文件并将它们放在相同结构的备份目录中。所以这将递归地完成。
例如,“main”是主目录。 “main”中有几个子目录,分别称为“1”、“2”、“3”等
main
\
\-----------
\ \ \
1 2 3
在每个编号的目录中,都会有一个扩展名为 .exe.config 的文件
我要获取主目录名、子目录名和 *.exe.config 文件。然后将它们放在具有相同结构的备份“主”目录中。
在这些编号的目录中还有其他文件,但我想忽略它们。
我无法递归复制所有内容。这是我一直在测试的代码。计算机列表放在 serverlist.txt 中
import os
import shutil
import fileinput
def copy_names(servername):
source = r'//' + servername + '/c$/Program Files (x86)/Main/'
dest = r'//' + servername + '/c$/' + servername + '/Main/'
for root, dirs, files in os.walk(source):
for file in files:
if file.endswith('.exe.config'):
try:
os.makedirs(dest, exist_ok=True)
shutil.copyfile(os.path.join(source, file), os.path.join(dest, file))
print ("\n" + servername + " " + file + " : Copy Complete")
except:
print (" Directory you are copying to does not exist.")
def start():
os.system('cls' if os.name == 'nt' else 'clear')
array = []
with open("serverlist.txt", "r") as f:
for servername in f:
copy_names(servername.strip())
# start program
start()
【问题讨论】:
标签: python python-3.x