Python Forum
trouble with os.listdir on a network drive - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: trouble with os.listdir on a network drive (/thread-18560.html)

Pages: 1 2


trouble with os.listdir on a network drive - lconner - May-22-2019

everyone,

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”20.2.2.44:” for example, in that exact format. The problem is when I input the network drive (ex:r”20.2.2.44:”)(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.

LCONNER


RE: trouble with os.listdir on a network drive - heiner55 - May-22-2019

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

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


RE: trouble with os.listdir on a network drive - lconner - May-22-2019

heiner55

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.

thanks,
lconner


RE: trouble with os.listdir on a network drive - heiner55 - May-22-2019

Python3 on Win7 runs without problems.

I am using https://docs.paramiko.org

But maybe os.listdir() works if you would use "net use":
net use x: \\xxx.xxx.xxx.xxx\sharefolder

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/gg651155(v%3Dws.11)


RE: trouble with os.listdir on a network drive - lconner - May-23-2019

entering in r"w:\20.2.2.44:" still gives me the exact same result, an empty list with no errors thrown.


RE: trouble with os.listdir on a network drive - heiner55 - May-24-2019

Then you have to use paramiko or something similiar.


RE: trouble with os.listdir on a network drive - lconner - Jun-03-2019

heiner55

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


RE: trouble with os.listdir on a network drive - DeaD_EyE - Jun-04-2019

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.

https://www.python.org/dev/peps/pep-0471/#os-walk


RE: trouble with os.listdir on a network drive - lconner - Jun-04-2019

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


RE: trouble with os.listdir on a network drive - snippsat - Jun-04-2019

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 [email protected]:/path/to/foo /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.