# Python to Retrieve list of file on a Network Folder

**URL:** <https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905>\
**Category:** Ignition\
**Created:** [December 11, 2015, 7:31pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905 "2015-12-11T19:31:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmoniz](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jmoniz](https://forum.inductiveautomation.com/u/jmoniz)\
**Post date:** [December 11, 2015, 7:31pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/1 "2015-12-11T19:31:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 11, 2015, 9:05pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/2 "2015-12-11T21:05:18Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![jmoniz](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jmoniz](https://forum.inductiveautomation.com/u/jmoniz)\
**Post date:** [December 14, 2015, 3:36pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/3 "2015-12-14T15:36:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 14, 2015, 3:50pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/4 "2015-12-14T15:50:46Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [December 14, 2015, 3:51pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/5 "2015-12-14T15:51:27Z")

</div>

Nevermind

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 14, 2015, 4:12pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/6 "2015-12-14T16:12:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmoniz](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jmoniz](https://forum.inductiveautomation.com/u/jmoniz)\
**Post date:** [December 14, 2015, 4:13pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/7 "2015-12-14T16:13:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmoniz](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@jmoniz](https://forum.inductiveautomation.com/u/jmoniz)\
**Post date:** [December 14, 2015, 6:21pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/8 "2015-12-14T18:21:19Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![IgnitionNewbie](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@IgnitionNewbie](https://forum.inductiveautomation.com/u/IgnitionNewbie)\
**Post date:** [February 22, 2023, 6:56pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/9 "2023-02-22T18:56:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 22, 2023, 7:04pm UTC](https://forum.inductiveautomation.com/t/python-to-retrieve-list-of-file-on-a-network-folder/10905/10 "2023-02-22T19:04:16Z")

</div>

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`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#walk(java.nio.file.Path,java.nio.file.FileVisitOption...)) or [`Files.walkFileTree`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#walkFileTree(java.nio.file.Path,java.nio.file.FileVisitor)).
