Python to Retrieve list of file on a Network Folder

Is is possible to have a gateway function to retrieve a list of file names and/or folders within a networked dive the gateway has access to?

Sure! Since you didn’t say what OS the share is on, I’m going to assume Windows

[code]def filelist(path):
from os import walk

f = []
d = []

for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
d.extend(dirnames)
break

return [f, d]

print filelist(’//pcName/shareName/folder’)
[/code]

You can remove the break if you need to recurse into subdirectories, but it’ll take more time to finish if it’s very deep.

For example, let’s say the share is called “mothra” and resides on the server “gamera”. The folder to want a listing of is in “/production/reports”. Your path to use would be “//mothra/gamera/production/reports”

Ok I am find that if try to use any of the

os.listdir

or any other ‘os’ function it will always report back from the view of the client. even if i have the following in a project gateway script.

def ImageSearch()
import os
dir = os.listdir(’\Z:\’)
return dir

and call it from the script playground it will always retun the list from the client pc. Why is this?

[quote=“jmoniz”]… call it from the script playground it will always retun the list from the client pc. Why is this?[/quote]Because it’s in a client. {Designers are clients, too.} Only scripts run from a gateway event will actually run in the gateway environment. Errors from those scripts show up in the gateway console, not in any client. Even if you are developing on the gateway server, the gateway service is separate from the user logged in.

Nevermind

As an exercise, create two memory tags in the gateway: a boolean ‘trigger’ and a string ‘result’. Define a value change event on the ‘trigger’ tag with: if not initialChange: from java.util import Date import os x = os.listdir("//someserver/someshare/path") system.tag.write("[default]result", str(x)) Then toggle the trigger tag. Look at the result.

Um i forgot to add that code was in a gateway script but i was calling the function from a client

Problem fixed.

Talked to support over the phone.

The issue was a user account issue trying to access a network share using a windows mapped drive. the path to the share needed to be explicit.

path = '\\xxx.xxx.xxx.xxx\sharename\folder"

vs

path = “\Z:\sharename\folder”

is there a way to make this 1 layer further?

I tried this test script in a string array and it gives me a list of folder names that are all random numbers (probably timestamps), and what i really need is the names of the jpegs and csvs inside of those folders , each folder has 2 images and 1 csv with the same name as the folder name

so a listdir of the result of this listdir?

7 years later, Phil would probably recommend against using Python's built in os module. The best API for access to files and directories in a Java environment is java.nio, so you'll want to start from one of the static methods in Files, e.g.
Files.walk or Files.walkFileTree.

5 Likes