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

os.path.getsize("somelongfilename.txt")

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?

Thanks,
Chris

Mark Carlson commented: great post +0

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)

x=0
print "\nNumber of characters of filename:"
for char in f:
    x=x+1
print x

Result:

>>>
Size of file:
896

Number of characters of filename:
211
>>>

*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.

Chris

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

Chris

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.

Chris

commented: Hi Chris, Found LongPathTool program useful +0

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

Chris

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)

os.chdir(info[0])

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.

chris

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

Hi Mr Freaky,
Yeah it can be a real headache, I tried LongPath Tool Program to good effect
thanks Rory

commented: duplicates previous post and is 16 years too late -3
commented: Duplicated above and 16 years late. -4
Be a part of the DaniWeb community

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