这是一个Python3脚本,该脚本使用保存时附加到原始文件名的时间戳来执行VMS(例如自动文件版本控制)的VMS。
我在脚本中添加了一堆注释,并在ubuntu计算机上运行了六个这样的脚本,但每个脚本的不同版本中的目录都不同,因此我同时对多个目录进行了版本控制。不会对机器性能造成真正的损失。
!/ usr / bin / env python3
打印(“项目文件版本已开始”)打印(“ version_creation.py”)#将所有这些代码放入具有此名称的脚本中打印(“从命令行运行为..'python3 version_creation.py'”)打印(“ ctrl” c'停止”)print(“”)print(“要在后台在命令行中运行程序,然后关闭窗口。”)print(“ nohup python3 version_creation.py”)print(“ .... to停止进程进入菜单/管理/系统监视器...并杀死python3“)print(”“)print(”始终将文件保存到'ProjectFiles'目录,版本文件“)print(”也会在该目录中创建。“)打印(”“)打印(”“)打印(”“)打印(”“)
导入shutil导入os导入时间
---设置以下时间间隔来检查以下新文件(以秒为单位)
-此间隔应小于新文件出现的间隔!
t = 10
---设置源目录(dr1)和目标目录(dr2)
dr1 =“ / path / to / source_directory”
dr2 =“ / path / to / target_directory”
导入glob导入os
dr1 =“ / home / michael / ProjectFiles”#原始和版本都将保存到此目录
dr2 =“ / home / michael / ProjectFileVersions”
而True:
if os.listdir(dr1) == []:
打印(“空”)
n = 100
else:
list_of_files = glob.glob(dr1+'/*') # * means all if need specific format then *.csv
latest_file_path = max(list_of_files, key=os.path.getctime)
打印(“ 1 Latest_file_path =”,latest_file_path)
originalname = latest_file_path.split('/')[-1]
打印(“ 2 originalname =”,原始名称)
filecreation = (os.path.getmtime(latest_file_path))
打印(“ filecreation =”,filecreation)
now = time.time()
fivesec_ago = now - 5 # Number of seconds
打印(“ fivesec_ago =”,Fivesec_ago)
timedif = fivesec_ago - filecreation #time between file creation
打印(“ timedif =”,timedif)
if timedif <= 5: #if file created less than 5 seconds ago
nameroot = originalname.split(".")[-0]
print ("3 nameroot= ", nameroot)
extension = os.path.splitext(originalname)[1][1:]
print ("4 extension = ", extension)
curdatetime = time.strftime('%Y%m%d-%H%M%S')
print ("5 curdatetime = ", curdatetime)
newassembledname = (nameroot + "_" + curdatetime + "." + extension)
print ("6 newassembledname = ", newassembledname)
source = dr1+"/"+originalname
print ("7 source = ", source)
target = dr1+"/"+newassembledname
print ("8 target = ", target)
shutil.copy(source, target)
time.sleep(t)
分享
下面是放在较早的工作,但我更喜欢上面的python脚本......(使用python约3个小时)
#!/usr/bin/env python3
print ("PROJECT FILES VERSIONING STARTED")
print ("projectfileversioning.py")
print ("run as.. 'python3 projectfileversioning.py' from command line")
print ("ctrl 'c' to stop")
print (" ")
print ("To run program in background type below to command line and then close the window. ")
print ("nohup python3 projectfileversioning.py")
print ("....to stop process go menu/administration/system monitor... and kill python")
print (" ")
print ("Always save files to the 'ProjectFiles' directory and the file ")
print (" will be redirected to the ProjectFileVersions where")
print (" time stamped versions will also be created.")
print (" ")
print ("If you like you may then copy/move the versioned and original file from 'ProjectFileVersions' to ")
print ("any other directory you like.")
import shutil
import os
import time
#--- set the time interval to check for new files (in seconds) below
#- this interval should be smaller than the interval new files appear!
t = 10
#--- set the source directory (dr1) and target directory (dr2)
#dr1 = "/path/to/source_directory"
#dr2 = "/path/to/target_directory"
import glob
import os
dr1 = "/home/michael/ProjectFiles"
dr2 = "/home/michael/ProjectFileVersions"
while True:
if os.listdir(dr1) == []:
n = 100
else:
list_of_files = glob.glob(dr1+'/*') # * means all if need specific format then *.csv
latest_file_path = max(list_of_files, key=os.path.getctime)
print ("1 Latest_file_path = ", latest_file_path)
originalname = latest_file_path.split('/')[-1]
print ("2 originalname = ", originalname)
nameroot = originalname.split(".")[-0]
print ("3 nameroot= ", nameroot)
extension = os.path.splitext(originalname)[1][1:]
print ("4 extension = ", extension)
curdatetime = time.strftime('%Y%m%d-%H%M%S')
print ("5 curdatetime = ", curdatetime)
newassembledname = (nameroot + "_" + curdatetime + "." + extension)
print ("6 newassembledname = ", newassembledname)
source = dr1+"/"+originalname
print ("7 source = ", source)
target = dr2+"/"+originalname
print ("8 target = ", target)
shutil.copy(source, target)
source = dr1+"/"+originalname
print ("9 source = ", source)
target = dr2+"/"+newassembledname
print ("10 target = ", target)
shutil.move(source, target)
time.sleep(t)
#share