4

Ok well i have another question. I implemented the error checking but for some reason it still isn't working. I still get a python error instead of the error i just wrote in the program.

Traceback (most recent call last):
  File "E:/python/copyfile.py", line 31, in <module>
    copyFile()
  File "E:/python/copyfile.py", line 8, in copyFile
    file1 = open(source,"r")
IOError: [Errno 2] No such file or directory: 'C:/Users/Public/asdf.txt'
Aegg
  • 63
  • 2
  • 6
  • 1
    Just FYI, there is [shutil.copyfile(src, dst)](http://docs.python.org/2/library/shutil.html#shutil.copyfile). – squiguy Dec 16 '13 at 17:16
  • So, what does your program do when you pass in an invalid filename? That should give you an idea of what to look for as you attempt to read/write files. Do you know how to implement exception-handling? If not, do a quick Google on 'python exception handling' and see what you can come up with. Good luck :) – bedwyr Dec 16 '13 at 17:16
  • Possible duplicate of http://stackoverflow.com/q/8380006/1579844 – Yariv Dec 16 '13 at 17:16
  • Traceback (most recent call last): File "", line 1, in copyFile() File "E:/python/copyfile.py", line 6, in copyFile file1 = open(source,"r") IOError: [Errno 2] No such file or directory: '/Users/Public/asdf.txt' This is the error I get. – Aegg Dec 16 '13 at 17:17
  • Alright I figured it out... thanks to you guys :) The problem was that I had my try and except statements all in the wrong places. With a quick move the problem was quickly resolved. Thank you guys again. – Aegg Dec 16 '13 at 19:27
  • Please don't deface your question, now that you have received answers to it. Questions aren't just for the benefit of the asker, but for many visitors in the future. – Brad Larson Dec 16 '13 at 20:22

2 Answers2

9

check out the shutil module in standard library:

shutil.copyfile(src, dst)

http://docs.python.org/2/library/shutil.html#shutil.copyfile

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • I'm not allowed to use any built in functions. anyways, i got it updated... but apparently the check still isn't quite right.. – Aegg Dec 16 '13 at 17:54
1

I would rather ask you to write your own:

import os
import hashlib

def md5ChkSum(_file):  # Calculates MD5 CheckSum
    with open(_file, 'rb') as fp:
        hash_obj = hashlib.md5()

        line = fp.readline()
        while line:
            hash_obj.update(line)
            line = fp.readline()
        return hash_obj.hexdigest()

def copier(_src, _dst):
    if not os.path.exists(_src):
        return False

    _src_fp = open(_src, "r")
    _dst_fp = open(_dst, "w")

    line = _src_fp.readline()
    while line:
        _dst_fp.write(line)
        line = _src_fp.readline()
    _src_fp.close()
    _dst_fp.close()

    if md5ChkSum(_src) == md5ChkSum(_dst):
        return "Copy: SUCCESSFUL"
    return "Copy: FAILED"

res = copier(r"/home/cnsiva/6.jpg", r"/home/cnsiva/6_copied.jpg")
if not res:
    print "FILE Does not Exists !!!"
else: print res

OUTPUT:

Copy: SUCCESSFUL
Siva Cn
  • 929
  • 4
  • 10