Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 993
Matthew Avatar asked Sep 25 '12 19:09

Matthew


People also ask

What does os path Getsize return?

os.path. getsize (path) Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.

What does Getsize return in Python?

getsize() method in Python is used to check the size of specified path. It returns the size of specified path in bytes.

How does Python validate file size?

Using stat() from the os module, you can get the details of a file. Use the st_size attribute of stat() method to get the file size. The unit of the file size is byte .

How do I check if a file size is greater than 0 in Python?

Use the os. path. getsize('file_path') function to check the file size.


3 Answers

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.

like image 185
pepr Avatar answered Oct 11 '22 13:10

pepr


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.

like image 25
zigg Avatar answered Oct 11 '22 13:10

zigg


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
like image 29
Thomas8 Avatar answered Oct 11 '22 15:10

Thomas8