Python Forum
trouble with os.listdir on a network drive
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trouble with os.listdir on a network drive
#1
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
Reply
#2
You should use Python 3.x instead of Python 2.7.

os.listdir() works only on local computer, not remote.
Reply
#3
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
Reply
#4
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/previou...v%3Dws.11)
Reply
#5
entering in r"w:\20.2.2.44:" still gives me the exact same result, an empty list with no errors thrown.
Reply
#6
Then you have to use paramiko or something similiar.
Reply
#7
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
Reply
#8
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
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
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to get a removable disc type in drive Daring_T 12 970 Feb-11-2024, 08:55 AM
Last Post: Gribouillis
  open python files in other drive akbarza 1 632 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Integrating Google Drive in App Lahearle 0 440 Jul-19-2023, 05:51 PM
Last Post: Lahearle
  code to send attachments contained on the drive. stefanoste78 1 822 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  access is denied error 5 for network drive mapping ? ahmedbarbary 2 1,731 Aug-17-2022, 10:09 PM
Last Post: ahmedbarbary
  Cant Access D: Drive kucingkembar 4 1,262 Jul-28-2022, 04:53 PM
Last Post: kucingkembar
  Calling exe on network drive GrahamL 4 2,466 Jan-21-2022, 12:01 PM
Last Post: GrahamL
  Failing to copy file from a network to a local drive tester_V 4 6,974 Jan-20-2021, 07:40 AM
Last Post: tester_V
  listdir on IP Adress OEMS1 3 2,833 Jul-19-2020, 06:01 PM
Last Post: bowlofred
  Nested Subdirectory within current sub on shared network drive Agregware 1 1,886 Jun-19-2019, 10:07 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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