Input-Output (IO) Questions
1. Write a program that will prompt the user for their name and a directory on the computer, and then write the name to the file name.txt and save it in that directory. Assume the directory will be properly formatted and end with '\\'.
2. Add code to the end of the previous program that will reopen the file and prompt the user for the current day. The day should be printed on a new line in the file without overwriting the previous contents of the file.
3. Now add code to the end of the previous program that will open the file and print out its contents.
4. Write a program that prompts the user for a file location and then prints out every line in the file until the first blank line. Note: the lines read will contain the '\n' newline character; this character is actually 'printed' every time you press the enter key to start a new line, and it will have to be removed from the lines you read from the file. Consider the strip() method in str. For example, if we have a file with the following contents:
Hello
This is a file
That was a blank line
Bye!
then the program should print:
Hello
This is a file
5. Write a program that prompts the user for a file location and then prints the longest line in the file. For example, if we have the file with the following contents:
Hello
This is
A very long line.
then the program should read the lines in the file and print 'A very long line.' because this is the longest line in the file. You will again need to use the strip() method to get rid of '\n'.
6. The position of a certain particle was measured at times [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] and the measurements were [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]. Write a program that writes this data to a file called data.txt. The data should be formatted into two columns, the first being labelled 'time' and the second being labelled 'position'. Then write a program that will read the data from the file, store it in an array, and produce a position-time graph for the motion.