本文主要研究的是Python編程刪除服務器文件,具體實現 代碼如下。
#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()用戶微信目錄因常年累月用戶上傳圖片較多,造成硬盤資源將耗盡,但客戶要求至少保存一個月的文件,
然而幾十萬張圖片的文件夾,不論是打開,排序刪除都是非常消耗服務器性能的,因為裝載這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刪除服務器文件代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答