2012-09-25 18 views
5
import os, sys 

def crawlLocalDirectories(directoryToCrawl): 
    crawledDirectory = [os.path.join(path, subname) for path, dirnames, filenames in os.walk(directoryToCrawl) for subname in dirnames + filenames] 
    return crawledDirectory 

print crawlLocalDirectories('.') 

dictionarySize = {} 
def getSizeOfFiles(filesToMeasure): 
    for everyFile in filesToMeasure: 
     size = os.path.getsize(everyFile) 
     dictionarySize[everyFile] = size 
    return dictionarySize 

print getSizeOfFiles(crawlLocalDirectories('.')) 

每當這個運行,我得到{'example.py':392L}的輸出,爲什麼?什麼是L?我不想在最後剝去L。os.path.getsize在最後報告帶有L的文件大小,爲什麼?

如果我在沒有將其添加到字典中的情況下運行它,它會返回文件大小爲392

+2

它可能會返回文件大小。 – xbonez

+0

@xbonez - 作爲一個答案發布 - 可能與一個*長*實際*是*的快速解釋。 – mgilson

+0

@Matthew - 只是好奇,這是什麼操作系統? – mgilson

回答

8

這僅顯示或以交互模式顯示,或者通過repr()獲取字符串表示形式。正如zigg寫的,你可以簡單地忽略它。考慮這個實現細節。在正常int和long int之間做出區別很重要時,它可能是有用的。例如,在Python 3中,沒有L。 int是INT不管有多大:

d:\>py 
Python 3.2.1 (default, Jul 10 2011, 20:02:51) [MSC v.1500 64 bit (AMD64)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> a = 100000000000000000000000000000000000000000000 
>>> a 
100000000000000000000000000000000000000000000 
>>> ^Z 

d:\>python 
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win 
32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> a = 100000000000000000000000000000000000000000000 
>>> a 
100000000000000000000000000000000000000000000L 
>>> 

通知的L被Python 2.7,但沒有被Python 3.2類似。

+0

啊,我明白了。這很有道理。作爲初學者,我應該學Py3還是繼續2.7? – Matthew

+0

這並不重要。重要的事情是一樣的。不過,如果可能的話,我會建議嘗試。我的猜測是Python 3很快就會獲勝。看看http://getpython3.com/diveintopython3/strings.html。字符串是最明顯的差異之一。 Python 3更合乎邏輯,現在更多地使用Python 2.6。 Python 2.7介於兩者之間。 – pepr

7

尾隨L表示您有long。您實際上始終擁有它,但print將顯示值的可打印表示形式,包括L表示法;然而,打印long本身只顯示數字。

你幾乎肯定不需要擔心剝離尾隨L;您可以在所有計算中使用long,就像您使用int一樣。

+1

我的+1,因爲它不會公平:) – pepr

0

這是真的PEPR的答案,但如果你真的需要,你可以做INT()函數,它的工作原理也大整數

Python 2.7.3 (default, Jul 24 2012, 10:05:39) 
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2 
>>> import os 
>>> os.path.getsize('File3') 
4099L 

,但如果你把函數int()自動的:

>>> int(os.path.getsize('File3')) 
4099 
相關問題