Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

unc - using pushd and popd to work with network directories in python

My question is similar to the one asked here. But the proposed solution is not working for what I am trying to do.

I am using Windows 10 with python 3.8.5. I am trying to run a cmd file which is stored in the network drive. But for that I dont want to use the path "Z:abcdefghi" instead I want to use "\ipabcdefghi". I tried using os.chdir("\ipabcdefghi") and check the directory I am in and it shows me exactly the path I enter. But when I try to run the file it says "returned non-zero exit status 1".

Here is a sample operation that I want to do;

First I tried this,

local_network_dir = "Z:\abc\def\ghi"

curr_dir = os.getcwd()

os.chdir(local_network_dir)

Works perfectly fine. But as I said I want to avoid this.

Then I tried this,

network_dir = "\\ip\abc\def\ghi"

local_output = subprocess.check_output('my_cmd_file -arg')

network_output = subprocess.check_output('pushd '+ key+ ' & ' + command + ' & popd',stderr=subprocess.STDOUT,shell=True)

This method works for me most of the time but sometimes fails so I want to avoid it.

Is there a way to make this robust so it doesnt fail ever?

question from:https://stackoverflow.com/questions/65830343/using-pushd-and-popd-to-work-with-network-directories-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...