我一直在制作下面的备份脚本,将文件从一台服务器复制到另一台服务器,有几个要求。代码如下。有帮助吗?我有一个示例测试输入。
import arcpy # Import arcpy module
from arcpy import env # Import env module from arcpy
import os # Import os module
import logging # Import logging module
import shutil # Import shutil module
inFile = "G:\data" # Input specified
outFile = "D:\data" # Output specified
backup = "D:\backup" # Intermediate backup file
logdirectory = "S:\backup_directory\log\*.log"
log = logging.getLogger("G:\data") # Creates logger instance
hdlr = logging.FileHandler("G:\data\*.log") # Creates FileHandler
# Creates Formatter
formatter = logging.Formatter("%(asctime)s %(levelName)s %(message)s")
hdlr.setFormatter(formatter) # Attaches Formatter to FileHandler
logger.addHandler(hdlr) # Attaches FileHandler to Logger
logger.setLevel(logging.NOTE) # Sets logger note
inFile = open("G:\users\tchaney\TestInputFile.txt", "r")
# Opens the data file to be read, and assigns it to the variable, inFile
outFile = open("G:\users\tchaney\TestOutputFile.txt", "w")
# Opens a data output file to be written to, and assigns it to outFile
shutil.copyfile(logger, "D:\data\copy_[fileDate].log")
# Copy logfile to archive as "copy_[filedate].log"
#for "*.log" in "logdirectory"
# For loop to check current date, and returns files 7 days old or less
#return(currDate - fileDate) <= 7
#logger.note("Log files updated")
# same as file.readline() - reads one line at a time into a string variable,
# loop to read, stop service, write, start service.
for inLine in inFile:
# Split each line, while removing the comma, and store each field
service, srcFile, destFile = inLine.split(",")
# If source exists, for each service in the array, stop the service
if os.path.exists("srcFile"):
for service in inFile[:]:
os.system("AGSSOM.EXE -x")
# If destination variable exists, copy destination to backup, and remove destination
if os.path.exists("destFile"):
#arcpy.env.overwriteOutput = True (needed with shutil?)
shutil.copyfile("G:\data\*", "D:\backup\*")
os.remove("destFile")
else:
logger.note("Filename did not exist prior to copy")
shutil.copyfile("srcFile", "destFile") # Copy source to destination
# If destination does not exist, copy backup to destination, and add logger note
if not os.path.exists("destFile"):
shutil.copyfile("D:\backup\*", "D:\data\*")
logger.note("[filename] copied from backup")
for service in inFile[:]: # For each service in the array, start the service
os.system("AGSSOM.EXE -s")
else: # If the source is not found, add logger note
logger.note("Source not found")
inFile.close() # Close input file
outFile.close() # Close output file
答案 0 :(得分:2)
一个主要错误是向shutil.copyfile
提供glob
参数。
shutil.copyfile("D:\backup\*", "D:\data\*")
应该阅读
import glob
for fn in glob.glob("D:\\backup\\*"):
shutil.copyfile(fn, "D:\\data")
同样,以下几行很奇怪:
hdr = logging.FileHandler("G:\data\*.log")
它可能应该是
的内容hdr = logging.FileHandler("G:\\data\\my_application.log")