Chapter 13: More Issues In Input/Output

Using argc and argv, Page number: 467

In [1]:
import sys

if ( len(sys.argv) != 3 ):
    print ( "Improper number of arguments" )
    exit( )
    
fs = open ( sys.argv[2], "r" ) #open file in read mode
if ( not fs):
    print ( "Cannot open source file" )
    exit( )
    
ft = open ( sys.argv[2], "w" ) #open file in write mode
if ( not ft ):
    print ( "Cannot open target file" )
    fs.close()
    exit( )
    
while ( 1 ):
    ch = fs.read(1)
    if ( not ch ):
        break
    else:
        ft.write(ch)

#close files     
fs.close()
ft.close()
Improper number of arguments
In [ ]: