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
.
os.path. getsize (path) Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.
getsize() method in Python is used to check the size of specified path. It returns the size of specified path in bytes.
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 .
Use the os. path. getsize('file_path') function to check the file size.
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.
The trailing L
means you have a long
. You actually always have it, but print
ing 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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With