Python Forum
Opening Directories on anothe File System
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening Directories on anothe File System
#1
Hi Everyone

I am trying to change to a directory on a remote server (Both Windows OS, same LAN)

I am doing this but it does not work

directory5 = os.chdir("\\servername\c$\DIR1\DIR2") # Change working directory

I get this error

File "C:\Python\TEST.py", line 3, in <module>
directory5 = open("\\servername\c$\DIR1\DIR2")
FileNotFoundError: [Errno 2] No such file or directory: '\\servername\c$\DIR1\DIR2'

I have looked at using an extra \ and r but can get this to work, please can anyone help?

TIA
Reply
#2
The resource you're trying to access is a samba share. First you have to mount the resource or use a library to access direct to samba shares. I guess new servers don't support the old samba protocol. Maybe this module works: https://github.com/jborean93/smbprotocol

Otherwise you can use subprocess to run the net command to mount a samba share.

from subprocess import Popen, PIPE


def mount_share(path, target, persistent=False):
    cmd = ['net', 'use', target, path]
    if persistent:
        cmd.append('/PERSISTENT:YES')
    proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    retval = proc.wait()
    if retval != 0:
        raise ValueError(proc.stderr.read())
Then you can access with Python the mounted share, just like with normal files.
I check in the function the return value of the net command.
Maybe you want some information if there were an error and why.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thanks for the help, turns out I just needed to add more backslashes

This worked

\\\\servername\\c$\\DIR1\\DIR2

Thanks again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 423 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 689 Oct-29-2023, 12:40 PM
Last Post: Mark17
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,563 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Listing directories (as a text file) kiwi99 1 834 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,096 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 711 Nov-04-2022, 05:23 PM
Last Post: elnk
  I need to copy all the directories that do not match the pattern tester_V 7 2,410 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Facing Problem while opening a file through command prompt vlearner 4 1,904 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Functions to consider for file renaming and moving around directories cubangt 2 1,746 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Subprocesses not opening File Select Dialog teut 2 2,403 Feb-22-2021, 08:07 PM
Last Post: teut

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020