2015-03-13 88 views
1

我想計算各種文件的大小的總和。這是我的腳本:腳本文件大小的總和

import os 
date = raw_input('Enter date in format YYYYMMDD ') 
file1 = 'p_poupe_' + date + '.tar.gz.done' 
file2 = 'p_poupw_' + date + '.tar.gz.done' 
file3 = 'p_pojk_' + date + '.tar.gz.done' 

a1 = os.system('zcat ' + file1 + '|wc --bytes') 
a2 = os.system('zcat ' + file2 + '|wc --bytes') 
a3 = os.system('zcat ' + file3 + '|wc --bytes') 

print a1,a2,a3 
sum = a1 + a2 + a3 

print sum 

但值不存儲在變量中。任何人都可以告訴我我做錯了什麼。我怎樣才能修改腳本,以便將值存儲在變量中而不是作爲輸出。

+0

'操作系統。system'將返回'wc'的返回碼,而不是輸出到'stdout' – 2015-03-13 12:37:06

+0

可能重複[在Python中os.system()的返回值是什麼?](http://stackoverflow.com/questions/ 6466711/os-system-in-python中的什麼是返回值) – 2015-03-13 12:38:44

+1

[os.path.getsize](https://docs.python.org/2/library/os.path.html #os.path.getsize)應該完成工作...''os.system'返回值不是創建進程的標準輸出。 – 2015-03-13 12:42:13

回答

0

可以從commands捕獲使用getoutput功能的命令的輸出爲:

import commands as cm 
. 
. 
. 
a1 = cm.getoutput('zcat ' + file1 + '|wc --bytes') 
a2 = cm.getoutput('zcat ' + file2 + '|wc --bytes') 
a3 = cm.getoutput('zcat ' + file3 + '|wc --bytes') 

# Note that the outputs are in string format so you need to convert them to integers or floats 
a1, a2, a3 = float(a1), float(a2), float(a3) 

print a1,a2,a3 
sum = a1 + a2 + a3 

print sum 
+0

'a1,a2,a3 = map(float,(a1,a2,a3))' – horns 2015-03-13 13:05:15

1

在Unix上,返回值是 爲等待指定的格式()編碼的過程的退出狀態。請注意,POSIX沒有指定C系統()函數返回值的 含義,因此Python函數的返回值 取決於系統。

在Windows中,是,運行命令後返回由系統外殼 返回值,由Windows環境變量 COMSPEC給出:上command.com系統(視窗95,98和ME)這始終是 0;在cmd.exe系統上(Windows NT,2000和XP)這是退出 命令運行狀態;在使用非本機外殼的系統上,請參閱您的外殼文檔。

https://docs.python.org/2/library/os.html#os.system

的問題是,你使用退出代碼,而不是標準輸出數據作爲你的「價值」。 您可能正在尋找使用subprocess.Popen的例子。或者只是簡單地通過打開文件手動編碼解決方案。

嘗試使用https://docs.python.org/3/library/gzip.html

import gzip 
def get_fcont_len(fname): 
    with gzip.open(fname) as f: 
     return len(f.read()) 
total = 0 
date = raw_input('Enter date in format YYYYMMDD ') 
total += get_fcont_len('p_poupe_' + date + '.tar.gz.done') 
total += get_fcont_len('p_poupw_' + date + '.tar.gz.done') 
total += get_fcont_len('p_pojk_' + date + '.tar.gz.done') 
print(total) 
0

可以使用os模塊來獲取文件的大小。試試這個:

import os 
import tarfile 

tar = tarfile.open("yourFile.tar.gz") 
tar.extractall("folderWithExtractedFiles") 
print os.path.getsize("folderWithExtractedFiles/yourFileInsideTarGz") 
+0

這不會解壓縮它們 – haavee 2015-03-13 12:43:23

+0

注意'.gz',實際的文件大小超出了磁盤上文件的物理空間。 – Torxed 2015-03-13 12:44:13

+0

我的不好,我正在用代碼編輯,以 – 2015-03-13 12:50:16

1

os.system返回命令的退出狀態而不是命令的輸出。要捕獲命令的輸出,您應該查看subprocess module

subprocess.check_output("zcat " + file1 + " | wc --bytes", shell=True) 
# Output the size in bytes of file1 with a trailing new line character 

但是它可能是最好使用其他Python模塊/方法來做到這一點其他的建議,因爲它是最好做的事情直接在Python。

1

未壓縮的文件的大小被存儲在最後4個字節gzip文件的。該函數將返回未壓縮文件的大小,即「gunzip解壓」尺寸:

import os 
import gzip 
import struct 

def get_gunzipped_size(filename): 
    with gzip.open(filename) as f: 
     _ = f.read(1) # elicit IOError if file is not a gzip file 
     f.fileobj.seek(-4, os.SEEK_END) 
     return struct.unpack('<i', f.fileobj.read(4))[0] 

上的大文件,這是比讀取所有未壓縮的數據,並計算它的長度,因爲整個文件不需要快得多被解壓縮。

配件到這個代碼:

import os 

date = raw_input('Enter date in format YYYYMMDD ') 
prefixes = ('p_poupe_', 'p_poupw_', 'p_pojk_') 
files = ['{}{}.tar.gz.done'.format(prefix, date) for prefix in prefixes] 

total_uncompressed = sum(get_gunzipped_size(f) for f in files) 
print total_uncompressed 
+0

'作爲fz顯示gzip.open(filename)語法錯誤,不知道爲什麼。 – user2922822 2015-03-16 07:40:28

+0

在'with'語句末尾應該有一個冒號,即''gzip.open(filename)'爲f:' – mhawke 2015-03-16 07:59:52

+0

@ user2922822:我剛剛對讀取文件大小的代碼做了一個小改動,此代碼也將在Python 3中工作(它必須傳遞要讀取的字節數)。 – mhawke 2015-03-16 08:15:33