Reading and Writing Files
Last updated
Was this helpful?
Last updated
Was this helpful?
We open files using the built-in open function. We need to tell the function if the file is to be used for reading, writing, or appending with the r, w, and a flags.
All the test files for the course are located at .
If you are on our cluster, you can copy them all to the current directory by typing:
Reading file seq.txt
Writing to file seq2.txt
Appending to file seq2.txt
Reading files with read()
and readlines()
We can read in a file using our Python script, process it, and output the results to an output file
Let's read in file seq.txt
find the palindrome sequences using our python script
Then output the palindrome sequences to file palindrome.txt
Let's do an exercise by writing a Python script to say hello to the class
First read in file class_list
as a list
Then output our greetings to file greetings
To avoid changing scripts, we can use arguments to read input files and to write output files
./hello_class2.py class_list greetings_again
Input another class list to hello_class2.py
will output greetings to another class
./hello_class2.py future_class_list greetings_to_future_class