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. Maven 编译

    pom.xml 添加插件 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins< ...

  2. C#中特性,以及应用场景(收藏链接)

    1:http://www.tracefact.net/CLR-and-Framework/Reflection-Part3.aspx 2:http://www.cnblogs.com/landeanf ...

  3. iOS改变UIAlertView、UIActionSheet、UIAlertController系统字体颜色

    废话不多说,直接上代码,效果是最好的说服力 1.改变UIAlertView字体颜色 [UIView appearance].tintColor = [UIColor greenColor]; 个人还是 ...

  4. 高性能 Socket 组件 HP-Socket v3.2.1-RC1 公布

    HP-Socket 是一套通用的高性能 TCP/UDP Socket 组件.包括服务端组件.client组件和 Agent 组件.广泛适用于各种不同应用场景的 TCP/UDP 通信系统.提供 C/C+ ...

  5. Tomcat 开启远程调试

    根据 Tomcat 启动方式在 catalina.sh 或者 startup.sh 添加一下内容 $TOMCAT_HOME/bin/catalina.sh 添加 CATALINA_OPTS=" ...

  6. Matlab图像处理(03)-基本概念

    概念定义 动态范围:灰度跨跃的值域称为动态范围.上限取决于饱和度,下限取决于噪声. 对比度:一幅图像中最高和最低灰度级间的灰度差. 空间分辨率:图像中可辨别的最小细节的度量.常用度量每单位距离线对数和 ...

  7. quick check

  8. UVA12103 —— Leonardo's Notebook —— 置换分解

    题目链接:https://vjudge.net/problem/UVA-12103 题意: 给出大写字母“ABCD……Z”的一个置换B,问是否存在一个置换A,使得A^2 = B. 题解: 对于置换,有 ...

  9. python读写txt大文件

    直接上代码: import easygui import os path = easygui.fileopenbox()#path是打开的文件的全路径 if path:#如果选择打开文件,没有选择取消 ...

  10. 应用提交 App Store 上架被拒绝

    审核的参考资料 https://developer.apple.com/icloud/documentation/data-storage/index.html https://developer.a ...