Python 可以打开的文件的最大大小?
我用Python打开了一个8MB的文件,因为我想批量更改各种类型的文件名。我将文件加载到字符串中,并使用字符串方法替换来替换所有内容。然后我注意到只有一半的文件被替换了;就好像 Python 没有完全打开文件一样。
是否有某种字符串大小限制或最大文件大小限制,我必须在 Python 的范围内播放?
请参阅Python搜索和替换未正确替换中的代码。
我已更改为建议的代码。该缓冲区是一个 8 MB 的 HTML 文件,超过 150k 行。替换代码完美运行;只是它并不能取代一切。或者,例如一个令人痛苦的错误是:
当我尝试将字符串 ff10 替换为 FF-10 时,它将更改为 FF-010。
I opened an 8 MB file in Python, because I wanted to batch change various types of file names. I went through and loaded the file into a string and used the string method replace to replace everything. I then noticed that only half of the file was being replaced; as if Python wasn't fully opening the file.
Is there some kind of string size limit or max file size limit that I must play within the bounds of in Python?
Refer to the code in Python search and replace not replacing properly.
I have changed to the suggested code. The buffer is an 8 MB HTML file that is over 150k lines. The replacement code works perfectly; it's just that it's not replacing everything. Or for example one error that is a pain is:
When I'm attempting to replace the string ff10 to FF-10, it'll be changed to FF-010.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,Python 可以打开的文件大小没有上限。用现代术语来说,8MB 很小。你在某个地方犯了错误。
人们经常将千兆字节的数据加载到内存中。根据您计算机的 RAM(无论是 64 位还是 32 位操作系统和处理器),在出现
MemoryError
之前,您的实际最大内存可能是 1 GB 以上。作为测试,我刚刚将 350 MB 文件加载到字符串中。只花了几秒钟。然后我将其写回到文件中。这花了一点时间。然后我对文件进行了哈希处理。两者是相同的。
Python 处理大字符串没有问题,直到达到 RAM、操作系统或处理器的限制。
你说你“仔细检查并将文件加载到字符串中”——这听起来像是你可能犯的第一个错误。要将文件加载到字符串中,只需执行
fileobject.read()
即可。如果您以其他方式执行此操作,则可能会出现问题。No, there is no reachable maximum on the size of a file Python can open. 8 MB is tiny in modern terms. You made a mistake somewhere.
People regularly load gigabytes of data into memory. Depending on your computer's RAM, whether it's 64- or 32- bit OS and processor, the practical maximum for you may be anywhere from 1 GB up before you get a
MemoryError
.As a test, I just loaded a 350 MB file into a string. It took only a few seconds. I then wrote it back out to a file. That took a little longer. I then hashed the file. The two are identical.
Python has no problems with large strings, until you hit the limit of your RAM, operating system, or processor.
You say you "went through and loaded the file into a string" -- that sounds like the first place you could have made a mistake. To load a file into a string, you just do
fileobject.read()
. If you did it some other way, that could be the problem.