Hello everyone,
How do I read only files from the root folder?
for (path, dirs, files) in os.walk(arcpy.env.workspace):
for file in files:
print file
Oops, missed the "only files" part. os.path.isfile can be used as well, then:
python - List files in ONLY the current directory - Stack Overflow
As always, lots of ways to do the same thing.
But with os.walk you know you are only getting files, with os.listdir you need to do additional steps to determine the type of file system object.
I'd use os.listdir:
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> os <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> myFiles <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>listdir<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\Users\jona6782\Documents\ArcGIS"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>myFiles<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'49058003.jpg'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'AddIns'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'c5A5E1C002DC8445A8FC05C471B1.json'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Default.gdb'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'GeoNet'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'happy.png'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'JQNew - Copy.sd'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'JQNew.sd'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'LotsOfData.mxd'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MyProject8.ppkx'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MyService123.sd'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'New Folder'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'OnlineProjects'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Packages'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Portland.slpk'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Projects'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ProjectTemplates'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ProPortalData'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Rotterdam.slpk'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'scratch'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'scratch.gdb'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Shapefiles'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'TilingSchemes'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Toolbox.tbx'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'worldcities.gdb'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'WorldCities.mxd'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'WorldCitiesPG.mxd'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
It will return a list of the files/folders in a specific directory.
just add a break
......
#
# os.walk read only the root folder
break
Some good, workable solutions here: python - os.walk without digging into directories below - Stack Overflow
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.