Copying a file with a question mark in it's name in Windows

Discussion in 'Python' started by drodrig, Aug 12, 2010.

  1. drodrig

    drodrig Guest

    A python script I use to backup files on a Windows 2003 server
    occasionally fails to retrieve the size of a file with a question mark
    in the name. The exception I get is "OSError #123 The filename,
    directory name, or volume label syntax is incorrect". I realize that
    technically a question mark in the name of a file on Windows is
    illegal, but nevertheless these files exist on the file system. It
    seems that they are created by Office 2007 Word, for the most part.

    The line that fails is:

    os.path.getsize(source)

    Where source is the full path to the file with the question mark in
    it's name.

    Any idea how to retrieve the file's size? Also, I imagine that after I
    overcome this hurdle, I'll need help finding a way to copy the file
    (assuming copy2() doesn't work). I've tried escaping the question mark
    ("\\?"). Same result.

    Although I could use the Windows "dir" command, parsing the results to
    find the size of the file then use the Windows "copy" command, I'd
    rather stay away from this type of solution.

    Any help is appreciated!
     
    drodrig, Aug 12, 2010
    #1
    1. Advertising

  2. drodrig

    Tim Golden Guest

    On 12/08/2010 12:49, drodrig wrote:
    > A python script I use to backup files on a Windows 2003 server
    > occasionally fails to retrieve the size of a file with a question mark
    > in the name. The exception I get is "OSError #123 The filename,
    > directory name, or volume label syntax is incorrect". I realize that
    > technically a question mark in the name of a file on Windows is
    > illegal, but nevertheless these files exist on the file system. It
    > seems that they are created by Office 2007 Word, for the most part.


    Could you show us the script you're using? I'm not aware of any
    way to get a question mark into a file name; even using the
    file namespace prefix \\?\ doesn't allow it as far as I can tell.

    Where are you seeing the question mark? Is it possible it's a
    placeholder for an unprintable character rather than an actual
    question mark?

    TJG
     
    Tim Golden, Aug 12, 2010
    #2
    1. Advertising

  3. drodrig

    Aleksey Guest

    On 12 авг, 18:49, drodrig <> wrote:
    > A python script I use to backup files on a Windows 2003 server
    > occasionally fails to retrieve the size of a file with a question mark
    > in the name. The exception I get is "OSError #123 The filename,
    > directory name, or volume label syntax is incorrect". I realize that
    > technically a question mark in the name of a file on Windows is
    > illegal, but nevertheless these files exist on the file system. It
    > seems that they are created by Office 2007 Word, for the most part.



    If "?" is a placeholder for an unprintable character you can try view
    real file name in IDLE:

    import glob
    print glob.glob(u'e:/full/path/to/file?')

    In path to file you must instead question use wild "?".
    Will be printed all like files.

    -----------------------------
    Under Windows I too have similar problem: windows sometimes (from any
    programs - e.g. Firefox) save files with wrong names, but later
    do not manipulate with it.
     
    Aleksey, Aug 12, 2010
    #3
  4. drodrig

    drodrig Guest

    [SOLVED]: Copying a file with a question mark in it's name in Windows

    On Aug 12, 9:16 am, Aleksey <> wrote:
    > On 12 авг, 18:49,drodrig<> wrote:
    >
    > > A python script I use to backup files on a Windows 2003 server
    > > occasionally fails to retrieve the size of a file with a question mark
    > > in the name. The exception I get is "OSError #123 The filename,
    > > directory name, or volume label syntax is incorrect". I realize that
    > > technically a question mark in the name of a file on Windows is
    > > illegal, but nevertheless these files exist on the file system. It
    > > seems that they are created by Office 2007 Word, for the most part.

    (Sorry for the late reply)

    Thank you. The questions marks are indeed placeholders for unprintable
    characters. The glob module did the trick.

    >
    > If "?" is a placeholder for an unprintable character you can try view
    > real file name in IDLE:
    >
    >   import glob
    >   print glob.glob(u'e:/full/path/to/file?')
    >
    > In path to file you must instead question use wild "?".
    > Will be printed all like files.
    >
    > -----------------------------
    > Under Windows I too have similar problem: windows sometimes (from any
    > programs - e.g. Firefox) save files with wrong names, but later
    > do not manipulate with it.
     
    drodrig, Aug 17, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Muller
    Replies:
    2
    Views:
    2,267
    Neomorph
    Sep 18, 2003
  2. Replies:
    2
    Views:
    444
    Larry Bates
    Jan 11, 2006
  3. Cirene
    Replies:
    5
    Views:
    615
    Cirene
    May 17, 2008
  4. Roger Pack
    Replies:
    4
    Views:
    132
    Rick DeNatale
    Oct 2, 2007
  5. Replies:
    3
    Views:
    99
Loading...

Share This Page