GENie
GENie copied to clipboard
os.mkdir only works with paths with forward slashes, even on windows
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.
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.
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.
@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.
yep that sounds about right, good bug report then! :)
I'll add path normalization.