IOError:[Errno 27] File too large
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Mar 13 13:32:59 EDT 2013
On Wed, 13 Mar 2013 09:53:17 -0700, ch.valderanis wrote:
> Hi,
>
> Relatively newcomer here.
> The following code fails with the above error: python version used 2.6.2
> under linux
Which part of the code fails? Please copy and paste the entire traceback,
starting with the line "Traceback (most recent call last)" and ending
with the complete error message.
Which file does it fail on?
> filestring='somestring'
> for files in glob.glob('*'):
> f2=open(files.replace('.xml','.sub'),'w')
> f2.write(filestring+files)
> f2.close()
Some other comments:
You use the name "files" to represent a single filename, rather than
multiple files. That is misleading, a poor choice of name.
"f2" is also a poor name.
You try to change the file extension using string.replace method. This is
risky, and will fail if some file has ".xml" in the filename apart from
the file extension. Worse, if you have a file *without* ".xml", your code
will over-write that file. Better to use the os.path.splitext function
for this.
Is filestring really a constant string like in the snippet above? Is
there any change that it could be an enormous string?
Re-writing your code snippet:
import os
filestring = 'somestring'
for filename in glob.glob('*.xml'):
newname = os.path.splitext(filename)[0] + '.sub'
newfile = open(newname, 'w')
newfile.write(filestring + filename)
newfile.close()
> I am pretty sure that the files is less than 1kB.
"Pretty sure"? You need to be 100% certain. We can't tell you whether
this is the case or not.
> Is there another reason for the operation to fail?
We don't know which operation has failed. That's why you need to show the
complete traceback.
--
Steven
More information about the Python-list
mailing list