Skip to content

Commit 8b50967

Browse files
authored
Merge pull request #20 from gcshearman/master
Writing files York 2025
2 parents f8979f8 + 2a60650 commit 8b50967

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

notebooks/writing_files.ipynb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyOe2vlSb52narfb6tegF/97"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":null,"metadata":{"id":"R-GtgLtZw1hH"},"outputs":[],"source":[]},{"cell_type":"markdown","source":["# **Learning Outcomes**\n","\n","The learning outcomes are as follows:\n","\n","1. Saving data into a newly created file\n","2. Add additional data into an existing file."],"metadata":{"id":"yjL3LTCFJXwl"}},{"cell_type":"markdown","source":["# **Pre-requisites**\n","\n","Variables <br>\n","FOR loops <br>\n","Writing f-strings <br>\n","\n","\n"],"metadata":{"id":"hTDYb2QRyNeb"}},{"cell_type":"markdown","source":["# **Writing files**\n","\n","It is often necesary to be able to save chemical data, images etc. that have been generated as a result of data processing etc., which is known as 'writing'. You can choose either to write your data to:\n","(i) a new file, or\n","(ii) alternatively add (aka append) to an existing file.\n"],"metadata":{"id":"xUUYv_pr1Sbt"}},{"cell_type":"markdown","source":["# **Writing data to a new file**\n","\n","In Python, in order to write data to a new file, you must first create a file, ensuring the the file is writable.\n","\n","In order to create a file, you can use the command 'open'. Following this (in ( ) brackets) you need to supply the name of the file that you want to create, followed by a comma and then indicate that you wish to *write* to the new file (as opposed to e.g. read) by using the tag 'w'.\n","\n","A simple example of this is given below:"],"metadata":{"id":"BVX99iEZ3Rc2"}},{"cell_type":"code","source":["file = open('gas_const.txt', 'w')\n","\n","R = 8.314\n","file.write(\"The gas constant is: \" + str(R) + \" J/K.mol\")\n","\n","file.close()"],"metadata":{"id":"JRvmRpz13Q0o"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["We have defined and assigned a value to the variable, R, that is to be written to the file. R will be written to the file as a string. We have constructed the string \"The gas constant is: 8.314 J/K.mol\" which is then written to the file using the file.write command. Lastly the file is closed.\n","\n","Google Colab: This simple file has been saved in a temporary storage location. You can have a look at the file that's just been written by selecting the File icon on the left-hand side of the Colab page. Within the 'Content' file, you should be able to see the file. Click on the three vertical dots on the right-hand side of the file, download and open.\n","**NOTICE FOR EDITORS: we appreciate that we've written this for Google Colab - so we will need to re-write this section**"],"metadata":{"id":"KI8esPi893AW"}},{"cell_type":"markdown","source":["The way in which data is written to a file depends on the type of data itself. So... FINISH"],"metadata":{"id":"JQCkCjKt9UI7"}},{"cell_type":"markdown","source":["# **Writing a simple structured file**\n","\n","XXXX\n","\n","In the example below, the ... FINISH"],"metadata":{"id":"uYsrtUqF_k_s"}},{"cell_type":"code","source":["file = open('spectrum.dat', 'w')\n","\n","wavelength = [240, 250, 260, 270, 280, 290]\n","absorbance = [0.123, 0.132, 0.346, 0.563, 0.998, 0.377, 0.021]\n","file.write(f\"nm \\t abs \\n\")\n","for i in range(len(wavelength)):\n"," file.write(f\"{wavelength[i]} \\t {absorbance[i]}\\n\")\n","\n","file.close()"],"metadata":{"id":"VPqkYrd1zla5"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["In the above example, the 'f' in the file.write commands... FINISH"],"metadata":{"id":"YBO7aNknGp7b"}},{"cell_type":"markdown","source":["# **Appending to an existing file**\n","\n","You can also write data to an existsing file. In the example below, we will append some more data to the spectrum data created above.\n","\n","... FINISH"],"metadata":{"id":"RscVg7DyHKVW"}},{"cell_type":"code","source":["file = open('spectrum.dat', 'a')\n","\n","wavelength = [300, 310]\n","absorbance = [0.007, 0.002]\n","\n","for i in range(len(wavelength)):\n"," file.write(f\"{wavelength[i]} \\t {absorbance[i]}\\n\")\n","\n","file.close()"],"metadata":{"id":"xpAqJWu2_ss2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# **User-defined save paths**\n","\n","Although a library is required for a file dialogue pop-up, ... FINISH ALSO NEED TO CREATE CODE FOR THIS."],"metadata":{"id":"Kp8OJXO1_oMr"}},{"cell_type":"markdown","source":["# **Exercises**\n","\n","**Exercise 1** <br>\n","Take the day of the month in which you were born (e.g. this would be 10 is you were born on the 10th April), and create a file named 'element.txt' and write the name of the element corresponding to the atomic number matching this. <br><br>\n","\n","**Exercise 2** <br>\n","Determine the group that the element identified in Exercise 1 below to and append this information to your file. <br><br>\n","\n","**Exercise 3:** <br>\n","Three moles of an ideal gas are ontained within a frictionless piston at 298.15 K. Use Python to calculate the volume of the gas at the following four different pressures: <br>\n","1.00 kPa <br>\n","10.00 kPa <br>\n","50.00 kPa <br>\n","100.00 kPa <br>\n","and output the results in a file, formatted as two columns of numbers (to two d.p.), with the first column being pressure and the second being volume."],"metadata":{"id":"aVs6rJBuJLiw"}},{"cell_type":"markdown","source":["# **Learning Outcomes**\n","\n","FINISH"],"metadata":{"id":"tOX5HbrsJR02"}}]}

0 commit comments

Comments
 (0)