在linux下使用ls命令結合正規表示式,能夠高效地進行檔案搜尋,並通過引數操作檔案,於是就想用python實現這個功能以便在windows上使用
import os
import re
import sys
path = os.getcwd()
substr = raw_input('the sub-string of the file (support for regular expression): ')
reg = r'' + substr
count = 0
totalsize = 0
filelist = os.listdir(path)
def getfilesize(filepath):
return os.path.getsize(filepath)
def humanreadfilesize(filebyte):
# filebyte = str(getfilesize(filepath))
if filebyte == 0:
return ''
elif filebyte < 1024:
# bfilesize = str(filebyte) + ' b'
elif filebyte < 1024*1024:
# kb
filesize = str(filebyte/1024.0) + ' kb'
elif filebyte < 1024*1024*1024:
# mb
filesize = str(filebyte/1024.0/1024.0) + ' mb'
else:
# gb
filesize = str(filebyte/1024.0/1024.0/1024.0) + ' gb'
return filesize
for filename in filelist:
if re.match(reg, filename):
count = count+1
filepath = os.path.join(path, filename)
filesize = getfilesize(filepath)
totalsize = totalsize+filesize
print '\t', count, ' - ', humanreadfilesize(filesize).ljust(30, ' '), filename
print '-'*160
if len(sys.argv)!=1:
if sys.argv[1] == 'remove':
os.remove(filepath)
print '\n\t\t%d file(s) in total, total size: %s' % (count, humanreadfilesize(totalsize))
Python實現CGI環境
要想安裝python的cgi環境,就繼續往下看吧。首先,要確定apache服務可以使用cgi服務。開啟apache的配置檔案,設定如圖。在啟動的apache服務的系統目錄下,建立目錄如 users iosqiao sites web cgi bin 然後新建hello.py檔案,如下 重啟伺服器後,...
linux環境安裝python
預設情況下 linux環境自帶python環境 可能是2.4版本 如果想安裝新版本 可以用如下方法 1 mkdir python 建立乙個資料夾 3 tar zxvf python 2.7.6.tgz 進行解壓 如果解壓失敗 檢視echo lang 檢視編碼 export lang zh cn.ut...
linux配置python環境
容器以管理員許可權進入 sudo docker exec ti u root 容器id bash 解包 2.tar xvjf python 3.9.0.tar.xz 進入python資料夾,生成編譯指令碼 3.執行 configure prefix var jenkins home python3 ...