python刪除服務(wù)器文件代碼示例
本文主要研究的是Python編程刪除服務(wù)器文件,具體實現(xiàn) 代碼如下。
實例1
#coding:utf-8 import paramiko """ 創(chuàng)建文件 刪除文件 root權(quán)限 """ 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
用戶微信目錄因常年累月用戶上傳圖片較多,造成硬盤資源將耗盡,但客戶要求至少保存一個月的文件,
然而幾十萬張圖片的文件夾,不論是打開,排序刪除都是非常消耗服務(wù)器性能的,因為裝載這10多個G的文件必然會造成內(nèi)存和CPU的大量消耗,因此寫了python腳本來自動刪除30天以前的文件
代碼如下:
#-*- coding:utf-8 -*- import os import time import datetime f = 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")
結(jié)果:
總結(jié)
以上就是本文關(guān)于python刪除服務(wù)器文件代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
python3.6.5基于kerberos認證的hive和hdfs連接調(diào)用方式
這篇文章主要介紹了python3.6.5基于kerberos認證的hive和hdfs連接調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python 比較文本相似性的方法(difflib,Levenshtein)
今天小編就為大家分享一篇Python 比較文本相似性的方法(difflib,Levenshtein),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件
這篇文章主要介紹了詳解使用PyInstaller將Pygame庫編寫的小游戲程序打包為exe文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08