From the course: Learning ArcGIS Python Scripting

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Read from and write to a text file

Read from and write to a text file - ArcGIS Pro Tutorial

From the course: Learning ArcGIS Python Scripting

Read from and write to a text file

- [Instructor] It's pretty common in the GIS world to get data in ask e-text format like a .TXT file or a comma separated values file, a CSV. Python can work with these. You can read data from a text file one line at a time and process it with your Python script, and you can write records out to a file as well. The process goes like this. Open the file with read, write, or append access. Read the records or write to the file, and then close the file. When the file is opened, a file handle is created. A file handle is an open connection to a file. For the rest of the script, when the file needs to be accessed, the file handle is used rather than the file name. Kind of like an object. Open a file handle with the open function. The open function takes two arguments, the name of the file and an access type, read, write or append. Here's an example of using read. inFile is the file handle we are creating. It's just like naming a variable, you can name it whatever you want, but no spaces or…

Contents