Lab 1 Solution


Category: Tag:


Write a program called in Python that reads a file and creates a copy of it in a sub-directory named recv (on the same system). The program will be executed using the command

python3 <filename>

where filename is a file in a local directory that needs to be copied. The program opens the file specified in the command line in binary mode and creates a new file with the same name in the recv directory. In a loop, the program keeps reading the next block of bytes (e.g., 1,000 bytes) from the file and writing it to the new file until all bytes have been read from the file. Finally, the program closes the two files. Submit well-documented code using the following command:

submit c3461aX lab1 <code-directory-name>

where X is a “dummy variable.” If you’re in Section 1 (TR, 11:10 a.m.–12:30 p.m.), your submit directory is c3461ab; if you’re in Section 2 (MWF, 3:00 p.m.–3:55 p.m.), your submit directory is c3461aa.

No buffer, array, or data structure used should exceed 1,000 bytes in size. You cannot read and keep the entire file in memory as your program should be able to work with files of arbitrary size. Your program should work for binary files (e.g., images). Submit a README.txt file with your program. You can use the sample images on the course webpage to test your program. Use diff or md5sum at the command line to ensure that the transferred file is bitwise identical to the original one.

Advice: The following may help you with the labs in this course.

Labs 2–4 build on previous labs, respectively, so make sure your lab implementations are correct.

Even if you have experience with Python 2, you are strongly encouraged to use Python 3 for the labs. Python 3 makes byte encoding and decoding much easier than Python 2, which requires tedious use of the struct package. (If you use Python 2, please note this in your README.txt file for the grader.)

Don’t procrastinate – labs can take much longer than you might expect initially.


error: Content is protected !!