0

I have a NodeJS project that I'm having call a Python script which simply makes a new directory in a remote file storing network (a UNC path).

However, when I try to use os.mkdir in the Python, it seems that on the server-side, the Node doesn't want to escape the '\' characters, meaning it's trying to create a directory "\\\\myserver.com\\folder1\\newFolder" instead of "\\myserver.com\folder1\newFolder"

Code:

NodeJS:

var pyshell = new PythonShell('mkdir.py', {scriptPath:"/path/toScript"});   //Calls python script (works well)

Python:

import os
import sys
print("SCRIPT CALLED")    #Test that .py file is being called
os.mkdir("\\\\myserver.com\\folder1\\newFolder")

Output (on server):

SCRIPT CALLED      #Shows script is being called properly, as print method is showing
Error: OSError: [Errno 71] Protocol error: '\\\\myserver.com\\folder1\\newFolder'
File "/home/mkdir.py", line 8, in <module>
      os.mkdir("\\\\myserver.com\\folder1\\newFolder")

My best guess here based on the error is that it's not escaping the characters when trying to make the directory. Any insight? I'd really appreciate any help.

mmarion
  • 859
  • 8
  • 20
  • That can't be your exact code, because what you're saying is impossible. Are you actually hard-coding a path in your Python file? Or are you passing it from node.js? Escape characters are only meaningful for hard-coded paths. If you're passing the path from node.js, then don't double the backslashes. Send the exact string you want. – Tim Roberts Feb 23 '21 at 22:54
  • Did you confirm that the `os.mkdir` script works to create a folder via UNC when called natively from Python (removing Node.js from the picture as it's almost certainly irrelevant)? – jarmod Feb 23 '21 at 22:54
  • Also remember that ALL Windows APIs accept forward slashes as well. You can skip the escaping and send forward slashes. – Tim Roberts Feb 23 '21 at 22:55
  • @TimRoberts I will eventually be passing in a path from Node but for the time being I was trying to hard code the path just to see if it's even possible. And after trying the forward slashes I get a `No such file or directory` error – mmarion Feb 23 '21 at 23:01
  • @jarmod Yes I just created a new .py file on my desktop with no supporting code and copied the code from above into it. It made the directory in the file share network no problem. – mmarion Feb 23 '21 at 23:01
  • You might investigate at [extrabacon/python-shell](https://github.com/extrabacon/python-shell) if that's what you're using. It may be parsing the Python script instead of simply executing the Python script using native Python. – jarmod Feb 23 '21 at 23:09
  • Are you creating that script on the fly, or is it stored in a known location? The error message says it occurred on line 8, so that can't be the exact contents of your file. – Tim Roberts Feb 24 '21 at 00:34
  • @TimRoberts the script is in the same directory as the .js file. And line 8 is correct; it's a 10 line python script and the `mkdir()` command is on line 8, which I'm guessing is throwing the error because it's trying to `os.mkdir()` the literal string with excess '\' 's and not escaping them, making it an invalid dir format – mmarion Feb 24 '21 at 02:07
  • I hear what you're saying, but Python simply does not work that way. Could you possibly have an "r" before the string? – Tim Roberts Feb 24 '21 at 07:36
  • @TimRoberts exactly which is why this really throwing me off, but I have had some similar issues with NodeJS and slashes before (circa another issue I was having: https://stackoverflow.com/a/66250918/12060361). I made sure it's not a raw string and I even tried doing a raw string with single slashes to no avail – mmarion Feb 24 '21 at 21:09

0 Answers0