Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 trouble with os.listdir on a network drive

I am trying to get a list of directories and files on a network drive.
My code is:

print 'running...'

# imports
import os, sys
from os import listdir
import arcpy, os
import os
import os.path
import csv

#set root and make list
path = input ('enter directory for searching: ')
driv = os.listdir( path ) #list of folders
print driv 
maplist = []
## more code to manipulate list 

I enter: r”” for example, in that exact format. The problem is when I input the network drive (ex:r””)(which I know has and folders file) the list (driv) returns blank. No errors are thrown. The script proceeds to the remainder of the code dedicated to manipulate the list (driv). If I enter: r”c:” the list (driv) is populated.

How do I format my input so the network drive will populate the list (driv)? Are there any other factors besides the formatting of the input that would prevent os.listdir from working on a network drive?

python version 2.7.5
IDLE version 2.7.5
OS: Windows 7 professional

Also if any of you know good sources about python on network drives, please refer me.

You should use Python 3.x instead of Python 2.7.

os.listdir() works only on local computer, not remote.

I can try to see if get python 3. I might not be able to.

what command/method should i use to get a list of files and folders on a network drive.

Python3 on Win7 runs without problems.

I am using

But maybe os.listdir() works if you would use "net use":
net use x: \\\sharefolder
entering in r"w:\" still gives me the exact same result, an empty list with no errors thrown.
Then you have to use paramiko or something similiar.

Can you elaborate? It seems to me that paraminko is a mod and has to do with encryption and creating/maintaining a network. This is is not what I am interested in. The network is already established and maintained. I have a connection to it and can access the drive in the same method I can the c drive. I just need the script to have the same abilities.

Thank you
You don't want a remote control, you want to access remote files.
Paramiko is used for ssh and ssh is used on linux servers.

Python can only access on mounted file systems. You can use the net command
to mount your samba share on a drive letter and with Python you can
access this path. Newer Python Versions will be a little bit faster
on remote file access, because they use os.scandir in os.walk.
os.scandir does not request all attributes of the file,
which increases the search speed.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!
Entering r"w:\\" worked.

If i am flowing what you said the using the letter drive made the script recognize it as a mounted drive (there is already a connection the computer can freely use). I not sure why using the IP address did not have the same result. In another script I have paths with the IP address and files are retrieved with out issue.

I assume, net, is the python method for establishing a connection to a drive (i.e. mount it).

also, both of you mentioned samba and paramiko which are for Linux if i am not mistaken. Was there something in my post that made you think i was using Linux?

Thank You
For Windows i use cmder,which have ssh build in.
So ssh user@host will connect to external host.
When have access then use standard tool like cd navigate and ls to list files.

Also cmder has a lot of tool build in,ssh, git, scp, cat, find...ect.
Copy from server folder(foo) to C:\ drive tmp folder.
scp -r /c/tmp
lconne Wrote:both of you mentioned samba and paramiko which are for Linux if i am not mistaken.
Paramiko works on Windows,there are more specialized tool like fabric, ansible.
Quote:Fabric is a high level Python (2.7, 3.4+) library designed to execute shell commands remotely over SSH,
yielding useful Python objects in return.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Nested Subdirectory within current sub on shared network drive Agregware 1 341 Jun-19-2019, 10:07 PM
Last Post: Gribouillis
  download file from google drive .. evilcode1 7 2,877 Sep-21-2018, 06:13 PM
Last Post: evilcode1
  os.listdir(path) and non-string as input metalray 4 3,703 Aug-15-2018, 11:43 AM
Last Post: metalray
  listdir trouble Dixon 1 723 Jan-17-2018, 11:32 PM
Last Post: micseydel
  Best Module for Navigating Google Drive? HiImNew 2 898 Nov-29-2017, 01:38 AM
Last Post: HiImNew
  Uploading CSV Files to Google drive in a specified folder prashant8530 1 1,256 Sep-30-2017, 10:58 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)