Python可以打开的文件最大大小? [英] Max size of a file Python can open?
问题描述
我用Python打开了一个8 MB的文件,因为我想批量更改各种类型的文件名.我仔细检查并将文件加载到字符串中,然后使用字符串方法replace替换了所有内容.然后我注意到只有一半的文件被替换了.好像Python没有完全打开文件.
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.
我是否必须在Python的范围内播放某种字符串大小限制或最大文件大小限制?
Is there some kind of string size limit or max file size limit that I must play within the bounds of in Python?
请参考 Python搜索并替换未正确替换的代码.
我已更改为建议的代码.缓冲区是一个超过15万行的8 MB HTML文件.替换代码可以完美地工作;只是它并不能替代一切.又例如,一个令人痛苦的错误是:
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:
当我尝试将字符串ff10替换为FF-10时,它将更改为FF-010.
When I'm attempting to replace the string ff10 to FF-10, it'll be changed to FF-010.
推荐答案
否,Python可以打开的文件大小没有可达到的最大值.现代意义上8 MB是 tiny .你在某个地方犯了错误.
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.
人们经常将千兆字节数据加载到内存中.根据计算机的RAM(无论是64位还是32位操作系统和处理器),在您获得MemoryError
之前,实际的最大值可能在1GB以上.
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
.
作为测试,我只是将一个350 MB的文件加载到一个字符串中.只花了几秒钟.然后,我将其写回到文件中.那花了一点时间.然后,我对文件进行了哈希处理.两者是相同的.
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的大型字符串没有问题,直到达到RAM,操作系统或处理器的极限为止.
Python has no problems with large strings, until you hit the limit of your RAM, operating system, or processor.
您说您遍历并将文件加载到字符串中" –听起来像您可能会犯错的第一个地方.要将文件加载到字符串中,只需执行fileobject.read()
.如果您以其他方式执行此操作,则可能是问题所在.
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.
这篇关于Python可以打开的文件最大大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!