473,561 Members | 3,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding file size

Hi.

Recently I made a small script to do some file transferring (among other
things). I wanted to monitor the progress of the file transfer, so I needed
to know the size of the files I was transferring. Finding out how to get
this information took some time (reading the manuals - googling did not
prove worthwhile). Anyway, I did eventually figure out how to do it (there
are a few ways, including os.path.getsize (filename)).

My question is this: Is there a reason why file objects could not have a
size method or property? So that you could then just ask the file how big it
is using fd.size or fd.size(). I'm just curious, because, well, it seems to
have obvious utility, and the way to find it is less than obvious (at least,
it was to me).

Thanks,
Sean
Jul 18 '05 #1
17 23146
"Sean Ross" <sr***@connectm ail.carleton.ca > wrote...
My question is this: Is there a reason why file objects could not have a
size method or property? So that you could then just ask the file how big it
is using fd.size or fd.size(). I'm just curious, because, well, it seems to
have obvious utility, and the way to find it is less than obvious (at least,
it was to me).
Hey!

1) Using 'fd' as a name for a file object is a bad idea - you can get
fds from os.open. If you insist on C-ish names, how about 'fp'
instead? :)

2) There's nothing to stop the file object from having a size method,
except that file-like objects then have more to implement.

How about something like:

py> class SizedFile(file) :
.... def __len__(self):
.... oldpos = self.tell()
.... self.seek(0, 2)
.... length = self.tell()
.... self.seek(oldpo s)
.... return length
....
py> bleh = SizedFile("/etc/passwd")
py> len(bleh)
1520
py> len([ x for x in bleh ])
33

As I wrote this I realised it's wrong - size() would be better, since
the length of the sequence is not the number of bytes. Maybe it is in
binary mode? Dunno, me sleepy, goodnight..
David.


Thanks,
Sean

Jul 18 '05 #2
Sean Ross wrote:
My question is this: Is there a reason why file objects could not have a
size method or property?


Yes. In Python, file objects belong to the larger category of "file-like
objects", and not all file-like objects have the inherent notion of a
size. E.g. what would you think sys.stdin.size should return (which
actually is a proper file object - not just file-like)?

Other examples include the things returned from os.popen or socket.socket.

Regards,
Martin

Jul 18 '05 #3

"Martin v. Loewis" <ma****@v.loewi s.de> wrote in message
news:bt******** *****@news.t-online.com...
Sean Ross wrote:
My question is this: Is there a reason why file objects could not have a
size method or property?


Yes. In Python, file objects belong to the larger category of "file-like
objects", and not all file-like objects have the inherent notion of a
size. E.g. what would you think sys.stdin.size should return (which
actually is a proper file object - not just file-like)?

Other examples include the things returned from os.popen or socket.socket.

Regards,
Martin


I see what you mean. I suppose the only option I could think of for
sys.stdin, os.popen, and socket.socket would be to return the number of
bytes written to these objects so far. But, then, those objects, or
something else, would have to track that information. Also, pipes and
sockets could be written to from two directions, so is the size the total
number of bytes written from both sides, or would you prefer to know how
much you'd written as the size, or how much the other side had written
(Perhaps all three would be nice). Another option would be to return '-1',
or 'None', to let people know that the request is unsupported for this
file-like object. Still another option would be to raise an exception. And,
of course, there's the ever popular, leave-well-enough-alone option.

Anyway, thank you for your response. I see it's merit.
Sean
Jul 18 '05 #4
"David M. Wilson" <dw***********@ botanicus.net> wrote in message
news:99******** *************** ***@posting.goo gle.com...
1) Using 'fd' as a name for a file object is a bad idea - you can get
fds from os.open. If you insist on C-ish names, how about 'fp'
instead? :)

or just f would work ...

2) There's nothing to stop the file object from having a size method, except that file-like objects then have more to implement.

See Martin v. Loewis' post for some other rationale.

How about something like:

py> class SizedFile(file) :
... def __len__(self):
... oldpos = self.tell()
... self.seek(0, 2)
... length = self.tell()
... self.seek(oldpo s)
... return length
...
py> bleh = SizedFile("/etc/passwd")
py> len(bleh)
1520
py> len([ x for x in bleh ])
33

As I wrote this I realised it's wrong - size() would be better, since
the length of the sequence is not the number of bytes. Maybe it is in
binary mode? Dunno, me sleepy, goodnight..
David.


Right. size() is more apt. Also, while I appreciate the effort of
subclassing file, what I was looking for was to have the builtin file (or
file-like) objects expose this operation, not just custom implementations .

Thanks for your response,
Sean
Jul 18 '05 #5
Hi,

I propose to add a "filename" type to Python.

Martin v. Loewis wrote:
Sean Ross wrote:
My question is this: Is there a reason why file objects could not have a
size method or property?


Yes. In Python, file objects belong to the larger category of "file-like
objects", and not all file-like objects have the inherent notion of a
size. E.g. what would you think sys.stdin.size should return (which
actually is a proper file object - not just file-like)?


A different solution to this problem would be to introduce "filename"
type to Python, a subclass of str. The "name" attribute of file would be of this
type. This type would inherit a lot of os.path stuff: getsize becomes
simpler, more readable, and more object oriented, as do other os.path
functions. I think the alternatives look a lot more prety:

OLD NEW
os.path.realpat h(fn) fn.realpath()
os.path.getmtim e(fp.name) fp.name.getmtim e()
os.path.ismount (os.path.dirnam e(fp.name)) fp.name.dirname ().ismount()

It's more beatiful, simpler, flatter (#3), practical, obvious, easy.

problem: what do do with os.path constants?
solution: make them class attributes
problem: how to handle posixpath, ntpath, macpath?
solution: abstract Path class with NTPath, MacPath, PosixPath sublasses which is the actual type of e.g. fn.name on a certain platform
problem: backwards compatibility
solution: same as string methods
problem: "/dev/null" reads as a Path but is a str
solution: path("/dev/null") is a little more typing for a lot more luxery
problem: what to do with commonprefix?
solution: don't know
problem: what to do with os.path.walk?
solution: use os.walk instead
problem: what to do with sameopenfile?
solution: make it a file method
problem: what to do with join, split?
solution: rename to joinpath, splitpath.

Any comments?

yours,
Gerrit.

--
158. If any one be surprised after his father with his chief wife, who
has borne children, he shall be driven out of his father's house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Jul 18 '05 #6
Gerrit Holl wrote:
Any comments?


It should be possible to implement that type without modifying
Python proper. It might make a good recipe for the cookbook.

Any volunteers?

Regards,
Martin

Jul 18 '05 #7
Gerrit Holl wrote:
I propose to add a "filename" type to Python. A different solution to this problem would be to introduce "filename"
type to Python, a subclass of str. The "name" attribute of file would be
of this type. This type would inherit a lot of os.path stuff: getsize
becomes simpler, more readable, and more object oriented, as do other
os.path functions. I think the alternatives look a lot more prety: OLD NEW
os.path.realpat h(fn) fn.realpath()
os.path.getmtim e(fp.name) fp.name.getmtim e()
os.path.ismount (os.path.dirnam e(fp.name)) fp.name.dirname ().ismount()

It's more beatiful, simpler, flatter (#3), practical, obvious, easy.


You might have a look at

http://mail.python.org/pipermail/pyt...ne/108425.html

http://members.rogers.com/mcfletch/p...ng/filepath.py

has an implementation of your proposal by Mike C. Fletcher. I think both
filename class and os.path functions can peacefully coexist.
Peter

Jul 18 '05 #8
Gerrit Holl wrote:
Any comments?


Are you aware of Jason Orendorff's path module?
(haven't tried it myself though)

See this thread: http://tinyurl.com/3gq8r (google link)

--Irmen
Jul 18 '05 #9
In article <ma************ *************** **********@pyth on.org>,
Gerrit Holl <ge****@nl.linu x.org> wrote:
I propose to add a "filename" type to Python.
[ ... ] A different solution to this problem would be to introduce "filename"
type to Python, a subclass of str. The "name" attribute of file would be of
this
type. This type would inherit a lot of os.path stuff: getsize becomes
simpler, more readable, and more object oriented, as do other os.path
functions. I think the alternatives look a lot more prety:

OLD NEW
os.path.realpat h(fn) fn.realpath()
os.path.getmtim e(fp.name) fp.name.getmtim e()
os.path.ismount (os.path.dirnam e(fp.name)) fp.name.dirname ().ismount()

It's more beatiful, simpler, flatter (#3), practical, obvious, easy.


This has been proposed a few times, and even implemented at least once:

http://www.jorendorff.com/articles/python/path/

I'm very much in favor of adding such an object, but I don't like Jason
Orendorff's design all that much. There has been a discussion about it
in the past:

http://groups.google.com/groups?q=g:...n&lr=&ie=UTF-8
&oe=UTF-8&safe=off&selm =mailman.105765 1032.22842.pyth on-list%40python.o rg

Just
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1713
by: Bastiaan Welmers | last post by:
Hi, I wondered if anyone has ever met this same mbox issue. I'm having the following problem: I need find messages in huge mbox files (50MB or more). The following way is (of course?) not very usable: fp = open("mbox", "r")
13
15208
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the pattern can occur on any byte boundary in the file, so chunking through the code at 16 bytes a frame maybe a problem. The file itself isn't so large,...
2
3127
by: Pawe³ | last post by:
Hello! I'm looking for efficient code or site where I can find code for finding one string in another string. String which I search should have "wild" characters like '?' for any one char and '*' for any string of characters. I'm looking for way to effective getting string from text file and then searching it like I write above. Thanks...
9
10282
by: minil | last post by:
Hi any c function in linux to return size of directory (Including its files & subdirectories & its files).. I can stat() only files . not for directories. please reply me soon Thanks in advance
10
1744
by: tshad | last post by:
I have a Datagrid with a column: <asp:HyperLinkColumn DataTextField="JobTitle" DataNavigateUrlField="PositionID" DataNavigateUrlFormatString="AddNewPositions.aspx?PositionID={0}" HeaderText="Job Title" Visible="True" SortExpression="JobTitle"/>
3
3244
by: Linh Luong | last post by:
Hi All, 1. I have been reading and the max size of a tuple is 8K. I have also read that I can it to a larger size in some config file. Where is this file? is it called pg_config.h and is the variable called BLKSZ?? 2. Is there a way I can find the actual size of the tuple? Do you go into each column and find the length of each value and...
20
10824
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've included a code snippet from Chuck Blake 'ls' app in python. It seems to do the job just fine on my comp, but regrettably, I'm not sassy enough to...
9
2131
by: richee | last post by:
Hi all, I am new to Perl but see that it offers some fantastic opportunities and am trying to use it more in problem solving but could do with a little help on a problem thats driving me nuts.... I am trying to remove certain sub headers from email, i want it to tick through the text until it reaches "content-disposition:" then delete the...
5
2839
by: bharathreddy | last post by:
How to find the size of a folder using C# code? step1: Here take the folder which u want to find the size, then pass that folder to the recursive function name FolderSize!. DirectoryInfo dirInfo = new DirectoryInfo(strPath) ; decimal dSize = 0; //If the folder exists then only its size will be calculated if not its size will...
0
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8085
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.