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

D

drodrig

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!
 
T

Tim Golden

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
 
A

Aleksey

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

drodrig

(Sorry for the late reply)

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top