os.path.getsize reports a filesize with an L at the end, why?

耗尽温柔 提交于 2019-12-10 02:16:10

问题


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('.'))

Whenever this is ran, I get the output of {'example.py':392L}, why? What's an L? I don't want to have to strip the L off at the end.

If I run it without adding it to a dictionary, it comes back with the filesize as 392.


回答1:


This is only displayed or in interactive mode or when you get the string representation via repr(). As zigg wrote, you can simply ignore it. Consider this an implementation detail. It was probably usefull in time when it was important to make a difference between normal int and long int. In Python 3, there is no L, for example. The int is int no matter how big:

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
>>>

Notice the L by Python 2.7, but nothing similar by Python 3.2.




回答2:


The trailing L means you have a long. You actually always have it, but printing a dict will show printable representations of the values, including the L notation; however, printing a long itself shows only the number.

You almost certainly don't need to worry about stripping off the trailing L; you can use a long in all your calculations just as you would use an int.




回答3:


It' true the pepr's answer but if you realy need you can do the int() Function, it works also on big integers

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

BUT if you put in the Function int() Automagically:

>>> int(os.path.getsize('File3'))
4099


来源:https://stackoverflow.com/questions/12589976/os-path-getsize-reports-a-filesize-with-an-l-at-the-end-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!