File Management
Last updated
Was this helpful?
Last updated
Was this helpful?
Use Python to read and write file contents, as well as to create and delete files.
Reference:
See also: for reading and writing CSV files, and 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.
Write some Python strings to a text file called "my_message.txt" by running this script:
Inspect the contents of the "my_message.txt" file to see the message.
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: