how to get the size of python file without using the os namespace

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

  1. open the file
  2. seek to the end
  3. 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.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *