国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python刪除服務器文件代碼示例

2020-02-22 23:11:27
字體:
來源:轉載
供稿:網友

本文主要研究的是Python編程刪除服務器文件,具體實現 代碼如下。

實例1

#coding:utf-8import paramiko"""  創建文件 刪除文件 root權限"""ssh=paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname="192.168.1.37",port=22,username="test",password="test")stdin,stdout,stderr=ssh.exec_command('sudo -i touch /a.txt',get_pty=True)stdin.write("test/n")# stdin.write("/n")stdin.close()stdout.close()print(stderr.read())stderr.close()stdin,stdout,stderr=ssh.exec_command('sudo -i rm -f /a.txt',get_pty=True)stdin.write("test/n")# stdin.write("/n")stdin.close()print(stderr.read())ssh.close()

實例2

用戶微信目錄因常年累月用戶上傳圖片較多,造成硬盤資源將耗盡,但客戶要求至少保存一個月的文件,

然而幾十萬張圖片的文件夾,不論是打開,排序刪除都是非常消耗服務器性能的,因為裝載這10多個G的文件必然會造成內存和CPU的大量消耗,因此寫了python腳本來自動刪除30天以前的文件

代碼如下:

#-*- coding:utf-8 -*-import osimport timeimport datetimef = list(os.listdir(‘G://qtp‘))for i in range(len(f)):  filedate = os.path.getmtime(‘G://qtp//‘ + f[i])  time1 = datetime.datetime.fromtimestamp(filedate).strftime(‘%Y-%m-%d‘)  date1 = time.time()  num1 =(date1 - filedate)/60/60/24  if num1 >= 30:    os.remove(‘G://qtp//‘ + f[i])    print("已刪除文件:%s : %s" % (time1, f[i]))else:  print("there are no file more than 30 days")

結果:

總結

以上就是本文關于python刪除服務器文件代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新安县| 白河县| 哈尔滨市| 马鞍山市| 太和县| 华蓥市| 邻水| 从化市| 小金县| 沙坪坝区| 宜兰市| 西畴县| 绍兴市| 甘肃省| 蕲春县| 屯昌县| 楚雄市| 体育| 郸城县| 德钦县| 黎平县| 阿勒泰市| 上杭县| 常德市| 赫章县| 丰县| 滦南县| 阿合奇县| 镇远县| 叶城县| 工布江达县| 浦江县| 西昌市| 合作市| 铁力市| 红安县| 繁昌县| 漯河市| 乐清市| 鄂托克旗| 澜沧|