Python代码如下:

import os
directory = "E:\\学习日志\\"
os.chdir(directory) # 改变当前工作目录
cwd = os.getcwd() # 查看当前工作目录
print("--------------current working directory : " + cwd + "----------") def deleteBySize(minSize):
"""删除小于minSize的文件(单位:K)"""
files = os.listdir(os.getcwd()) # 列出目录中文件
for file in files:
## print file + " : " + str(os.path.getsize(file))
if os.path.getsize(file) < minSize * 1000:
os.remove(file)
print(file + " deleted.")
return def deleteNullFile():
'''删除所有大小为0的文件'''
files = os.listdir(os.getcwd()) # 列出目录中文件
for file in files:
if os.path.getsize(file) == 0: #得到文件大小,如果是目录返回0
os.remove(file)
print(file + " deleted")
return def create():
'''根据本地时间创建新文件,如果已存在则不创建'''
import time
#将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出
t = time.strftime('%Y-%m-%d',time.localtime())
suffix = ".docx"
newFile =os.getcwd() + "\\" + t + suffix
if not os.path.exists(newFile):
f = open(newFile,'w')
f.close()
print newFile + " created."
else:
print newFile + " already exist."
return hint = '''funtion :
1 create new file
2 delete null file
3 delete by size
q quit\n
please input number: '''
while True:
option = raw_input(hint)
if cmp(option,"1") == 0:
create()
elif cmp(option,"2") == 0:
deleteNullFile()
elif cmp(option,"3") == 0:
minSize = raw_input("minSize(K) : ")
deleteBySize(minSize)
elif cmp(option,"q") == 0:
print "quit !"
break
else:
print ("disabled input. please try again...")

主要涉及到的就是一些文件操作函数和时间函数。

Python创建删除文件的更多相关文章

  1. 【Python脚本】Python创建删除文件-----------我的第一个Python脚本

    Python相对C++和Java来说,是解释性语言,非常适合来编写脚本. 很久之前就开始学习Python的语法了,今天写了第一个Python的脚本,来简化我的一些日常工作. 我平时学习的时候喜欢新建一 ...

  2. Linux命令——创建删除文件

    创建文件夹 mkdir filename 进入目录文件 cd filename 返回上一级目录 cd ..返回多级目录   cd ../../.. (../表示一级) 创建文件 touch filen ...

  3. hdfs创建删除文件和文件夹

    在 hadoop 中,基于 Linux 命令可以给 hdfs 创建文件和文件夹,或者删除文件和文件夹 创建文件的命令为: hadoop fs -touch /file.txt 创建文件夹的命令为: h ...

  4. Python定期删除文件、整理文件夹

    1.根据传入的参数,文件所在目录,匹配文件的正则表达式,过期天数进行删除,这些可写在配置文件del_file.conf. del_file3.py #!/usr/bin/env python # en ...

  5. Python新建/删除文件夹

    新建以当前日期为名的文件夹 import datetime,os,shutil today = datetime.datetime.now().date().strftime('%Y%m%d') pa ...

  6. Linux创建删除文件和文件夹

    要想删除和创建,需要有root权限 [xwg@bogon ~]$ su root密码:[root@bogon xwg]# cd /home/a      切换到目录a[root@bogon a]# t ...

  7. python:删除文件及文件夹

    #!/usr/bin/python# -*- coding:utf-8 -*- import os import shutil os.remove(path) #删除文件shutil.rmtree(p ...

  8. python实现删除文件与目录的方法

    os.remove(path) 删除文件 path. 如果path是一个目录, 抛出 OSError错误.如果要删除目录,请使用rmdir().os.rmdir()只能删除空目录 remove() 同 ...

  9. Python 创建递归文件夹

    # 创建递归文件夹 def createfiles(filepathname): try: os.makedirs(filepathname) except Exception as err: pri ...

随机推荐

  1. java 表示当前时间的第二天的几点

    Calendar cal = Calendar.getInstance();  cal.setTime(new Date());  cal.add(Calendar.DAY_OF_YEAR, 1);  ...

  2. 已知某公司总人数为W,平均年龄为Y岁(每年3月末计算,同时每年3月初入职新人),假设每年离职率为x,x>0&&x<1,每年保持所有员工总数不变进行招聘,新员工平均年龄21岁。 从今年3月末开始,请实现一个算法,可以计算出第N年后公司员工的平均年龄。(最后结果向上取整)。

    // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...

  3. HTML5,不仅仅是看上去非常美(第二弹:打造最美3D机房)

    前言 近期项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,曾经一直搞java,对js的一些语言特性和概念一时还转换只是来. 上一篇第一 ...

  4. php部分:网页中报表的打印,并用CSS样式控制打印的部分;

    网页中报表的打印,是通过调用window对象中的print()方法实现打印功能的: 调用浏览器本身的打印功能实现打印 <a href="#" onclick="wi ...

  5. 九度OJ 1003:A+B

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15078 解决:6299 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计 ...

  6. Richard Stallman's computer

    What hardware do you use? I am using a Lemote Yeelong, a netbook with a Loongson chip and a 9-inch d ...

  7. Channel (digital image) 通道 色彩深度 Color_depth

    en.wikipedia.org/wiki/Channel_(digital_image) Color digital images are made of pixels, and pixels ar ...

  8. CrystalReport runtime的下载地址

    SAP网站的东西实在太多了,找个CrytalReport都费劲.13.*版的可以通过下面的地址下载: SAP Crystal Reports, developer version for Micros ...

  9. ruby 精选网站

    ruby 基础   http://www.yiibai.com/ruby/2013/0820174.html     http://www.rubydoc.info/github       http ...

  10. BZOJ 4523 [Cqoi2016]路由表 Trie树

    Trie树的应用题目. 在线建立一棵01 Trie树,然后按照要求用询问在上面跑,用单调栈维护答案即可. #include<iostream> #include<cstdio> ...