3

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!

Avión
  • 7,963
  • 11
  • 64
  • 105
Irina Fortiss
  • 95
  • 1
  • 2
  • 6
  • could be a number of things. Is there a global address to the drive? rather than a locally mapped 'letter'? (for example large corporate intranet networks have a global address, that employees often map to their own "N:" (or whatever letter) drive. try using the overall address. Additionally, try accessing the drive through a string prefaced by an 'r' as such: `r'C:/Users/Vlox/Stuff'` the `r` indicates a raw string, so slashes will be read properly (regularly they are a python escape character which may cause your issue). alternatively, double your backslashes `'C://Users//Vlox//Stuff'` – Vlox Jun 26 '17 at 13:01
  • I believe I am using a global address since it is what everyone else is using to access the network "\\ourFolderName\reports". From what I understand, `popd` will create a temporary drive for me, but I'm not at that stage just yet. The way I entered slashes already accounts for escape characters, since I have tested a string with a path name on it's own, but I still gave a try to 'r", unfortunately that didn't help. – Irina Fortiss Jun 26 '17 at 13:12
  • The PUSHD is probably working fine. However, when the shell that was created exits, you are back to your existing process which still has the cwd it had before calling os.system(). – lit Jun 26 '17 at 13:46
  • That's what I thought too, since I've seen it explained in other related questions. However, I check manually after the program runs, and no temporary drives are being created. – Irina Fortiss Jun 26 '17 at 13:50

1 Answers1

5

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.

Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
  • This is where I have started originally, and I got thrown off by the fact that I can't access the server directly through cmd, but in fact, I don't even have to! Thank you! – Irina Fortiss Jun 26 '17 at 14:51