我希望构建一个Python脚本,该脚本可以根据一个列表将文件/目录从一个目录移动到另一个目录。
以下是我已经拥有的内容:
import os, shutil
// Read in origin & destination from secrets.py Readlines() stores each line followed by a '/n' in a list
f = open('secrets.py', 'r')
paths = f.readlines()
// Strip out those /n
srcPath = paths[0].rstrip('\n')
destPath = paths[1].rstrip('\n')
// Close stream
f.close()
// Empty destPath
for root, dirs, files in os.walk(destPath, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
// Copy & move files into destination path
for srcDir, dirs, files in os.walk(srcPath):
destDir = srcDir.replace(srcPath, destPath)
if not os.path.exists(destDir):
os.mkdir(destDir)
for file in files:
srcFile = os.path.join(srcDir, file)
destFile = os.path.join(destDir, file)
if os.path.exists(destFile):
os.remove(destFile)
shutil.copy(srcFile, destDir)
secrets.py文件包含了src/dest路径。
目前,这将传输所有文件/目录。我想读入另一个文件,允许您指定要传输的文件(而不是制作“忽略”列表)。
tar
或rsync
来完成这个任务吗?它们允许你指定包含或排除文件的文件列表。 - Pedro Romano