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

asked Feb 20 by arun

1 Answer

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.

answered Feb 20 by Eric Z Ma (41,910 points)

Related questions

Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...