Introduction To Python - Learn Python the hard way v2.0
Title: Exercise 17
$ cat ex-17.py
#!/usr/bin/python
from sys import argv
from os.path import exists
script, src, dst = argv
print "file-copy(src=%s, dst=%s)" % (src, dst)
s_fd = open(src)
s_data = s_fd.read()
s_fd.close()
print "The input file is %d bytes long" % len(s_data)
print "Checking if destination file is present: %r" % exists(dst)
d_fd = open(dst, "w")
d_fd.write(s_data)
d_fd.close()
print "File copied successfully."
$ chmod +x ex-17.py
$ ./ex-17.py ./ex-17.py test
file-copy(src=./ex-17.py, dst=test)
The input file is 406 bytes long
Checking if destination file is present: False
File copied successfully.
$ cat test
#!/usr/bin/python
from sys import argv
from os.path import exists
script, src, dst = argv
print "file-copy(src=%s, dst=%s)" % (src, dst)
s_fd = open(src)
s_data = s_fd.read()
s_fd.close()
print "The input file is %d bytes long" % len(s_data)
print "Checking if destination file is present: %r" % exists(dst)
d_fd = open(dst, "w")
d_fd.write(s_data)
d_fd.close()
print "File copied successfully."
### EXTRA CREDITS 1 ###
$ python
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> import file
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named file
>>> import mountpoint
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named mountpoint
>>> import basename
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named basename
>>> import random
>>> import printer
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named printer
>>>
### EXTRA CREDITS 3 ###
$ cat test
#!/usr/bin/python
from sys import argv
# script name is argv[0]
# source file name argv[1]
# destination file name argv[2]
open(argv[2], "w").write(open(argv[1]).read())
$ cat ex-17-03.py
#!/usr/bin/python
from sys import argv
# script name is argv[0]
# source file name argv[1]
# destination file name argv[2]
# since we need to import sys module, not able to write script in one line.
open(argv[2], "w").write(open(argv[1]).read())
$ chmod +x ex-17-03.py
$ ./ex-17-03.py ex-17-03.py test
$ cat test
#!/usr/bin/python
from sys import argv
# script name is argv[0]
# source file name argv[1]
# destination file name argv[2]
# since we need to import sys module, not able to write script in one line.
open(argv[2], "w").write(open(argv[1]).read())
### EXTRA CREDITS 6 ###
Under most of the circumstances the written contents would not be stored in
file unless file is flushed or file is closed. When a file is closed, the
written data is automatically flushed and then file is closed.
Here is an interesting program which waits for user to close the file. While
the program is waiting for input from user, the program allows user to check
the contents of file from another terminal.
$ cat ex-17-06.py
#!/usr/bin/python
from sys import argv
script, src, dst = argv
s_fd = open(src)
s_data = s_fd.read()
s_fd.close()
d_fd = open(dst, "w")
d_fd.write(s_data)
print '''File has been written, but not closed.
Open another terminal and check the contents of file.
'''
raw_input("Press a enter to close file.")
d_fd.close()
print "File copied successfully."
RUNNING PROGRAM
===============
$ ./ex-17-06.py ex-17-03.py test
File has been written, but not closed.
Open another terminal and check the contents of file.
Press a enter to close file.
File copied successfully.
CHECKING THE FILE CONTENTS WHEN THE ABOVE PROGRAM IS WAITING FOR USER INPUT
===========================================================================
$ ls
ex-17-03.py ex-17-06.py ex-17.py test
$ cat test
$ wc test
0 0 0 test
As can be seen from the output, the file is created at this moment but it has
no data
FILE CONTENTS AFTER PROGRAM FINISHES
====================================
$ cat test
#!/usr/bin/python
from sys import argv
# script name is argv[0]
# source file name argv[1]
# destination file name argv[2]
# since we need to import sys module, not able to write script in one line.
open(argv[2], "w").write(open(argv[1]).read())
$ wc test
10 38 249 test
Once file is closed the data is committed to disk. Now file has
10 lines, 38 words, and 249 characters
