help with file path exceeding 255 characters

John Machin sjmachin at lexicon.net
Tue May 13 17:38:28 EDT 2008


yguan08 at gmail.com wrote:
> I have trouble of obtaining the file size of a file because the
> fullpath exceeds 255 characters. I get this message with
> os.path.getsize(fullpath).
> 
> fullpath = r"\\LOSSSFS002\NWE_TECHNICAL\05. UK\Schiehallion (204_25a)
> \Amos&Burke_P559\07. Technical (Subsurface)\06. Well Planning\Amos
> \Daily Reports\Wireline\final_204_25a_8z\204_25a_8z\Run_1A
> \HRLA_SP_DSI_PEX_HNGS_ECRD\ANCILLARY\Amos_204_25a-8Z_GR_to_surface_3-
> Mar-2008_DSI_HRLA_TLD_MCFL_047PUC.las"
> 
> fsize = os.path.getsize(fullpath)
> 
> 
> WindowsError: [Error 206] The filename or extension is too long:
> 
> The same thing with os.stat(fullpath).
> 
> I tried using the relative path, but it does not work with the message
> fname not found.
> 
> dir = os.path.dirname(fullpath)
> fname = os.path.basename(fullpath)
> os.chdir(dir)

I suspect that the cause of the problem here is that while *x OSes have 
just one current working directory, Windows has one cwd per drive.

> filesize = os.path.getsize(fname)
> 
> Is there a walk around of this?

At the DOS command level, you can do
     subst x: very\long\path
and then refer to
     x:filestem.ext
where x is any unused drive letter.

This seems to work:

 >>> import os
 >>> os.system('subst x: c:\\junk')
0
 >>> f = open('x:foo.py')
 >>> f.read()
"print 'hello'\n"
 >>>

HTH,
John



More information about the Python-list mailing list