Accessing a network folder through a python program

☆樱花仙子☆ 提交于 2021-02-05 06:15:07

问题


Just a brief outline of what I'm doing: I'm trying to automate some pdf merging routine with python in a network directory, which involves copying, deleting and creating files at a specific network location. Apologies if my language is not very precise.

I'm coding on windows 7, using python 3.6. The program will need to be distributed on other machines, so local and temporary fixes will probably not help. The code I wrote is fully functional and works fine with all the local folders and files, however, now that I need to make use of it on the network, I am having some difficulties accessing the folder I need.

Here is what I have tried:

os.system("pushd " + "\\" + "\\netWorkDrive\Reports")  
check_output("pushd " + "\\" + "\\netWorkDrive\Reports", shell=True)

pushd and popd work fine when entered just in the cmd, but when I do system calls through python, they just don't go through. I send a system call, and it runs correctly, but then when I "cd" a current directory, it shows that I'm still in my previous one. If done through the cmd manually, everything works as desired. I have googled the issue, but did not end up finding anything working/useful. I would really appreciate any suggestions, and let me know if I need to clarify my problem further.

Thank you!


回答1:


I would not use pushd/popd in such a way, I would just include the full paths, including network paths in the paths of whatever file operation I need to do

However if I really need to change working directory, I would do this with python:

import os

original_working_directory = os.getcwd()

# do stuff

new_networked_directory = r'\\server\share\folder'
# change to the networked directory
os.chdir(new_networked_directory)

# do stuff

#changeback to original working directory
os.chdir(original_working_directory)

# do more stuff

There should be no need for "temp drives" or the like really.



来源:https://stackoverflow.com/questions/44760302/accessing-a-network-folder-through-a-python-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!