how to get the size of python file without using the os namespace
Posted on In QAi need to create a program which is taking information from printer , so that i cannot use os namespace , so there is any way without using the os namespace
You may use a way as follows.
- open the file
- seek to the end
- tell its position which is the file size
An example is as follows.
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open("/tmp/file.jar", "rb")
>>> file.seek(0, 2) # 2 is os.SEEK_END
>>> filesize = file.tell()
>>> print filesize
3364
Here, the value of os.SEEK_END
(2) is used directly. This piece of code is not 100% portable. But as you have no access to the os
module, this may be a way to work around of that.