通过python程序访问网络文件夹 [英] Accessing a network folder through a python program

查看:58
本文介绍了通过python程序访问网络文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是我正在做的一个简短概述:我正在尝试使用网络目录中的python自动执行一些pdf合并例程,这涉及在特定网络位置复制,删除和创建文件.抱歉,如果我的语言不太准确.

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.

我正在Windows 7上使用python 3.6进行编码.该程序将需要在其他计算机上分发,因此本地和临时修复程序可能无济于事.我编写的代码具有完整的功能,并且可以与所有本地文件夹和文件一起正常工作,但是,由于现在我需要在网络上使用它,因此在访问所需的文件夹时遇到了一些困难.

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.

这是我尝试过的:

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

仅在cmd中输入

pushd popd 即可正常工作,但是当我通过python进行系统调用时,它们只是无法通过.我发送了一个系统调用,它可以正常运行,但是当我"cd"当前目录时,它表明我仍在上一个目录中.如果通过cmd手动完成,则一切都会按需进行.我已经用谷歌搜索了这个问题,但是最终没有发现任何有用的/有用的东西.我真的很感谢任何建议,如果需要进一步澄清我的问题,请告诉我.

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.

谢谢!

推荐答案

我不会以这种方式使用push/popd,我只会在需要进行任何文件操作的路径中包括完整路径,包括网络路径.做

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

但是,如果我真的需要更改工作目录,则可以使用python:

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.

这篇关于通过python程序访问网络文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆