==============================================
# coding: utf-8
import re
import os
import time
import datetime
from google.colab import drive
from google.colab import files
from google.colab import auth
import re
import sys
drive.mount('/content/drive', force_remount=False)
dir = '/content/drive/MyDrive/UbuntuServer/'
files=os.listdir(dir)
#print(files)
list=[(os.path.join(dir,i)) for i in files if re.match(".*\.zip|.*\.sql",i,re.IGNORECASE)]#正側式表達,匹配.zip同.sql的所有文件
print("共"+str(len(list))+"個備份文件")
today = datetime.datetime.now()
# 計算偏移 -14日前
offset = datetime.timedelta(days=-14)
#獲取想要的日期,即14日前既時間
re_date = (today + offset)
# 14日前時間轉換16xxxxxx.0 用作與file time做對比
re_date_unix = time.mktime(re_date.timetuple())
print("今日日期",today.strftime('%Y-%m-%d')) #當前日期
print("14日前日期",re_date.strftime('%Y-%m-%d')) #14日前既日期
print("\n")
for i in list:
file_time = os.path.getmtime(i) # 文件修改时间
timeArray = time.localtime(file_time) # 时间戳->结构化时间
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) #格式化时间
print("文件被修改的時間",otherStyleTime)
if file_time <= re_date_unix:
#os.remove(i)
print("14日前,建議刪除!",i)
else:
print("14日內,請先保留!",i)
=================================================================
#or !mv "/content/adc.json" "/content/drive/My Drive/TS"
import shutil import pandas as pd from google.colab import drive from google.colab import files from google.colab import auth auth.authenticate_user() from oauth2client.client import GoogleCredentials drive.mount('/content/drive', force_remount=True) global path path = "/content/drive" from google.colab import files
uploaded = files.upload()
filename = next(iter(uploaded)) #loop through dic 一次只拿頭一個
#print(filename) shows the file name that you've just uploaded
shutil.move("/content/%s"%filename, "/content/drive/MyDrive/TS")