Berikut ini adalah skrip Python3 yang membuat VMS menyukai versi file otomatis menggunakan cap waktu yang ditambahkan ke nama file asli saat disimpan.
Saya telah memasukkan banyak komentar ke dalam skrip dan menjalankan setengah lusin skrip seperti itu di mesin ubuntu saya dengan hanya direktori yang berbeda di setiap versi skrip yang berbeda sehingga saya secara bersamaan memversi banyak direktori. Tidak ada penalti nyata untuk kinerja alat berat.
! / usr / bin / env python3
print ("PROYEK FILES VERSIONING STARTED") print ("version_creation.py") # tempatkan semua kode ini ke dalam skrip dari print nama ini ("run as .. 'python3 version_creation.py' dari command line") print ("ctrl ' c 'untuk berhenti ") cetak (" ") cetak (" Untuk menjalankan program di latar belakang ketik di bawah ini untuk baris perintah dan kemudian tutup jendela. ") cetak (" nohup python3 version_creation.py ") cetak (" .... untuk hentikan proses buka menu / administrasi / monitor sistem ... dan bunuh python3 ") print (" ") print (" Selalu simpan file ke direktori 'ProjectFiles' dan file versi ") print (" juga akan dibuat di direktori itu . ") print (" ") print (" ") print (" ") print (" ")
import shutil import os import time
--- atur interval waktu untuk memeriksa file baru (dalam detik) di bawah ini
- Interval ini harus lebih kecil daripada interval file baru muncul!
t = 10
--- atur direktori sumber (dr1) dan direktori target (dr2)
dr1 = "/ path / ke / source_directory"
dr2 = "/ path / ke / target_directory"
impor global impor os
dr1 = "/ home / michael / ProjectFiles" #keduanya asli dan versi akan disimpan ke direktori ini
dr2 = "/ home / michael / ProjectFileVersions"
sementara Benar:
if os.listdir(dr1) == []:
cetak ("Kosong")
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)
filecreation = (os.path.getmtime(latest_file_path))
print ("filecreation =", filecreation)
now = time.time()
fivesec_ago = now - 5 # Number of seconds
print ("fivesec_ago =", fivesec_ago)
timedif = fivesec_ago - filecreation #time between file creation
print ("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)
Bagikan
di bawah ini dimasukkan sebelumnya dan berfungsi tetapi saya suka skrip python di atas jauh lebih baik ...... (telah menggunakan python selama sekitar 3 jam)
#!/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