网络编程
位置:首页>> 网络编程>> Python编程>> python删除服务器文件代码示例

python删除服务器文件代码示例

作者:douyunqian668  发布时间:2023-07-26 15:44:08 

标签: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删除服务器文件代码示例

来源:http://blog.csdn.net/douyunqian668/article/details/78712575

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com