<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Reading and writing in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033761#M60270</link>
    <description>&lt;P&gt;Per your question on how to run this on multiple file extensions, the simplest solution is just to call the locate function multiple times.&amp;nbsp; Such as:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for ext in ('*.txt', '*.thn', '*.csv'):
  # This will run three times first returning all 
  # the TXT files, then THN,and finally CSV files
  filelist = locate(ext, r'D:\path')
  for fp in filelist: 
    # Do what you need to with this file
    # You can also use the variable 'ext' here to
    # process files differently base upon the 'ext'&lt;/LI-CODE&gt;</description>
    <pubDate>Sun, 07 Mar 2021 13:59:14 GMT</pubDate>
    <dc:creator>LanceCole</dc:creator>
    <dc:date>2021-03-07T13:59:14Z</dc:date>
    <item>
      <title>Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033673#M60251</link>
      <description />
      <pubDate>Sun, 07 Mar 2021 15:06:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033673#M60251</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-07T15:06:57Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033681#M60252</link>
      <description>&lt;P&gt;&lt;A href="https://www.w3schools.com/python/python_file_write.asp" target="_blank"&gt;Python File Write (w3schools.com)&lt;/A&gt;&amp;nbsp;- be very careful which mode you open the textfiles in, as this can delete them inadvertently.&amp;nbsp; e.g. 'w', 'a', 'ab'... I'd really recommend on reading up on them first.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#the os.path.join method is useful for joining paths
import os

#folder that texfiles will be saved in
myfolder = r'D:\Downloads'

#name of your textfiles
textfile_name_1 = 'demo1.txt'
textfile_name_2 = 'demo2.txt'

#concatenate(add together) path strings
textfile_path_1 = myfolder + '\\' + textfile_name_1
textfile_path_2 = myfolder + '\\' + textfile_name_2
#see the resulting paths
print(textfile_path_1)
print(textfile_path_2)

#or I prefer to use os.path.join
textfile_path_1 = os.path.join(myfolder, textfile_name_1)
textfile_path_2 = os.path.join(myfolder, textfile_name_2)
#see the resulting paths
print(textfile_path_1)
print(textfile_path_2)

#open/create a textfile and write to it
#the 'w' means write, but will also delete and
#recreate the entire textfile if already exists
f = open(textfile_path_1, 'w')
f.write('I have created a textfile and written this line to it')
f.close()

#the with statement means no .close() method is needed
with open(textfile_path_2, 'w') as text_file:
    text_file.write('I have created a tetxfile and written this line to it')&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 06 Mar 2021 12:26:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033681#M60252</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-06T12:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033682#M60253</link>
      <description>&lt;P&gt;Unfortunately, I have more than one hundred files in the folder, so it is very difficult to indicate all of them.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 12:34:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033682#M60253</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-06T12:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033683#M60254</link>
      <description>&lt;P&gt;No fair David, you can type faster than I can.&lt;/P&gt;&lt;P&gt;One other note.&amp;nbsp; Make sure you use a Python raw string for your path.&amp;nbsp; This will treat the backslash (\) as a literal character and not as an escape character.&amp;nbsp; David has included this in is example by using the lower case (r) in front of his path "myfolder" string.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;myfolder = r'D:\Downloads'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 12:39:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033683#M60254</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2021-03-06T12:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033684#M60255</link>
      <description>&lt;P&gt;What do you want to actually do?&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 12:45:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033684#M60255</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-06T12:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033687#M60257</link>
      <description>&lt;P&gt;Mick,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you trying to crawl through a folder looking for files and their path?&amp;nbsp; I use the following to reclusively look for a particular file type in a folder and subfolders.&amp;nbsp; The function call below returns a list containing the folder and filename for all "txt" files in a directory and its subdirectors.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, fnmatch

def locate(pattern, root=os.curdir):
  for folder, subs, files in os.walk(root):
    for filename in fnmatch.filter(files, pattern):
      yield os.path.join(folder, filename)

filelist = locate('*.txt', r'D:\path')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 12:58:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033687#M60257</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2021-03-06T12:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033688#M60258</link>
      <description>&lt;P&gt;wow you were ready for that one!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os

my_folder = r'D:\Downloads'

file_list = []
#walk your folder
for root, dirs, files in os.walk(my_folder):
    
    for filename in files:
        #join filename to folder path
        filepath = os.path.join(my_folder, filename)
        #print to test the functionality
        print(filepath)
        #you can also append all the filepaths to a list, rather
        #than within this generator.
        #also added a test for .txt in filename
        if '.txt' in filename:
            
            file_list.append(filepath)
        
        
    break&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 06 Mar 2021 13:01:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033688#M60258</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-06T13:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033689#M60259</link>
      <description>&lt;P&gt;Yes, you are right. I have tried and received "line 9, in &amp;lt;module&amp;gt;&lt;BR /&gt;with open(filelist,"r") as f:&lt;BR /&gt;TypeError: coercing to Unicode: need string or buffer, generator found".&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 13:06:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033689#M60259</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-06T13:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033691#M60260</link>
      <description>&lt;P&gt;Mick, if you are referencing the filelist from Dave or my examples you iterate through the list using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for fp in filelist:
  # fp is the full path and filename for each item in the filelist
  with open(fp,"r") as f:
    # do what you need here with the file data
    &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 16:40:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033691#M60260</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2021-03-06T16:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033693#M60261</link>
      <description>&lt;P&gt;I have a syntax error for "as" in&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; open(fp, "r") &lt;STRONG&gt;as&lt;/STRONG&gt; f.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 13:26:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033693#M60261</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-06T13:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033699#M60262</link>
      <description>&lt;LI-CODE lang="python"&gt;for fp in filelist:
  with open(fp, "r") as f:
    &lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 06 Mar 2021 13:51:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033699#M60262</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-06T13:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033717#M60264</link>
      <description>&lt;P&gt;Sorry, I left the "with" out of the statement when I cut and pasted.&amp;nbsp; I have corrected my previous post.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 16:43:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033717#M60264</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2021-03-06T16:43:33Z</dc:date>
    </item>
    <item>
      <title>Re: Reading and writing</title>
      <link>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033761#M60270</link>
      <description>&lt;P&gt;Per your question on how to run this on multiple file extensions, the simplest solution is just to call the locate function multiple times.&amp;nbsp; Such as:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for ext in ('*.txt', '*.thn', '*.csv'):
  # This will run three times first returning all 
  # the TXT files, then THN,and finally CSV files
  filelist = locate(ext, r'D:\path')
  for fp in filelist: 
    # Do what you need to with this file
    # You can also use the variable 'ext' here to
    # process files differently base upon the 'ext'&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 07 Mar 2021 13:59:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/reading-and-writing/m-p/1033761#M60270</guid>
      <dc:creator>LanceCole</dc:creator>
      <dc:date>2021-03-07T13:59:14Z</dc:date>
    </item>
  </channel>
</rss>

