File Management

Use Python to read and write file contents, as well as to create and delete files.

Reference:

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 two Python scripts in that directory called "write_message.py" and "read_message.py", and place inside contents from the following sections, respectively.

Writing Files

Write some Python strings to a text file called "my_message.txt" by running this script:

# file-mgmt/write_message.py

file_name = "my_message.txt" # a relative filepath

with open(file_name, "w") as file: # "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("Hello Again")

Inspect the contents of the "my_message.txt" file to see the message.

Reading Files

Process the "my_message.txt" file into a Python string by running this script:

Further, it's possible to split the file contents on line break characters (\n) to assemble a Python list of strings, each representing its own line. Try revising the script and running it again:

Last updated

Was this helpful?