Hey, im having a problem retrieving information about some files due to the fact either the file name or the extension is too long.

For example i want to retrieve the size of a file only i call


Note that it may not be a .txt extension, but it normall no longer than a 3 character extension.

And i get the following error

WindowsError: [Error 206] The filename or extension is too long:

Is there away around this?


Recommended Answers

All 8 Replies

I don't seem to be getting any errors, maybe you could post a screenie?

I did:

import os
f = '''txttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttextWEEtxttex.text'''

print "Size of file:"
print os.path.getsize(f)

print "\nNumber of characters of filename:"
for char in f:
print x


Size of file:

Number of characters of filename:

*I did the max amount of characters that Windows XP SP2 let me

Its probably including the path in that case then, since the whole path C:\\..............\\whatever including the file is 305 characters long.


Its probably including the path in that case then, since the whole path C:\\..............\\whatever including the file is 305 characters long.


You could use os.chdir(path) to establish the working directory and then use the filename only.

Thanks vega i had a feeling it would come down to that, i will test it when i get home as im currently in college. But i am testing is on a memory stick and i have set up my script so that as it iterates through the files it changes the current working dir to that of the file im retrieving the size of, then i retrieve the size and finally return to the original dir of the python script ready to output results to the text file.
This is done if an exception is thrown, other wise i just call os.path.getsize() on the whole file location.

It seems to work fine i just need a long file to test it on.


I have just tested this to see if it works, and unfortuantly not, it is now throwing an error saying cannot find the file, im guessing this is because the name is too long.

is there a version of getsize that allows for bigger file names?

WindowsError: [Error 3] The system cannot find the path specified:

note this file does exist and is not inuse by another application


You probably syntaxed the the file incorrectly :P

import os

filepath = "C:\\Documents and Settings\\Username\\Desktop\\coolapps\\pythonprograms\\zomglongfilename.file"

info = os.path.split(filepath)


print os.path.getsize(info[-1])

Copy and paste the full directory name, replacing my string. Then simply add a forward slash to escape the ones already there!

fsz += os.path.getsize( os.path.join(path, f) )

Above is what i normally call providing there is no error thrown due to the file naming etc being too long.

Below is what i call if the file name / dir is too long.

os.chdir( path )
fsz += os.path.getsize( f )

to me this is fine. If you can see something wrong with this then please tell me.


Well, you can use Long Path Tool as well, it works good for such problems!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.