python建立新檔案 python建立和刪除檔案

2021-10-10 03:51:44 字數 1738 閱讀 6998

#!/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....")

shell建立新資料夾

bin bash read dir1 for d in dir1 do mkdir cd for file in doecho cur dir create file c touch c done cd done echo create file success awk beginend test....

Linux 修改檔案時間及建立新檔案

檔案時間 modification time mtime 當該檔案的 內容資料 更改時,就會更新這個時間。內容資料指的是檔案的內容,而不是檔案的屬性或許可權 status time ctime 當該檔案的 狀態 改變時,就會更新這個時間,舉例來說,像是許可權與屬性被更改了都會更新這個時間 acces...

Python定期從SVN更新檔案

如果乙個測試站點要及時的從svn獲取最新的檔案,那麼寫乙個定期更新程式是非常必要的,下面的 python的簡單實現 svnupdate.py import time,os,sys,svnconfig dist svnconfig.setting dist os.chdir svnconfig.set...