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
.