{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2020-03-12T17:53:07.574269Z", "start_time": "2020-03-12T17:53:07.571922Z" } }, "source": [ "# Lesson A9 – Working with files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Text files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sooner or later you will be confronted with a situation where you want to\n", "create a text file to store some data in it, or you want to read data from a\n", "file to work with it. For example you may want to read in the coordinates of a molecule from a structure file (maybe an entry from the PDB-database) or you could want to write a human-readable log-file during the execution of a program. A text file can be considered as a sequence of lines (or\n", "rows), each having a basically unlimited number of characters. In general, a text\n", "file is read and written to line by line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The straighforward approach to open a file, write to it, and close it would look\n", "something like this in Python:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-03-12T18:19:44.471124Z", "start_time": "2020-03-12T18:19:44.466319Z" } }, "outputs": [], "source": [ "file_ = open(\"io/file.txt\", \"w\") # open a file in \"w\" write mode\n", "file_.write(\"0 1\\n\") # write to the file\n", "file_.close() # close the file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "