#!/usr/bin/python
#-*-coding:utf-8-*- #指定編碼格式,python默認unicode編碼
import os
directory = "./dir"
os.chdir(directory) #切換到directory目錄
cwd = os.getcwd() #獲取當前目錄即dir目錄下
print("------------------------current working directory------------------")
def deleteBySize(minSize):
"""刪除小於minSize的文件(單位:K)"""
files = os.listdir(os.getcwd()) #列出目錄下的文件
for file in files:
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: #獲取文件大小
os.remove(file)
print(file + " deleted.")
return
def create():
'''根據本地時間創建新文件,如果已存在則不創建'''
import time
t = time.strftime('%Y-%m-%d',time.localtime()) #將指定格式的當前時間以字符串輸出
suffix = ".docx"
newfile= t+suffix
if not os.path.exists(newfile):
f = open(newfile,'w')
print newfile
f.close()
print newfile + " created."
else:
print newfile + " already existed."
return
hint = '''funtion:
1 create new file
2 delete null file
3 delete by size
please input number:'''
while True:
option = raw_input(hint) #獲取IO輸入的值
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....")