5

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'
6
  • 1
    Just FYI, there is shutil.copyfile(src, dst). Commented Dec 16, 2013 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 :) Commented Dec 16, 2013 at 17:16
  • Possible duplicate of stackoverflow.com/q/8380006/1579844 Commented Dec 16, 2013 at 17:16
  • Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> 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. Commented Dec 16, 2013 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. Commented Dec 16, 2013 at 19:27

2 Answers 2

9

check out the shutil module in standard library:

shutil.copyfile(src, dst)

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not allowed to use any built in functions. anyways, i got it updated... but apparently the check still isn't quite right..
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

5 Comments

@khachik, Does it make any trouble, here we are not stick to the stylings. Check for the Functionality !!!
for the functionality there is shutil, no need to reinvent it.
@khachik, the Question is not to use the built-in methods
shutil is not built-in and I don't see anything about built-ins in the question as well.
Check the comment on the other answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.