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

3

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

Disaffirm answered 25/9, 2012 at 19:43 Comment(3)
It's probably returning the file size as a long.Commonable
@xbonez -- post that as an answer -- maybe with a quick explanation of what a long actually is.Michaelmichaela
@Disaffirm -- Just curious, what OS is this on?Michaelmichaela
F
13

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.

Franchescafranchise answered 25/9, 2012 at 20:47 Comment(2)
Ah, I see. That makes perfect sense. As a beginner, should I be learning Py3 or continue with 2.7?Disaffirm
It does not matter that much. The important things are the same. However, I would suggest to try both if possible. My guess is that Python 3 wins soon. Have a look at getpython3.com/diveintopython3/strings.html. The strings are one of the most visible differences. Python 3 is more logical, Python 2.6 is more used these days. Python 2.7 is in between.Franchescafranchise
T
8

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.

Tillford answered 25/9, 2012 at 19:57 Comment(0)
S
2

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
Shakti answered 4/4, 2013 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.