spaces at ends of filenames or directory names on Win32

mensanator at aol.com mensanator at aol.com
Thu Feb 23 16:22:00 EST 2006


rtilley wrote:
> # Spaces are present before and after the XXX
> filename = ' XXX '
>
> new_filename = filename.strip()
>
> if new_filename != filename:
>      print filename
>
> Macs allow these spaces in file and folder names. Which is OK. The
> problem arises when the file or folder is copied to a PC running Windows
> from a Mac. Windows allows the Mac to copy the file to it, but when
> Windows itself attempts to do anything with the file it strips the
> spaces and then tries to move it, copy it, etc and complains that the
> file isn't there!
>
> I can rectify this by striping the spaces from the files and folders on
> the Mac before they are copied to the PC or mounting the Windows share
> from the Mac and running a recursive strip program I wrote, but Windows
> will not allow the whitespace removal directly from within Windows....
> how annoying!
>
> For example... tell windows to move a file named ' XXX ' (one space
> before and one space after the filename). Windows will complain that
> file 'XXX' does not exist. It's correct of course, 'XXX' does not exist,
> but ' XXX ' does indeed exist.
>
> Can anyone rescue me from this madness :(

Use quatation marks.

>copy tt1.txt " XXX "
        1 file(s) copied.

>dir XXX
 Volume in drive C is XXXXPXXABA
 Volume Serial Number is 90C0-740E

 Directory of C:\Documents and Settings\Desktop

File Not Found

>dir " XXX
 Volume in drive C is XXXXPXXABA
 Volume Serial Number is 90C0-740E

 Directory of C:\Documents and Settings\Desktop

02/06/2006  07:05p                  10  XXX
               1 File(s)             10 bytes
               0 Dir(s)   2,063,822,848 bytes free

>dir *.
 Volume in drive C is XXXXPXXABA
 Volume Serial Number is 90C0-740E

 Directory of C:\Documents and Settings\Desktop

02/23/2006  03:20p      <DIR>          .
02/23/2006  03:20p      <DIR>          ..
02/06/2006  07:05p                  10  XXX
01/20/2006  04:53p      <DIR>          AGMP
01/20/2006  04:53p      <DIR>          BPWR Fe-Mn
01/20/2006  04:53p      <DIR>          GM
01/20/2006  04:53p      <DIR>          IDEM response
01/20/2006  04:53p      <DIR>          msgs
01/20/2006  04:53p      <DIR>          New Folder
02/08/2006  05:29p      <DIR>          New Folder (2)
01/20/2006  04:53p      <DIR>          OCAML
01/20/2006  04:53p      <DIR>          review05
01/20/2006  04:53p      <DIR>          scanned docs
02/17/2006  03:07p      <DIR>          USS repository
               1 File(s)             10 bytes
              13 Dir(s)   2,063,822,848 bytes free

Note that the trailing quote isn't always necessary.
Note from the last listing that the  XXX  file is indented one space.

> 
> Many Thanks,
> Brad




More information about the Python-list mailing list