arcpy

2119
12
Jump to solution
03-12-2021 02:25 AM
Mick
by
New Contributor

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

I guess everything because of fileName[:-4] + ".txt",so Python can not define and write in each file.  However, I don't know how to correct it.

import arcpy

inPut = r'C:\12'


arcpy.env.workspace = inPut

myList = arcpy.ListFiles()

for fileName in myList:

if fileName.endswith(".txt"):
     out = fileName[:-4] + ".txt"
     with open(out, "w") as k:
         k.write('Hellow')

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Yes, your method would have done the same, it's the 'w' write argument which replaces a file if it exists.

python - Difference between modes a, a+, w, w+, and r+ in built-in open function? - Stack Overflow

you may wish to use the 'a' append argument or others.

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')

View solution in original post

12 Replies
Mick
by
New Contributor

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

0 Kudos
Mick
by
New Contributor

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. 

0 Kudos
by Anonymous User
Not applicable

Sorry, its too early...

Edited: I think you need the full path for open. I dont think it can use the workspace like arcpy does.

open(‘full path to the file’, w) as k:

0 Kudos
Luke_Pinner
MVP Regular Contributor

Does arcpy.ListFiles() return the 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')?

Mick
by
New Contributor

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.

I have tried arcpy.ListFiles('*.txt'), but the result is the same.

0 Kudos
Luke_Pinner
MVP Regular Contributor

That's because @Anonymous User points out, you need to pass the full filepath:

 

     with open(os.path.join(inPut, out), "w") as k:
         k.write('Hellow')

 

Note, the above will overwrite your original files. If you want to append to the existing files, use the 'a' argument to open:

 

     with open(os.path.join(inPut, out), "a") as k:
         k.write('Hellow')

 

 

Mick
by
New Contributor

I have corrected my script. I had an issue with 'workspace'.

import arcpy

inPut = r'C:\12'


inPut = arcpy.env.workspace

myList = arcpy.ListFiles()

for fileName in myList:
if fileName.endswith(".txt"):
      out = fileName[:-4] + ".txt"
      with open(out, "w") as k:
          k.write('Hellow')

I have an error (line 10, in <module> for fileName in myList: TypeError: 'NoneType' object is not iterable)

0 Kudos
Luke_Pinner
MVP Regular Contributor

arcpy.env.workspace = inPut is correct, you set the workspace to a path

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)

0 Kudos
DavidPike
MVP Frequent Contributor

it would have to be arcpy.env.workspace = inPut

As others have said, It's the arcpy workspace setting not being interpreted by the file.write() method.

Without using the os module and os.path.join:

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')