查找文件大小 [英] finding file size

查看:56
本文介绍了查找文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


最近我做了一个小脚本来做一些文件传输(以及其他

的东西)。我想监视文件传输的进度,所以我需要

才能知道我传输的文件的大小。找出如何获得

这些信息花了一些时间(阅读手册 - 谷歌搜索没有

证明是值得的)。无论如何,我最终弄明白该怎么做(有

有几种方法,包括os.path.getsize(filename))。


我的问题是:文件对象有没有理由不能使用

大小的方法或属性?那么你可以问一下这个文件有多大

正在使用fd.size或fd.size()。我只是很好奇,因为,好吧,似乎

有明显的效用,找到它的方法不太明显(至少,它是b $ b)对我来说。


谢谢,

肖恩

解决方案

"肖恩罗斯 < SR *** @ connectmail.carleton.ca>写道......

我的问题是:文件对象是否有原因无法使用
大小的方法或属性?这样你就可以问文件使用fd.size或fd.size()有多大。我只是很好奇,因为它似乎具有明显的实用性,找到它的方式并不明显(至少,对我来说是这样)。


嘿!


1)使用''fd''作为文件对象的名称是一个坏主意 - 你可以得到<来自os.open的
fds。如果你坚持使用C-ish名称,那么''fp''

怎么样呢? :)


2)没有什么可以阻止文件对象使用大小方法,

除了文件类对象之外还有更多实现。


如下所示:


py> class SizedFile(file):

.... def __len __(self):

.... oldpos = self.tell()

.... self.seek(0,2)

.... length = self.tell()

.... self.seek(oldpos)

....退货长度

....

py> bleh = SizedFile(" / etc / passwd")

py> len(bleh)

1520

py> len([x for x in bleh])

33


正如我写的那样,我意识到这是错的 - size()会更好,因为

序列的长度不是字节数。也许它在

二进制模式下? Dunno,我昏昏欲睡,晚安..

David。

谢谢,
Sean



Sean Ross写道:

我的问题是:文件对象有没有原因可能没有
尺寸方法或属性?



是的。在Python中,文件对象属于较大类别的类文件

对象,并非所有类似文件的对象都具有

大小的固有概念。例如。您认为sys.stdin.size应该返回什么(

实际上是一个合适的文件对象 - 而不仅仅是文件类型)?


其他示例包括从os.popen或socket.socket返回的东西。


问候,

马丁



" Martin v.Loewis" <毫安**** @ v.loewis.de>在消息中写道

新闻:bt ************* @ news.t-online.com ...

Sean Ross写道:

我的问题是:文件对象是否有原因无法使用
尺寸方法或属性?



是。在Python中,文件对象属于较大类别的类文件对象,并非所有类似文件的对象都具有
大小的固有概念。例如。您认为sys.stdin.size应该返回什么(实际上它是一个合适的文件对象 - 而不仅仅是文件)?

其他示例包括从os.popen返回的内容或socket.socket。

问候,
Martin




我明白你的意思了。我想我唯一可以想到的选择

sys.stdin,os.popen和socket.socket将返回写入这些对象的

字节数至今。但是,那些对象,或者其他东西,必须跟踪这些信息。另外,管道和

套接字可以从两个方向写入,所以从双方写入的总字节数是多少?或者你想知道如何

很多你写的大小,或者对方写了多少

(也许这三个都很好)。另一种选择是返回''-1'',

或''无'',让人们知道该请求不支持这个

文件宾语。另一种选择是提出例外。而且,

当然,这里有流行的,留下足够好的选择。


无论如何,谢谢你的回复。我觉得这很有道理。

Sean


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

解决方案

"Sean Ross" <sr***@connectmail.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(oldpos)
.... 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



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



"Martin v. Loewis" <ma****@v.loewis.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


这篇关于查找文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆