python刪除服務(wù)器文件代碼示例
本文主要研究的是Python編程刪除服務(wù)器文件,具體實(shí)現(xiàn) 代碼如下。
實(shí)例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()
實(shí)例2
用戶微信目錄因常年累月用戶上傳圖片較多,造成硬盤資源將耗盡,但客戶要求至少保存一個(gè)月的文件,
然而幾十萬(wàn)張圖片的文件夾,不論是打開,排序刪除都是非常消耗服務(wù)器性能的,因?yàn)檠b載這10多個(gè)G的文件必然會(huì)造成內(nèi)存和CPU的大量消耗,因此寫了python腳本來(lái)自動(dòng)刪除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ù)器文件代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式
這篇文章主要介紹了python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-06-06Pandas庫(kù)中iloc[]函數(shù)的使用方法
在數(shù)據(jù)分析過程中,很多時(shí)候需要從數(shù)據(jù)表中提取出相應(yīng)的數(shù)據(jù),而這么做的前提是需要先“索引”出這一部分?jǐn)?shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Pandas庫(kù)中iloc[]函數(shù)的使用方法,需要的朋友可以參考下2023-01-01Python 比較文本相似性的方法(difflib,Levenshtein)
今天小編就為大家分享一篇Python 比較文本相似性的方法(difflib,Levenshtein),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-10-10詳解使用PyInstaller將Pygame庫(kù)編寫的小游戲程序打包為exe文件
這篇文章主要介紹了詳解使用PyInstaller將Pygame庫(kù)編寫的小游戲程序打包為exe文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08python買賣股票的最佳時(shí)機(jī)(基于貪心/蠻力算法)
這篇文章主要介紹了python買賣股票的最佳時(shí)機(jī)(基于貪心/蠻力算法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07