python删除服务器文件代码示例
2019/7/13 22:15:23
本文主要是介绍python删除服务器文件代码示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文主要研究的是Python编程删除服务器文件,具体实现 代码如下。
实例1
#coding:utf-8 import 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 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")
结果:
总结
以上就是本文关于python删除服务器文件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
这篇关于python删除服务器文件代码示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python