Use Python to read and write file contents, as well as to create and delete files.
Reference:
- https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
- https://docs.python.org/3/glossary.html#term-file-object
- https://docs.python.org/3/library/io.html#module-io
- https://www.tutorialspoint.com/python/python_files_io.htm
See also: the csv
module for reading and writing CSV files, and the os
module for command-line-style file operations and functionality to help specify file paths.
To setup these examples, create a new directory on your Desktop called "file-mgmt" and navigate there from your command line. Create a Python script in that directory called my_script.py
and place inside it contents from each of the following sections, respectively.
Write content to file:
file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.
with open(file_name, "w") as file: # NOTE: "w" means "open the file for writing"
file.write("Hello World")
file.write("\n")
file.write("\n")
file.write("...")
file.write("\n")
file.write("\n")
file.write("Goodbye")
#> Hello World
#>
#> ...
#>
#> Goodbye
Read file contents:
file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.
with open(file_name, "r") as file: # NOTE: "r" means "open the file for reading"
contents = file.read()
print(contents)
#> Hello World.
#>
#> ...
#>
#> Goodbye World.
Split file contents on line breaks and loop through each line:
file_name = "my_message.txt" # refers to a file path relative to the path from which you invoke your your script.
with open(file_name, "r") as file: # NOTE: "r" means "open the file for reading"
contents = file.read()
lines = contents.split("\n")
print("THERE ARE", len(lines), "LINES IN THIS FILE")
for line in lines:
print("LINE:", line)
#> THERE ARE 5 LINES IN THIS FILE
#> LINE: Hello World
#> LINE:
#> LINE: ...
#> LINE:
#> LINE: Goodbye.