os.path.getsize reports a filesize with an L at th

2019-04-22 18:53发布

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.

3条回答
ゆ 、 Hurt°
2楼-- · 2019-04-22 19:11

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-22 19:21

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-04-22 19:29

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
查看更多
登录 后发表回答