GENie icon indicating copy to clipboard operation
GENie copied to clipboard

os.mkdir only works with paths with forward slashes, even on windows

Open SonnyBonds opened this issue 6 years ago • 5 comments

When using os.mkdir I have to do a path.translate(dir, '/') on Windows. Example: If I do "os.mkdir('c:/a/b/c\d/e/f')" I end up with the folders c:\a\b only.

SonnyBonds avatar Aug 17 '18 07:08 SonnyBonds

This is because \ in strings creates an escape sequence. The string "\d" is actually interpreted as being the escape sequence d but because Lua doesn't know the \d escape sequence, print("\d") is just d.

If you write print("\\d"), then the \ is escaped by another \ (to create a literal \) and the output is \d.

(By the way, if you write print(path.translate('c:/a/b/c\d/e/f', '/')) the output will have \\ as literal \s.)

Why os.mkdir('c:/a/b/c\d/e/f') doesn't create the folder C:\a\b\cd\e\f is a mystery (maybe another bug), but \\ will work to create Windows-style pathnames.

catb0t avatar Aug 18 '18 02:08 catb0t

Sorry, I wrote my example wrong. (In my actual case it has concatenated paths with actual mix of forward and back slashes.) Will double check with my simpler test case again if I screwed up my slash escaping there, but in my production case they are correct and doing a path.translate to forward slashes fixed it.

SonnyBonds avatar Aug 18 '18 06:08 SonnyBonds

@catb0t Double checked now and I just wrote my example wrong here in original comment. Doing this: print(os.mkdir('c:/a/b/c\\d/e/f')) prints: nil unable to create directory 'c:/a/b/c\d'

My guess would be it splits the path on forward slash to create each folder in turn, and then tries to create a folder called "c\d" which windows won't allow.

SonnyBonds avatar Aug 18 '18 11:08 SonnyBonds

yep that sounds about right, good bug report then! :)

catb0t avatar Aug 19 '18 03:08 catb0t

I'll add path normalization.

bkaradzic avatar Aug 20 '18 14:08 bkaradzic