2

I am trying to opne a file that is located on a server using the windows cmd. What I do is basically the following thing:

import os 
os.system('pushd '+ \\Server\PathToFile)
os.system('start Notepad '+ NameOfFile)

The point is that this works if I enter it by hand in the cmd. If I try to do it within python it does not work. I get this error message:

CMD.EXE was started with the path given above as current directory.
UNC-paths are not supported, therefore the windows-directory is used as 
current directory.

The actual error message is in german, that's why I translated it and I'm not sure whether it's understandable or not. What actually happens is that the path where notepad is looking for the current file is C:\Windows instead of the path that I indicated.

1 Answer 1

4

Windows doesn't support setting the current directory to an UNC path, and it wouldn't have worked anyway since those are 2 separate os.system commands.

You could mount a drive on this path and use os.chdir, but that would make it more complex yet!

You don't really need current directory change. Moreover os.system is deprecated, it's recommended to use subprocess instead.

So change your code to run the command providing full path to the file:

import subprocess
subprocess.call(["start","notepad",os.path.join("\\Server\PathToFile",NameOfFile)],shell=True)

but I suspect you'd be better off with

os.startfile(os.path.join("\\Server\PathToFile",NameOfFile))

(default associations of Windows will probably open "notepad" in background, that's one-line & simple, & users can even change the editor used by just changing text file association in windows)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.