开发者

how to check for platform incompatible folder (file) names in python

开发者 https://www.devze.com 2023-03-21 13:44 出处:网络
I would like to be able to check from python if a given string could be a valid cross platform folder name - below is the concrete problem I ran into (folder name ending in .), but I\'m sure there are

I would like to be able to check from python if a given string could be a valid cross platform folder name - below is the concrete problem I ran into (folder name ending in .), but I'm sure there are some more special cases (e.g.: con, etc.).

Is there a library for this?


From python (3.2) I created a folder on Windows (7) with a name ending in dot ('.'), e.g. (without square brackets): [What I've done on my holidays, Part II.]

When the created folder was ftp'd (to linux, but I guess that's irrelevant), it did not have the dot in it anymore (and in return, this broke a l开发者_开发技巧ot of hyperlinks).

I've checked it from the command line, and it seems that the folder doesn't have the '.' in the filename

mkdir tmp.
dir
cd tmp 
cd ..\tmp.

Apparently, adding a single dot at the end of the folder name is ignored, e.g.:

cd c:\Users.

works just as expected.


Nope there's sadly no way to do this. For windows you basically can use the following code to remove all illegal characters - but if someone still has a FAT filesystem you'd have to handle these too since those are stricter. Basically you'll have to read the documentation for all filesystem and come up with a complete list. Here's the NTFS one as a starting point:

ILLEGAL_NTFS_CHARS = "[<>:/\\|?*\"]|[\0-\31]"
def __removeIllegalChars(name):
    # removes characters that are invalid for NTFS
    return re.sub(ILLEGAL_NTFS_CHARS, "", name)

And then you need some "forbidden" name list as well to get rid of COM. Pretty much a complete mess that.. and that's ignoring linux (although there it's pretty relaxed afaik)


Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not.

http://msdn.microsoft.com/en-us/library/aa365247.aspx#naming_conventions

That page will give you information about other illegal names too, for Windows that is. Including CON as you said your self.

If you respect those (seemingly harsh) rules, I think you'll be safe on Linux and most other systems too.

0

精彩评论

暂无评论...
验证码 换一张
取 消