25
2021
08

Colab delete files and upload files to your destination folder

#Removing all BOM with excel format
import os
from google.colab import drive
from google.colab import files
from google.colab import auth
import re
 
drive.mount('/content/drive', force_remount=False)
location = '/content/drive/MyDrive/'
files = os.listdir(location)

del_file_list = [i for i in files if re.match(".*\.xls",i,re.IGNORECASE) != None]

no_of_files=len(del_file_list)

for file in del_file_list:
    deletefile = os.path.join(location, file)
    os.remove(deletefile)

print(("{} files have been removed to TRASH BIN!").format(no_of_files))

==============================================

# 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)



=================================================================











import os
import shutil
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)
path='/content/'
 
uploaded = files.upload()
 
data = [filename for filename in os.listdir(path) if filename != ".config" and filename != "drive" and filename != "sample_data" and ".json" not in filename]
print(data)

for i in data:
    shutil.move("/content/%s"%i, "/content/drive/MyDrive/TS")


#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")

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。