<?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: arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035869#M60358</link>
    <description>&lt;P&gt;Actually, it creates the copies of my file with 'Hellow' not in the folder where located the files but in the folder where my script located. Moreover, it deletes all additional information inside of the copied files.&lt;/P&gt;&lt;P&gt;I have tried&amp;nbsp;&lt;SPAN&gt;arcpy.ListFiles('*.txt'), but the result is the same.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 12 Mar 2021 11:37:04 GMT</pubDate>
    <dc:creator>Mick</dc:creator>
    <dc:date>2021-03-12T11:37:04Z</dc:date>
    <item>
      <title>arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035851#M60351</link>
      <description>&lt;P&gt;Can you explain why I have problems with 'with open(out, "w") as k:'? I would like to create a script to use for ArcGIS to add to each file in the folder with text extension 'Hellow'.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess everything because of&amp;nbsp;fileName[:-4] + ".txt",so Python can not define and write in each file.&amp;nbsp; However, I don't know how to correct it.&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;inPut = r'C:\12'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;arcpy.env.workspace = inPut&lt;/P&gt;&lt;P&gt;myList = arcpy.ListFiles()&lt;/P&gt;&lt;P&gt;for fileName in myList:&lt;BR /&gt;&lt;BR /&gt;if fileName.endswith(".txt"):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;out = fileName[:-4] + ".txt"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;with open(out, "w") as k:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;k.write('Hellow')&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 10:25:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035851#M60351</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T10:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035861#M60353</link>
      <description>&lt;P&gt;My main problem is that function write does not work with a folder (it works with each file). I know how to deal by using other modules but I do not know how to use only with arcpy. I have one hundred .txt files in my folder and want to add text "Hellow"in each file with the function "write".&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 10:58:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035861#M60353</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T10:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035864#M60355</link>
      <description>&lt;P&gt;It runs without any changes inside of the files (I do not have any errors after the process ). I would like only to learn how to use only arcpy module for this purpose.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:17:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035864#M60355</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T11:17:07Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035865#M60356</link>
      <description>&lt;P&gt;Sorry, its too early...&lt;/P&gt;&lt;P&gt;Edited: I think you need the full path for open. I dont think it can use the workspace like arcpy does.&lt;/P&gt;&lt;P&gt;open(‘full path to the file’, w) as k:&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:28:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035865#M60356</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-03-12T11:28:50Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035866#M60357</link>
      <description>&lt;P&gt;Does&amp;nbsp;&lt;SPAN&gt;arcpy.ListFiles() return the&amp;nbsp;file list you expect? Have you inspected the returned list to make sure it's not empty? Have you tried setting a wildcard filter, e.g. arcpy.ListFiles('*.txt')?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:24:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035866#M60357</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-03-12T11:24:33Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035869#M60358</link>
      <description>&lt;P&gt;Actually, it creates the copies of my file with 'Hellow' not in the folder where located the files but in the folder where my script located. Moreover, it deletes all additional information inside of the copied files.&lt;/P&gt;&lt;P&gt;I have tried&amp;nbsp;&lt;SPAN&gt;arcpy.ListFiles('*.txt'), but the result is the same.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:37:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035869#M60358</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T11:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035880#M60359</link>
      <description>&lt;P&gt;That's because&amp;nbsp;@Anonymous User&amp;nbsp;points out, you need to pass the full filepath:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;     with open(os.path.join(inPut, out), "w") as k:
         k.write('Hellow')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note, the above will &lt;EM&gt;overwrite&lt;/EM&gt; your original files. If you want to &lt;EM&gt;append&lt;/EM&gt; to the existing files, use the 'a' argument to open:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;     with open(os.path.join(inPut, out), "a") as k:
         k.write('Hellow')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:49:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035880#M60359</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-03-12T11:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035884#M60360</link>
      <description>&lt;P&gt;I have corrected my script. I had an issue with 'workspace'.&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;inPut = r'C:\12'&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;inPut = arcpy.env.workspace&lt;/P&gt;&lt;P&gt;myList = arcpy.ListFiles()&lt;/P&gt;&lt;P&gt;for fileName in myList:&lt;BR /&gt;if fileName.endswith(".txt"):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; out = fileName[:-4] + ".txt"&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; with open(out, "w") as k:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; k.write('Hellow')&lt;/P&gt;&lt;P&gt;I have an error (line 10, in &amp;lt;module&amp;gt; for fileName in myList: TypeError: 'NoneType' object is not iterable)&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 11:59:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035884#M60360</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T11:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035887#M60361</link>
      <description>&lt;P&gt;arcpy.env.workspace = inPut is correct, you set the workspace to a path&lt;/P&gt;&lt;P&gt;inPut = arcpy.env.workspace is wrong, you are setting your inPut variable to whatever the workspace is (which will be None if you haven't set it previously)&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 12:13:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035887#M60361</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-03-12T12:13:02Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035888#M60362</link>
      <description>&lt;P&gt;it would have to be arcpy.env.workspace = inPut&lt;/P&gt;&lt;P&gt;As others have said, It's the arcpy workspace setting not being interpreted by the file.write() method.&lt;/P&gt;&lt;P&gt;Without using the os module and os.path.join:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;inPut = r'D:\Downloads\aatester'


arcpy.env.workspace = inPut

myList = arcpy.ListFiles()

for fileName in myList:

    if fileName.endswith(".txt"):
       out = inPut + "\\" + fileName[:-4] + ".txt"
       print(out)
       with open(out, "w") as k:
         k.write('Hellow')&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 12 Mar 2021 12:17:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035888#M60362</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-12T12:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035894#M60363</link>
      <description>&lt;P&gt;I see. Thank you. Your method deletes everything inside the files and replaces it for only 'Hellow'.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 12:47:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035894#M60363</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-12T12:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035896#M60364</link>
      <description>&lt;P&gt;Yes, your method would have done the same, it's the 'w' write argument which replaces a file if it exists.&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function" target="_blank"&gt;python - Difference between modes a, a+, w, w+, and r+ in built-in open function? - Stack Overflow&lt;/A&gt;&lt;/P&gt;&lt;P&gt;you may wish to use the 'a' append argument or others.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;inPut = r'D:\Downloads\aatester'


arcpy.env.workspace = inPut

myList = arcpy.ListFiles()

for fileName in myList:

    if fileName.endswith(".txt"):
       out = inPut + "\\" + fileName[:-4] + ".txt"
       print(out)
       with open(out, "a") as k:
         k.write('Hellow')&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 12 Mar 2021 12:55:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035896#M60364</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-12T12:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy</title>
      <link>https://community.esri.com/t5/python-questions/arcpy/m-p/1035935#M60366</link>
      <description>&lt;P&gt;Sorry if I convolute this situation more for you &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/460169"&gt;@Mick&lt;/a&gt;&amp;nbsp;, but I just want to add that you can use a wildcard argument with &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/listfiles.htm" target="_self"&gt;ListFiles()&lt;/A&gt; to only return txt files.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os

inPut = r'D:\Downloads\aatester'

arcpy.env.workspace = inPut

myList = arcpy.ListFiles("*.txt")

for fileName in myList:
    out = os.path.join(inPut, f"{fileName[:-4]}.txt")

    print(out)
    with open(out, "a") as k:
        k.write('Hellow')&lt;/LI-CODE&gt;&lt;P&gt;Additionally, I updated your path construction to use os.path.join() so it reliably does the separator. You will also notice the f string formatting for the file name. This will function the same but I just wanted to demonstrate there are multiple ways to accomplish something.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Mar 2021 14:54:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy/m-p/1035935#M60366</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2021-03-12T14:54:56Z</dc:date>
    </item>
  </channel>
</rss>

